]> git.ipfire.org Git - thirdparty/git.git/commitdiff
parse-options: PARSE_OPT_KEEP_UNKNOWN only applies to --options
authorSZEDER Gábor <szeder.dev@gmail.com>
Fri, 19 Aug 2022 16:03:57 +0000 (18:03 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 19 Aug 2022 18:13:14 +0000 (11:13 -0700)
The description of 'PARSE_OPT_KEEP_UNKNOWN' starts with "Keep unknown
arguments instead of erroring out".  This is a bit misleading, as this
flag only applies to unknown --options, while non-option arguments are
kept even without this flag.

Update the description to clarify this, and rename the flag to
PARSE_OPTIONS_KEEP_UNKNOWN_OPT to make this obvious just by looking at
the flag name.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
17 files changed:
Documentation/technical/api-parse-options.txt
builtin/archive.c
builtin/bisect--helper.c
builtin/difftool.c
builtin/env--helper.c
builtin/fast-export.c
builtin/log.c
builtin/reflog.c
builtin/revert.c
builtin/sparse-checkout.c
builtin/stash.c
diff.c
parse-options.c
parse-options.h
t/helper/test-parse-options.c
t/helper/test-serve-v2.c
t/t0040-parse-options.sh

index 5a04f3daecd5a29b3f08d93e16c9ef09c063ecfe..4412377fa3d2ef9883f5214c47352c93db9344fd 100644 (file)
@@ -90,8 +90,8 @@ Flags are the bitwise-or of:
        Keep the first argument, which contains the program name.  It's
        removed from argv[] by default.
 
-`PARSE_OPT_KEEP_UNKNOWN`::
-       Keep unknown arguments instead of erroring out.  This doesn't
+`PARSE_OPT_KEEP_UNKNOWN_OPT`::
+       Keep unknown options instead of erroring out.  This doesn't
        work for all combinations of arguments as users might expect
        it to do.  E.g. if the first argument in `--unknown --known`
        takes a value (which we can't know), the second one is
@@ -101,6 +101,8 @@ Flags are the bitwise-or of:
        non-option, not as a value belonging to the unknown option,
        the parser early.  That's why parse_options() errors out if
        both options are set.
+       Note that non-option arguments are always kept, even without
+       this flag.
 
 `PARSE_OPT_NO_INTERNAL_HELP`::
        By default, parse_options() handles `-h`, `--help` and
index 7176b041b6d85b5760c91f94fcdde551a38d147f..f094390ee01f810e7035f2efc2be75deedc3befa 100644 (file)
@@ -75,7 +75,7 @@ static int run_remote_archiver(int argc, const char **argv,
 
 #define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH |         \
                             PARSE_OPT_KEEP_ARGV0 |     \
-                            PARSE_OPT_KEEP_UNKNOWN |   \
+                            PARSE_OPT_KEEP_UNKNOWN_OPT |       \
                             PARSE_OPT_NO_INTERNAL_HELP )
 
 int cmd_archive(int argc, const char **argv, const char *prefix)
index 8a052c7111f97f02690a31e98ef4102961796800..7097750fc6b1221e9441fa65dcb545362d1e46ee 100644 (file)
@@ -1324,7 +1324,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 
        argc = parse_options(argc, argv, prefix, options,
                             git_bisect_helper_usage,
-                            PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_UNKNOWN);
+                            PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_UNKNOWN_OPT);
 
        if (!cmdmode)
                usage_with_options(git_bisect_helper_usage, options);
index b3c509b8de305db3ba5b393618024087e387dc46..8706f68492535dfaf7033a316802ff9de155c96f 100644 (file)
@@ -716,7 +716,7 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
        symlinks = has_symlinks;
 
        argc = parse_options(argc, argv, prefix, builtin_difftool_options,
-                            builtin_difftool_usage, PARSE_OPT_KEEP_UNKNOWN |
+                            builtin_difftool_usage, PARSE_OPT_KEEP_UNKNOWN_OPT |
                             PARSE_OPT_KEEP_DASHDASH);
 
        if (tool_help)
index 27349098b074f9c3118114ee16f3653f3d61baa2..ea04c166364fe5776cd196950c3ee44a866076d7 100644 (file)
@@ -50,7 +50,7 @@ int cmd_env__helper(int argc, const char **argv, const char *prefix)
        };
 
        argc = parse_options(argc, argv, prefix, opts, env__helper_usage,
-                            PARSE_OPT_KEEP_UNKNOWN);
+                            PARSE_OPT_KEEP_UNKNOWN_OPT);
        if (env_default && !*env_default)
                usage_with_options(env__helper_usage, opts);
        if (!cmdmode)
index e1748fb98be5b970a8d31bf43f3f708d6a60c3d9..bf3c20dea24125ae486753ab681d3c844d2586b9 100644 (file)
@@ -1221,7 +1221,7 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
        revs.sources = &revision_sources;
        revs.rewrite_parents = 1;
        argc = parse_options(argc, argv, prefix, options, fast_export_usage,
-                       PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN);
+                       PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT);
        argc = setup_revisions(argc, argv, &revs, NULL);
        if (argc > 1)
                usage_with_options (fast_export_usage, options);
index 88a5e98875adb0398b2855460bddd5fa43e073ec..fb84a0d399e83c06bc15eb4142bd1160bfca6de4 100644 (file)
@@ -199,7 +199,7 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
        mailmap = use_mailmap_config;
        argc = parse_options(argc, argv, prefix,
                             builtin_log_options, builtin_log_usage,
-                            PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
+                            PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT |
                             PARSE_OPT_KEEP_DASHDASH);
 
        if (quiet)
@@ -1926,7 +1926,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
         */
        argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
                             builtin_format_patch_usage,
-                            PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
+                            PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT |
                             PARSE_OPT_KEEP_DASHDASH);
 
        /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
index 4dd297dce86e5212087ee1202d5b9dd1f754f9d1..b8b1f4f8ea84d279a7fc102d0bf962f43681db4a 100644 (file)
@@ -223,7 +223,7 @@ static int cmd_reflog_show(int argc, const char **argv, const char *prefix)
 
        parse_options(argc, argv, prefix, options, reflog_show_usage,
                      PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
-                     PARSE_OPT_KEEP_UNKNOWN);
+                     PARSE_OPT_KEEP_UNKNOWN_OPT);
 
        return cmd_log_reflog(argc, argv, prefix);
 }
@@ -410,7 +410,7 @@ int cmd_reflog(int argc, const char **argv, const char *prefix)
 
        argc = parse_options(argc, argv, prefix, options, reflog_usage,
                             PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
-                            PARSE_OPT_KEEP_UNKNOWN |
+                            PARSE_OPT_KEEP_UNKNOWN_OPT |
                             PARSE_OPT_NO_INTERNAL_HELP);
 
        /*
index 2554f9099ccf11721cbd079ece7add901549f1f1..ee2a0807f011e817801f1dc45e858b99ced613a3 100644 (file)
@@ -141,7 +141,7 @@ static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
 
        argc = parse_options(argc, argv, NULL, options, usage_str,
                        PARSE_OPT_KEEP_ARGV0 |
-                       PARSE_OPT_KEEP_UNKNOWN);
+                       PARSE_OPT_KEEP_UNKNOWN_OPT);
 
        prepare_repo_settings(the_repository);
        the_repository->settings.command_requires_full_index = 0;
index f91e29b56a97eea0f8814a0bfcca45c512a3ab06..a5e4b95a9deb3383dd4358ffe4ca838f8bbc57e5 100644 (file)
@@ -767,7 +767,7 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
        argc = parse_options(argc, argv, prefix,
                             builtin_sparse_checkout_add_options,
                             builtin_sparse_checkout_add_usage,
-                            PARSE_OPT_KEEP_UNKNOWN);
+                            PARSE_OPT_KEEP_UNKNOWN_OPT);
 
        sanitize_paths(argc, argv, prefix, add_opts.skip_checks);
 
@@ -813,7 +813,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
        argc = parse_options(argc, argv, prefix,
                             builtin_sparse_checkout_set_options,
                             builtin_sparse_checkout_set_usage,
-                            PARSE_OPT_KEEP_UNKNOWN);
+                            PARSE_OPT_KEEP_UNKNOWN_OPT);
 
        if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index))
                return 1;
index 30fa1014605425b3731d5f65c16338a7164f5140..a14e832e9f41adf00817bf8ab9b359291b59c7f9 100644 (file)
@@ -782,7 +782,7 @@ static int list_stash(int argc, const char **argv, const char *prefix)
 
        argc = parse_options(argc, argv, prefix, options,
                             git_stash_list_usage,
-                            PARSE_OPT_KEEP_UNKNOWN);
+                            PARSE_OPT_KEEP_UNKNOWN_OPT);
 
        if (!ref_exists(ref_stash))
                return 0;
@@ -873,7 +873,7 @@ static int show_stash(int argc, const char **argv, const char *prefix)
        init_revisions(&rev, prefix);
 
        argc = parse_options(argc, argv, prefix, options, git_stash_show_usage,
-                            PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
+                            PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT |
                             PARSE_OPT_KEEP_DASHDASH);
 
        strvec_push(&revision_args, argv[0]);
@@ -979,7 +979,7 @@ static int store_stash(int argc, const char **argv, const char *prefix)
 
        argc = parse_options(argc, argv, prefix, options,
                             git_stash_store_usage,
-                            PARSE_OPT_KEEP_UNKNOWN);
+                            PARSE_OPT_KEEP_UNKNOWN_OPT);
 
        if (argc != 1) {
                if (!quiet)
@@ -1795,7 +1795,7 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
        git_config(git_stash_config, NULL);
 
        argc = parse_options(argc, argv, prefix, options, git_stash_usage,
-                            PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
+                            PARSE_OPT_KEEP_UNKNOWN_OPT | PARSE_OPT_KEEP_DASHDASH);
 
        prepare_repo_settings(the_repository);
        the_repository->settings.command_requires_full_index = 0;
diff --git a/diff.c b/diff.c
index 974626a6211514b25af43e8f48933c6929826aa5..dd68281ba4442cb9e2fa3d13305323c55a245ed9 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -5661,7 +5661,7 @@ int diff_opt_parse(struct diff_options *options,
 
        ac = parse_options(ac, av, prefix, options->parseopts, NULL,
                           PARSE_OPT_KEEP_DASHDASH |
-                          PARSE_OPT_KEEP_UNKNOWN |
+                          PARSE_OPT_KEEP_UNKNOWN_OPT |
                           PARSE_OPT_NO_INTERNAL_HELP |
                           PARSE_OPT_ONE_SHOT |
                           PARSE_OPT_STOP_AT_NON_OPTION);
index edf55d3ef5d7428b68e793d6d684c0299710c628..a0a2cf98fa29a03995090250d232003944a6c653 100644 (file)
@@ -332,7 +332,7 @@ again:
                        rest = NULL;
                if (!rest) {
                        /* abbreviated? */
-                       if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
+                       if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN_OPT) &&
                            !strncmp(long_name, arg, arg_end - arg)) {
 is_abbreviated:
                                if (abbrev_option &&
@@ -515,7 +515,7 @@ static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
        ctx->prefix = prefix;
        ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
        ctx->flags = flags;
-       if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
+       if ((flags & PARSE_OPT_KEEP_UNKNOWN_OPT) &&
            (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
            !(flags & PARSE_OPT_ONE_SHOT))
                BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
@@ -839,7 +839,7 @@ enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
 unknown:
                if (ctx->flags & PARSE_OPT_ONE_SHOT)
                        break;
-               if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
+               if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT))
                        return PARSE_OPT_UNKNOWN;
                ctx->out[ctx->cpidx++] = ctx->argv[0];
                ctx->opt = NULL;
index 685fccac137fc23884f0e816d53b7be549506441..591df641916b82511689ab0cc746855450a803e5 100644 (file)
@@ -30,7 +30,7 @@ enum parse_opt_flags {
        PARSE_OPT_KEEP_DASHDASH = 1 << 0,
        PARSE_OPT_STOP_AT_NON_OPTION = 1 << 1,
        PARSE_OPT_KEEP_ARGV0 = 1 << 2,
-       PARSE_OPT_KEEP_UNKNOWN = 1 << 3,
+       PARSE_OPT_KEEP_UNKNOWN_OPT = 1 << 3,
        PARSE_OPT_NO_INTERNAL_HELP = 1 << 4,
        PARSE_OPT_ONE_SHOT = 1 << 5,
        PARSE_OPT_SHELL_EVAL = 1 << 6,
index 88919785d37109a54e3153e0fd93ee98bceaf019..99ad6fa4f0b9fa91a5b1f6bcc900a9d0d45c60d4 100644 (file)
@@ -232,9 +232,9 @@ static const struct option test_flag_options[] = {
        OPT_BIT(0, "keep-argv0", &test_flags,
                "pass PARSE_OPT_KEEP_ARGV0 to parse_options()",
                PARSE_OPT_KEEP_ARGV0),
-       OPT_BIT(0, "keep-unknown", &test_flags,
-               "pass PARSE_OPT_KEEP_UNKNOWN to parse_options()",
-               PARSE_OPT_KEEP_UNKNOWN),
+       OPT_BIT(0, "keep-unknown-opt", &test_flags,
+               "pass PARSE_OPT_KEEP_UNKNOWN_OPT to parse_options()",
+               PARSE_OPT_KEEP_UNKNOWN_OPT),
        OPT_BIT(0, "no-internal-help", &test_flags,
                "pass PARSE_OPT_NO_INTERNAL_HELP to parse_options()",
                PARSE_OPT_NO_INTERNAL_HELP),
index 28e905afc36afdc3202ec68ac915b1ddc6a08781..824e5c0a95819f8d11393a74da4b231728bb0615 100644 (file)
@@ -24,7 +24,7 @@ int cmd__serve_v2(int argc, const char **argv)
        /* ignore all unknown cmdline switches for now */
        argc = parse_options(argc, argv, prefix, options, serve_usage,
                             PARSE_OPT_KEEP_DASHDASH |
-                            PARSE_OPT_KEEP_UNKNOWN);
+                            PARSE_OPT_KEEP_UNKNOWN_OPT);
 
        if (advertise_capabilities)
                protocol_v2_advertise_capabilities();
index 8511ce24bb476827d5b85fdd903336eefadeb788..264b7373098c102924a6a295f882a739421f80f2 100755 (executable)
@@ -488,8 +488,8 @@ test_expect_success 'STOP_AT_NON_OPTION works' '
        test_cmp expect actual
 '
 
-test_expect_success 'KEEP_UNKNOWN works' '
-       test-tool parse-options-flags --keep-unknown cmd --unknown=1 --opt=6 -u2 >actual &&
+test_expect_success 'KEEP_UNKNOWN_OPT works' '
+       test-tool parse-options-flags --keep-unknown-opt cmd --unknown=1 --opt=6 -u2 >actual &&
        cat >expect <<-\EOF &&
        opt: 6
        arg 00: --unknown=1
@@ -515,8 +515,8 @@ do
        "
 done
 
-test_expect_success 'KEEP_UNKNOWN | NO_INTERNAL_HELP works' '
-       test-tool parse-options-flags --keep-unknown --no-internal-help cmd -h --help --help-all >actual &&
+test_expect_success 'KEEP_UNKNOWN_OPT | NO_INTERNAL_HELP works' '
+       test-tool parse-options-flags --keep-unknown-opt --no-internal-help cmd -h --help --help-all >actual &&
        cat >expect <<-\EOF &&
        opt: 0
        arg 00: -h