parse-options: introduce OPT_HIDDEN_GROUP
Hidden options are not shown by `git <cmd> -h`, but are still shown by `git <cmd> --help-all`. If there are a lot of hidden options or if they don't belong to the same categories as other options, there is currently no way to properly group them. Using `OPT_GROUP("Foo")` means that "Foo" will always be shown which we don't want if that group contains only hidden options. To provide a way to have groups shown only when hidden options are shown, let's implement an OPT_HIDDEN_GROUP macro. To test this new macro, let's also improve `test-tool parse-options` and test its output with `--help-all`. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Christian Couder committed
Jul 16, 2026 at 18:55 UTC
e8529cc8da9437e6b7bf958192ca0984103b43c1
4 files changed
+35
-3
parse-options.c
+2
-2
@@ -1404,6 +1404,8 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
1404
1405
if (opts->type == OPTION_SUBCOMMAND)
1406
continue;
1407
+ if (!full && (opts->flags & PARSE_OPT_HIDDEN))
1408
+ continue;
1409
if (opts->type == OPTION_GROUP) {
1410
fputc('\n', outfile);
1411
need_newline = 0;
@@ -1411,8 +1413,6 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
1413
fprintf(outfile, "%s\n", _(opts->help));
1414
continue;
1415
}
1414
- if (!full && (opts->flags & PARSE_OPT_HIDDEN))
1415
- continue;
1416
1417
if (need_newline) {
1418
fputc('\n', outfile);
parse-options.h
+5
@@ -236,6 +236,11 @@ struct option {
236
.type = OPTION_GROUP, \
237
.help = (h), \
238
}
239
+#define OPT_HIDDEN_GROUP(h) { \
240
+ .type = OPTION_GROUP, \
241
+ .help = (h), \
242
+ .flags = PARSE_OPT_HIDDEN, \
243
+}
244
#define OPT_BIT(s, l, v, h, b) OPT_BIT_F(s, l, v, h, b, 0)
245
#define OPT_BITOP(s, l, v, h, set, clear) { \
246
.type = OPTION_BITOP, \
t/helper/test-parse-options.c
+4
@@ -209,6 +209,10 @@ int cmd__parse_options(int argc, const char **argv)
209
OPT_GROUP("Alias"),
210
OPT_STRING('A', "alias-source", &string, "string", "get a string"),
211
OPT_ALIAS('Z', "alias-target", "alias-source"),
212
+ OPT_HIDDEN_GROUP("Hidden options"),
213
+ OPT_HIDDEN_BOOL(0, "hidden-bool", &boolean, "get a boolean"),
214
+ OPT_INTEGER_F('k', "hidden-integer", &integer, "get a integer",
215
+ PARSE_OPT_HIDDEN),
216
OPT_END(),
217
};
218
int ret = 0;
t/t0040-parse-options.sh
+24
-1
@@ -7,7 +7,7 @@ test_description='our own option parser'
7
8
. ./test-lib.sh
9
10
-cat >expect <<\EOF
10
+cat >expect-part1 <<\EOF
11
usage: test-tool parse-options <options>
12
13
A helper function for the parse-options API.
@@ -41,6 +41,9 @@ String options
41
--[no-]string2 <str> get another string
42
--[no-]st <st> get another string (pervert ordering)
43
-o <str> get another string
44
+EOF
45
+
46
+cat >expect-part2 <<\EOF
47
--longhelp help text of this entry
48
spans multiple lines
49
--[no-]list <str> add str to list
@@ -67,12 +70,32 @@ Alias
70
71
EOF
72
73
+cat >expect-noop <<\EOF
74
+ --[no-]obsolete no-op (backward compatibility)
75
+EOF
76
+
77
+cat >expect-hidden <<\EOF
78
+Hidden options
79
+ --[no-]hidden-bool get a boolean
80
+ -k, --[no-]hidden-integer <n>
81
+ get a integer
82
+
83
+EOF
84
+
85
test_expect_success 'test help' '
86
+ cat expect-part1 expect-part2 >expect &&
87
test_must_fail test-tool parse-options -h >output 2>output.err &&
88
test_must_be_empty output.err &&
89
test_cmp expect output
90
'
91
92
+test_expect_success 'test --help-all shows hidden group and options' '
93
+ cat expect-part1 expect-noop expect-part2 expect-hidden >expect-help-all &&
94
+ test_must_fail test-tool parse-options --help-all >output 2>output.err &&
95
+ test_must_be_empty output.err &&
96
+ test_cmp expect-help-all output
97
+'
98
+
99
mv expect expect.err
100
101
check () {