Skip to content

Commit 652a6b1

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e8e5d29 commit 652a6b1

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

parse-options.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,9 +1020,28 @@ static int usage_argh(const struct option *opts, FILE *outfile)
10201020
return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
10211021
}
10221022

1023+
static int usage_indent(FILE *outfile)
1024+
{
1025+
return fprintf(outfile, " ");
1026+
}
1027+
10231028
#define USAGE_OPTS_WIDTH 24
10241029
#define USAGE_GAP 2
10251030

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+
10261045
static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *ctx,
10271046
const char * const *usagestr,
10281047
const struct option *opts,
@@ -1108,7 +1127,6 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
11081127

11091128
for (; opts->type != OPTION_END; opts++) {
11101129
size_t pos;
1111-
int pad;
11121130
const char *cp, *np;
11131131

11141132
if (opts->type == OPTION_SUBCOMMAND)
@@ -1128,7 +1146,7 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
11281146
need_newline = 0;
11291147
}
11301148

1131-
pos = fprintf(outfile, " ");
1149+
pos = usage_indent(outfile);
11321150
if (opts->short_name) {
11331151
if (opts->flags & PARSE_OPT_NODASH)
11341152
pos += fprintf(outfile, "%c", opts->short_name);
@@ -1152,29 +1170,20 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
11521170
!(opts->flags & PARSE_OPT_NOARG))
11531171
pos += usage_argh(opts, outfile);
11541172

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-
}
11631173
if (opts->type == OPTION_ALIAS) {
1164-
fprintf(outfile, "%*s", pad + USAGE_GAP, "");
1174+
usage_padding(outfile, pos);
11651175
fprintf_ln(outfile, _("alias of --%s"),
11661176
(const char *)opts->value);
11671177
continue;
11681178
}
11691179

11701180
for (cp = _(opts->help); *cp; cp = np) {
11711181
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);
11751184
if (*np)
11761185
np++;
1177-
pad = USAGE_OPTS_WIDTH;
1186+
pos = 0;
11781187
}
11791188
}
11801189
fputc('\n', outfile);

0 commit comments

Comments
 (0)