parse-options: factor out usage_indent() and usage_padding()

Extract functions for printing spaces before and after options. We'll need them in the next commit. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Aug 5, 2023 at 16:43 UTC 652a6b15bc1cd0f837bc969e87fd31f3e88413f6
1 file changed +24 -15
parse-options.c
+24 -15
@@ -1020,9 +1020,28 @@ static int usage_argh(const struct option *opts, FILE *outfile)
1020 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
1021 }
1022
1023 +static int usage_indent(FILE *outfile)
1024 +{
1025 + return fprintf(outfile, " ");
1026 +}
1027 +
1028 #define USAGE_OPTS_WIDTH 24
1029 #define USAGE_GAP 2
1030
1031 +static void usage_padding(FILE *outfile, size_t pos)
1032 +{
1033 + int pad;
1034 + if (pos == USAGE_OPTS_WIDTH + 1)
1035 + pad = -1;
1036 + else if (pos <= USAGE_OPTS_WIDTH)
1037 + pad = USAGE_OPTS_WIDTH - pos;
1038 + else {
1039 + fputc('\n', outfile);
1040 + pad = USAGE_OPTS_WIDTH;
1041 + }
1042 + fprintf(outfile, "%*s", pad + USAGE_GAP, "");
1043 +}
1044 +
1045 static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *ctx,
1046 const char * const *usagestr,
1047 const struct option *opts,
@@ -1108,7 +1127,6 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
1127
1128 for (; opts->type != OPTION_END; opts++) {
1129 size_t pos;
1111 - int pad;
1130 const char *cp, *np;
1131
1132 if (opts->type == OPTION_SUBCOMMAND)
@@ -1128,7 +1146,7 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
1146 need_newline = 0;
1147 }
1148
1131 - pos = fprintf(outfile, " ");
1149 + pos = usage_indent(outfile);
1150 if (opts->short_name) {
1151 if (opts->flags & PARSE_OPT_NODASH)
1152 pos += fprintf(outfile, "%c", opts->short_name);
@@ -1152,16 +1170,8 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
1170 !(opts->flags & PARSE_OPT_NOARG))
1171 pos += usage_argh(opts, outfile);
1172
1155 - if (pos == USAGE_OPTS_WIDTH + 1)
1156 - pad = -1;
1157 - else if (pos <= USAGE_OPTS_WIDTH)
1158 - pad = USAGE_OPTS_WIDTH - pos;
1159 - else {
1160 - fputc('\n', outfile);
1161 - pad = USAGE_OPTS_WIDTH;
1162 - }
1173 if (opts->type == OPTION_ALIAS) {
1164 - fprintf(outfile, "%*s", pad + USAGE_GAP, "");
1174 + usage_padding(outfile, pos);
1175 fprintf_ln(outfile, _("alias of --%s"),
1176 (const char *)opts->value);
1177 continue;
@@ -1169,12 +1179,11 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
1179
1180 for (cp = _(opts->help); *cp; cp = np) {
1181 np = strchrnul(cp, '\n');
1172 - fprintf(outfile,
1173 - "%*s%.*s\n", pad + USAGE_GAP, "",
1174 - (int)(np - cp), cp);
1182 + usage_padding(outfile, pos);
1183 + fprintf(outfile, "%.*s\n", (int)(np - cp), cp);
1184 if (*np)
1185 np++;
1177 - pad = USAGE_OPTS_WIDTH;
1186 + pos = 0;
1187 }
1188 }
1189 fputc('\n', outfile);