Skip to content

Commit f9712d7

Browse files
committed
Merge branch 'jc/parse-options-short-help'
Command line parser fix, and a small parse-options API update. * jc/parse-options-short-help: short help: allow a gap smaller than USAGE_GAP remote: simplify "remote add --tags" help text short help: allow multi-line opthelp
2 parents 23b20ff + c512643 commit f9712d7

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

builtin/remote.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,9 @@ static int add(int argc, const char **argv, const char *prefix)
168168
struct option options[] = {
169169
OPT_BOOL('f', "fetch", &fetch, N_("fetch the remote branches")),
170170
OPT_SET_INT(0, "tags", &fetch_tags,
171-
N_("import all tags and associated objects when fetching"),
171+
N_("import all tags and associated objects when fetching\n"
172+
"or do not fetch any tag at all (--no-tags)"),
172173
TAGS_SET),
173-
OPT_SET_INT(0, NULL, &fetch_tags,
174-
N_("or do not fetch any tag at all (--no-tags)"), TAGS_UNSET),
175174
OPT_STRING_LIST('t', "track", &track, N_("branch"),
176175
N_("branch(es) to track")),
177176
OPT_STRING('m', "master", &master, N_("branch"), N_("master branch")),

parse-options.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,7 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
11091109
for (; opts->type != OPTION_END; opts++) {
11101110
size_t pos;
11111111
int pad;
1112+
const char *cp, *np;
11121113

11131114
if (opts->type == OPTION_SUBCOMMAND)
11141115
continue;
@@ -1145,7 +1146,9 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
11451146
!(opts->flags & PARSE_OPT_NOARG))
11461147
pos += usage_argh(opts, outfile);
11471148

1148-
if (pos <= USAGE_OPTS_WIDTH)
1149+
if (pos == USAGE_OPTS_WIDTH + 1)
1150+
pad = -1;
1151+
else if (pos <= USAGE_OPTS_WIDTH)
11491152
pad = USAGE_OPTS_WIDTH - pos;
11501153
else {
11511154
fputc('\n', outfile);
@@ -1157,7 +1160,16 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
11571160
(const char *)opts->value);
11581161
continue;
11591162
}
1160-
fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
1163+
1164+
for (cp = _(opts->help); *cp; cp = np) {
1165+
np = strchrnul(cp, '\n');
1166+
fprintf(outfile,
1167+
"%*s%.*s\n", pad + USAGE_GAP, "",
1168+
(int)(np - cp), cp);
1169+
if (*np)
1170+
np++;
1171+
pad = USAGE_OPTS_WIDTH;
1172+
}
11611173
}
11621174
fputc('\n', outfile);
11631175

t/helper/test-parse-options.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ int cmd__parse_options(int argc, const char **argv)
133133
OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
134134
OPT_STRING('o', NULL, &string, "str", "get another string"),
135135
OPT_NOOP_NOARG(0, "obsolete"),
136+
OPT_SET_INT_F(0, "longhelp", &integer, "help text of this entry\n"
137+
"spans multiple lines", 0, PARSE_OPT_NONEG),
136138
OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
137139
OPT_GROUP("Magic arguments"),
138140
OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",

t/t0040-parse-options.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ usage: test-tool parse-options <options>
3030
-F, --file <file> set file to <file>
3131
3232
String options
33-
-s, --string <string>
34-
get a string
33+
-s, --string <string> get a string
3534
--string2 <str> get another string
3635
--st <st> get another string (pervert ordering)
3736
-o <str> get another string
37+
--longhelp help text of this entry
38+
spans multiple lines
3839
--list <str> add str to list
3940
4041
Magic arguments

0 commit comments

Comments
 (0)