Skip to content

Commit c512643

Browse files
committed
short help: allow a gap smaller than USAGE_GAP
The parse-options API responds to "git cmd -h" by listing the option flag (padded to the USAGE_OPTS_WIDTH column), followed by USAGE_GAP (set to 2) whitespaces, followed by the help text. If the flags part does not fit within the USAGE_OPTS_WIDTH, the help text is given on its own line. Imagine that "@" below depicts the USAGE_OPTS_WIDTH'th column, and "#" are for the usage help text, the output may look like this: @@@@@@@@@@@@@ ######################################## -f description of the flag '-f' comes here --short=<num> description of the flag '--short' --very-long-option=<number> description of the flag '--very-long-option' This is all good and nice in principle, but it becomes awkward when the flags part is just one column over the limit and forces a line break. See the description of the "--almost" option below: @@@@@@@@@@@@@ ######################################## -f description of the flag '-f' comes here --short=<num> description of the flag '--short' --almost=<num> description of the flag '--almost' --very-long-option=<number> description of the flag '--very-long-option' If we allow shrinking the gap to a single whitespace only in such a case, we would instead get: @@@@@@@@@@@@@ ######################################## -f description of the flag '-f' comes here --short=<num> description of the flag '--short' --almost=<num> description of the flag '--almost' --very-long-option=<number> description of the flag '--very-long-option' and the boundary between the flags and their descriptions does not become any harder to see, while saving precious vertical screen real estate. Signed-off-by: Junio C Hamano <[email protected]>
1 parent d86a8f3 commit c512643

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

parse-options.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,9 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
11461146
!(opts->flags & PARSE_OPT_NOARG))
11471147
pos += usage_argh(opts, outfile);
11481148

1149-
if (pos <= USAGE_OPTS_WIDTH)
1149+
if (pos == USAGE_OPTS_WIDTH + 1)
1150+
pad = -1;
1151+
else if (pos <= USAGE_OPTS_WIDTH)
11501152
pad = USAGE_OPTS_WIDTH - pos;
11511153
else {
11521154
fputc('\n', outfile);

t/t0040-parse-options.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ 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

0 commit comments

Comments
 (0)