Skip to content

Commit 448abbb

Browse files
committed
short help: allow multi-line opthelp
When "-h" triggers the short-help in a command that implements its option parsing using the parse-options API, the option help text is shown with a single fprintf() as a long line. When the text is multi-line, the second and subsequent lines are not left padded, that breaks the alignment across options. Borrowing the idea from the advice API where its hint strings are shown with (localized) "hint:" prefix, let's internally split the (localized) help text into lines, and showing the first line, pad the remaining lines to align. Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb7d80e commit 448abbb

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

parse-options.c

Lines changed: 11 additions & 1 deletion
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;
@@ -1157,7 +1158,16 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
11571158
(const char *)opts->value);
11581159
continue;
11591160
}
1160-
fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
1161+
1162+
for (cp = _(opts->help); *cp; cp = np) {
1163+
np = strchrnul(cp, '\n');
1164+
fprintf(outfile,
1165+
"%*s%.*s\n", pad + USAGE_GAP, "",
1166+
(int)(np - cp), cp);
1167+
if (*np)
1168+
np++;
1169+
pad = USAGE_OPTS_WIDTH;
1170+
}
11611171
}
11621172
fputc('\n', outfile);
11631173

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ String options
3535
--string2 <str> get another string
3636
--st <st> get another string (pervert ordering)
3737
-o <str> get another string
38+
--longhelp help text of this entry
39+
spans multiple lines
3840
--list <str> add str to list
3941
4042
Magic arguments

0 commit comments

Comments
 (0)