Skip to content

Commit e8e5d29

Browse files
rscharfegitster
authored andcommitted
parse-options: show negatability of options in short help
Add a "[no-]" prefix to options without the flag PARSE_OPT_NONEG to document the fact that you can negate them. This looks a bit strange for options that already start with "no-", e.g. for the option --no-name of git show-branch: --[no-]no-name suppress naming strings You can actually use --no-no-name as an alias of --name, so the short help is not wrong. If we strip off any of the "no-"s, we lose either the ability to see if the remaining one belongs to the documented variant or to see if it can be negated. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d5dc68f commit e8e5d29

File tree

7 files changed

+58
-45
lines changed

7 files changed

+58
-45
lines changed

Documentation/git-rev-parse.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,10 @@ usage: some-command [<options>] <args>...
424424
some-command does foo and bar!
425425

426426
-h, --help show the help
427-
--foo some nifty option --foo
428-
--bar ... some cool option --bar with an argument
429-
--baz <arg> another cool option --baz with a named argument
430-
--qux[=<path>] qux may take a path argument but has meaning by itself
427+
--[no-]foo some nifty option --foo
428+
--[no-]bar ... some cool option --bar with an argument
429+
--[no-]baz <arg> another cool option --baz with a named argument
430+
--[no-]qux[=<path>] qux may take a path argument but has meaning by itself
431431

432432
An option group Header
433433
-C[...] option C with an optional argument

contrib/subtree/t/t7900-subtree.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ test_expect_success 'shows short help text for -h' '
7171
test_expect_code 129 git subtree -h >out 2>err &&
7272
test_must_be_empty err &&
7373
grep -e "^ *or: git subtree pull" out &&
74-
grep -e --annotate out
74+
grep -F -e "--[no-]annotate" out
7575
'
7676

7777
#

parse-options.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,8 +1137,14 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
11371137
}
11381138
if (opts->long_name && opts->short_name)
11391139
pos += fprintf(outfile, ", ");
1140-
if (opts->long_name)
1141-
pos += fprintf(outfile, "--%s", opts->long_name);
1140+
if (opts->long_name) {
1141+
const char *long_name = opts->long_name;
1142+
if (opts->flags & PARSE_OPT_NONEG)
1143+
pos += fprintf(outfile, "--%s", long_name);
1144+
else
1145+
pos += fprintf(outfile, "--[no-]%s", long_name);
1146+
}
1147+
11421148
if (opts->type == OPTION_NUMBER)
11431149
pos += utf8_fprintf(outfile, _("-NUM"));
11441150

t/t0040-parse-options.sh

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,34 @@ usage: test-tool parse-options <options>
1313
1414
A helper function for the parse-options API.
1515
16-
--yes get a boolean
17-
-D, --no-doubt begins with 'no-'
16+
--[no-]yes get a boolean
17+
-D, --[no-]no-doubt begins with 'no-'
1818
-B, --no-fear be brave
19-
-b, --boolean increment by one
20-
-4, --or4 bitwise-or boolean with ...0100
21-
--neg-or4 same as --no-or4
19+
-b, --[no-]boolean increment by one
20+
-4, --[no-]or4 bitwise-or boolean with ...0100
21+
--[no-]neg-or4 same as --no-or4
2222
23-
-i, --integer <n> get a integer
23+
-i, --[no-]integer <n>
24+
get a integer
2425
-j <n> get a integer, too
2526
-m, --magnitude <n> get a magnitude
26-
--set23 set integer to 23
27+
--[no-]set23 set integer to 23
2728
--mode1 set integer to 1 (cmdmode option)
2829
--mode2 set integer to 2 (cmdmode option)
29-
-L, --length <str> get length of <str>
30-
-F, --file <file> set file to <file>
30+
-L, --[no-]length <str>
31+
get length of <str>
32+
-F, --[no-]file <file>
33+
set file to <file>
3134
3235
String options
33-
-s, --string <string> get a string
34-
--string2 <str> get another string
35-
--st <st> get another string (pervert ordering)
36+
-s, --[no-]string <string>
37+
get a string
38+
--[no-]string2 <str> get another string
39+
--[no-]st <st> get another string (pervert ordering)
3640
-o <str> get another string
3741
--longhelp help text of this entry
3842
spans multiple lines
39-
--list <str> add str to list
43+
--[no-]list <str> add str to list
4044
4145
Magic arguments
4246
-NUM set integer to NUM
@@ -45,16 +49,17 @@ Magic arguments
4549
--no-ambiguous negative ambiguity
4650
4751
Standard options
48-
--abbrev[=<n>] use <n> digits to display object names
49-
-v, --verbose be verbose
50-
-n, --dry-run dry run
51-
-q, --quiet be quiet
52-
--expect <string> expected output in the variable dump
52+
--[no-]abbrev[=<n>] use <n> digits to display object names
53+
-v, --[no-]verbose be verbose
54+
-n, --[no-]dry-run dry run
55+
-q, --[no-]quiet be quiet
56+
--[no-]expect <string>
57+
expected output in the variable dump
5358
5459
Alias
55-
-A, --alias-source <string>
60+
-A, --[no-]alias-source <string>
5661
get a string
57-
-Z, --alias-target <string>
62+
-Z, --[no-]alias-target <string>
5863
alias of --alias-source
5964
6065
EOF

t/t1502-rev-parse-parseopt.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ test_expect_success 'test --parseopt help-all output hidden switches' '
111111
|
112112
| some-command does foo and bar!
113113
|
114-
| --hidden1 A hidden switch
114+
| --[no-]hidden1 A hidden switch
115115
|
116116
|EOF
117117
END_EXPECT

t/t1502/optionspec-neg.help

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ usage: some-command [options] <args>...
33

44
some-command does foo and bar!
55

6-
--foo can be negated
7-
--no-bar can be positivated
6+
--[no-]foo can be negated
7+
--[no-]no-bar can be positivated
88
--positive-only cannot be negated
99
--no-negative cannot be positivated
1010

t/t1502/optionspec.help

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,33 @@ usage: some-command [options] <args>...
44
some-command does foo and bar!
55

66
-h, --help show the help
7-
--foo some nifty option --foo
8-
--bar ... some cool option --bar with an argument
9-
-b, --baz a short and long option
7+
--[no-]foo some nifty option --foo
8+
--[no-]bar ... some cool option --bar with an argument
9+
-b, --[no-]baz a short and long option
1010

1111
An option group Header
1212
-C[...] option C with an optional argument
13-
-d, --data[=...] short and long option with an optional argument
13+
-d, --[no-]data[=...] short and long option with an optional argument
1414

1515
Argument hints
1616
-B <arg> short option required argument
17-
--bar2 <arg> long option required argument
18-
-e, --fuz <with-space>
17+
--[no-]bar2 <arg> long option required argument
18+
-e, --[no-]fuz <with-space>
1919
short and long option required argument
2020
-s[<some>] short option optional argument
21-
--long[=<data>] long option optional argument
22-
-g, --fluf[=<path>] short and long option optional argument
23-
--longest <very-long-argument-hint>
21+
--[no-]long[=<data>] long option optional argument
22+
-g, --[no-]fluf[=<path>]
23+
short and long option optional argument
24+
--[no-]longest <very-long-argument-hint>
2425
a very long argument hint
25-
--pair <key=value> with an equals sign in the hint
26-
--aswitch help te=t contains? fl*g characters!`
27-
--bswitch <hint> hint has trailing tab character
28-
--cswitch switch has trailing tab character
29-
--short-hint <a> with a one symbol hint
26+
--[no-]pair <key=value>
27+
with an equals sign in the hint
28+
--[no-]aswitch help te=t contains? fl*g characters!`
29+
--[no-]bswitch <hint> hint has trailing tab character
30+
--[no-]cswitch switch has trailing tab character
31+
--[no-]short-hint <a> with a one symbol hint
3032

3133
Extras
32-
--extra1 line above used to cause a segfault but no longer does
34+
--[no-]extra1 line above used to cause a segfault but no longer does
3335

3436
EOF

0 commit comments

Comments
 (0)