]> git.ipfire.org Git - thirdparty/git.git/commitdiff
parse-options: introduce OPT_HIDDEN_GROUP
authorChristian Couder <christian.couder@gmail.com>
Thu, 16 Jul 2026 16:55:11 +0000 (18:55 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 16 Jul 2026 21:11:12 +0000 (14:11 -0700)
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>
parse-options.c
parse-options.h
t/helper/test-parse-options.c
t/t0040-parse-options.sh

index f4647e0099ea99a21146010f03e2ad72cf9915e5..640e600de8c7580124ec3fcbe339d7d14e09c041 100644 (file)
@@ -1404,6 +1404,8 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
 
                if (opts->type == OPTION_SUBCOMMAND)
                        continue;
+               if (!full && (opts->flags & PARSE_OPT_HIDDEN))
+                       continue;
                if (opts->type == OPTION_GROUP) {
                        fputc('\n', outfile);
                        need_newline = 0;
@@ -1411,8 +1413,6 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
                                fprintf(outfile, "%s\n", _(opts->help));
                        continue;
                }
-               if (!full && (opts->flags & PARSE_OPT_HIDDEN))
-                       continue;
 
                if (need_newline) {
                        fputc('\n', outfile);
index 0d1f738f8d8671e5eb8da731178a1b0d8071c70e..a28b3cd9426e7affe3fc7c702f35d94354a645d0 100644 (file)
@@ -236,6 +236,11 @@ struct option {
        .type = OPTION_GROUP, \
        .help = (h), \
 }
+#define OPT_HIDDEN_GROUP(h) { \
+       .type = OPTION_GROUP, \
+       .help = (h), \
+       .flags = PARSE_OPT_HIDDEN, \
+}
 #define OPT_BIT(s, l, v, h, b)      OPT_BIT_F(s, l, v, h, b, 0)
 #define OPT_BITOP(s, l, v, h, set, clear) { \
        .type = OPTION_BITOP, \
index 68579d83f3939e8f17ed0822e16d61c3c9d8253b..f181f0c02d355a9dd55abd482b4ed1caecfecebe 100644 (file)
@@ -209,6 +209,10 @@ int cmd__parse_options(int argc, const char **argv)
                OPT_GROUP("Alias"),
                OPT_STRING('A', "alias-source", &string, "string", "get a string"),
                OPT_ALIAS('Z', "alias-target", "alias-source"),
+               OPT_HIDDEN_GROUP("Hidden options"),
+               OPT_HIDDEN_BOOL(0, "hidden-bool", &boolean, "get a boolean"),
+               OPT_INTEGER_F('k', "hidden-integer", &integer, "get a integer",
+                             PARSE_OPT_HIDDEN),
                OPT_END(),
        };
        int ret = 0;
index ca55ea8228c3789c5089d9141412224589ac18f7..4040333185b22283fcc7146c750c5995fe8b9bad 100755 (executable)
@@ -7,7 +7,7 @@ test_description='our own option parser'
 
 . ./test-lib.sh
 
-cat >expect <<\EOF
+cat >expect-part1 <<\EOF
 usage: test-tool parse-options <options>
 
     A helper function for the parse-options API.
@@ -41,6 +41,9 @@ String options
     --[no-]string2 <str>  get another string
     --[no-]st <st>        get another string (pervert ordering)
     -o <str>              get another string
+EOF
+
+cat >expect-part2 <<\EOF
     --longhelp            help text of this entry
                           spans multiple lines
     --[no-]list <str>     add str to list
@@ -67,12 +70,32 @@ Alias
 
 EOF
 
+cat >expect-noop <<\EOF
+    --[no-]obsolete       no-op (backward compatibility)
+EOF
+
+cat >expect-hidden <<\EOF
+Hidden options
+    --[no-]hidden-bool    get a boolean
+    -k, --[no-]hidden-integer <n>
+                          get a integer
+
+EOF
+
 test_expect_success 'test help' '
+       cat expect-part1 expect-part2 >expect &&
        test_must_fail test-tool parse-options -h >output 2>output.err &&
        test_must_be_empty output.err &&
        test_cmp expect output
 '
 
+test_expect_success 'test --help-all shows hidden group and options' '
+       cat expect-part1 expect-noop expect-part2 expect-hidden >expect-help-all &&
+       test_must_fail test-tool parse-options --help-all >output 2>output.err &&
+       test_must_be_empty output.err &&
+       test_cmp expect-help-all output
+'
+
 mv expect expect.err
 
 check () {