parse-options: PARSE_OPT_KEEP_UNKNOWN only applies to --options

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>

SZEDER Gábor committed Aug 19, 2022 at 18:03 UTC 99d86d60e59e11cbc46766346e3e379164a6e4df
17 files changed +33 -31
Documentation/technical/api-parse-options.txt
+4 -2
@@ -90,8 +90,8 @@ Flags are the bitwise-or of:
90 Keep the first argument, which contains the program name. It's
91 removed from argv[] by default.
92
93 -`PARSE_OPT_KEEP_UNKNOWN`::
94 - Keep unknown arguments instead of erroring out. This doesn't
93 +`PARSE_OPT_KEEP_UNKNOWN_OPT`::
94 + Keep unknown options instead of erroring out. This doesn't
95 work for all combinations of arguments as users might expect
96 it to do. E.g. if the first argument in `--unknown --known`
97 takes a value (which we can't know), the second one is
@@ -101,6 +101,8 @@ Flags are the bitwise-or of:
101 non-option, not as a value belonging to the unknown option,
102 the parser early. That's why parse_options() errors out if
103 both options are set.
104 + Note that non-option arguments are always kept, even without
105 + this flag.
106
107 `PARSE_OPT_NO_INTERNAL_HELP`::
108 By default, parse_options() handles `-h`, `--help` and
builtin/archive.c
+1 -1
@@ -75,7 +75,7 @@ static int run_remote_archiver(int argc, const char **argv,
75
76 #define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | \
77 PARSE_OPT_KEEP_ARGV0 | \
78 - PARSE_OPT_KEEP_UNKNOWN | \
78 + PARSE_OPT_KEEP_UNKNOWN_OPT | \
79 PARSE_OPT_NO_INTERNAL_HELP )
80
81 int cmd_archive(int argc, const char **argv, const char *prefix)
builtin/bisect--helper.c
+1 -1
@@ -1324,7 +1324,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
1324
1325 argc = parse_options(argc, argv, prefix, options,
1326 git_bisect_helper_usage,
1327 - PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_UNKNOWN);
1327 + PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_UNKNOWN_OPT);
1328
1329 if (!cmdmode)
1330 usage_with_options(git_bisect_helper_usage, options);
builtin/difftool.c
+1 -1
@@ -716,7 +716,7 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
716 symlinks = has_symlinks;
717
718 argc = parse_options(argc, argv, prefix, builtin_difftool_options,
719 - builtin_difftool_usage, PARSE_OPT_KEEP_UNKNOWN |
719 + builtin_difftool_usage, PARSE_OPT_KEEP_UNKNOWN_OPT |
720 PARSE_OPT_KEEP_DASHDASH);
721
722 if (tool_help)
builtin/env--helper.c
+1 -1
@@ -50,7 +50,7 @@ int cmd_env__helper(int argc, const char **argv, const char *prefix)
50 };
51
52 argc = parse_options(argc, argv, prefix, opts, env__helper_usage,
53 - PARSE_OPT_KEEP_UNKNOWN);
53 + PARSE_OPT_KEEP_UNKNOWN_OPT);
54 if (env_default && !*env_default)
55 usage_with_options(env__helper_usage, opts);
56 if (!cmdmode)
builtin/fast-export.c
+1 -1
@@ -1221,7 +1221,7 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
1221 revs.sources = &revision_sources;
1222 revs.rewrite_parents = 1;
1223 argc = parse_options(argc, argv, prefix, options, fast_export_usage,
1224 - PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN);
1224 + PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT);
1225 argc = setup_revisions(argc, argv, &revs, NULL);
1226 if (argc > 1)
1227 usage_with_options (fast_export_usage, options);
builtin/log.c
+2 -2
@@ -199,7 +199,7 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
199 mailmap = use_mailmap_config;
200 argc = parse_options(argc, argv, prefix,
201 builtin_log_options, builtin_log_usage,
202 - PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
202 + PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT |
203 PARSE_OPT_KEEP_DASHDASH);
204
205 if (quiet)
@@ -1926,7 +1926,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1926 */
1927 argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
1928 builtin_format_patch_usage,
1929 - PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
1929 + PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT |
1930 PARSE_OPT_KEEP_DASHDASH);
1931
1932 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
builtin/reflog.c
+2 -2
@@ -223,7 +223,7 @@ static int cmd_reflog_show(int argc, const char **argv, const char *prefix)
223
224 parse_options(argc, argv, prefix, options, reflog_show_usage,
225 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
226 - PARSE_OPT_KEEP_UNKNOWN);
226 + PARSE_OPT_KEEP_UNKNOWN_OPT);
227
228 return cmd_log_reflog(argc, argv, prefix);
229 }
@@ -410,7 +410,7 @@ int cmd_reflog(int argc, const char **argv, const char *prefix)
410
411 argc = parse_options(argc, argv, prefix, options, reflog_usage,
412 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
413 - PARSE_OPT_KEEP_UNKNOWN |
413 + PARSE_OPT_KEEP_UNKNOWN_OPT |
414 PARSE_OPT_NO_INTERNAL_HELP);
415
416 /*
builtin/revert.c
+1 -1
@@ -141,7 +141,7 @@ static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
141
142 argc = parse_options(argc, argv, NULL, options, usage_str,
143 PARSE_OPT_KEEP_ARGV0 |
144 - PARSE_OPT_KEEP_UNKNOWN);
144 + PARSE_OPT_KEEP_UNKNOWN_OPT);
145
146 prepare_repo_settings(the_repository);
147 the_repository->settings.command_requires_full_index = 0;
builtin/sparse-checkout.c
+2 -2
@@ -767,7 +767,7 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
767 argc = parse_options(argc, argv, prefix,
768 builtin_sparse_checkout_add_options,
769 builtin_sparse_checkout_add_usage,
770 - PARSE_OPT_KEEP_UNKNOWN);
770 + PARSE_OPT_KEEP_UNKNOWN_OPT);
771
772 sanitize_paths(argc, argv, prefix, add_opts.skip_checks);
773
@@ -813,7 +813,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
813 argc = parse_options(argc, argv, prefix,
814 builtin_sparse_checkout_set_options,
815 builtin_sparse_checkout_set_usage,
816 - PARSE_OPT_KEEP_UNKNOWN);
816 + PARSE_OPT_KEEP_UNKNOWN_OPT);
817
818 if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index))
819 return 1;
builtin/stash.c
+4 -4
@@ -782,7 +782,7 @@ static int list_stash(int argc, const char **argv, const char *prefix)
782
783 argc = parse_options(argc, argv, prefix, options,
784 git_stash_list_usage,
785 - PARSE_OPT_KEEP_UNKNOWN);
785 + PARSE_OPT_KEEP_UNKNOWN_OPT);
786
787 if (!ref_exists(ref_stash))
788 return 0;
@@ -873,7 +873,7 @@ static int show_stash(int argc, const char **argv, const char *prefix)
873 init_revisions(&rev, prefix);
874
875 argc = parse_options(argc, argv, prefix, options, git_stash_show_usage,
876 - PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
876 + PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT |
877 PARSE_OPT_KEEP_DASHDASH);
878
879 strvec_push(&revision_args, argv[0]);
@@ -979,7 +979,7 @@ static int store_stash(int argc, const char **argv, const char *prefix)
979
980 argc = parse_options(argc, argv, prefix, options,
981 git_stash_store_usage,
982 - PARSE_OPT_KEEP_UNKNOWN);
982 + PARSE_OPT_KEEP_UNKNOWN_OPT);
983
984 if (argc != 1) {
985 if (!quiet)
@@ -1795,7 +1795,7 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
1795 git_config(git_stash_config, NULL);
1796
1797 argc = parse_options(argc, argv, prefix, options, git_stash_usage,
1798 - PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
1798 + PARSE_OPT_KEEP_UNKNOWN_OPT | PARSE_OPT_KEEP_DASHDASH);
1799
1800 prepare_repo_settings(the_repository);
1801 the_repository->settings.command_requires_full_index = 0;
diff.c
+1 -1
@@ -5661,7 +5661,7 @@ int diff_opt_parse(struct diff_options *options,
5661
5662 ac = parse_options(ac, av, prefix, options->parseopts, NULL,
5663 PARSE_OPT_KEEP_DASHDASH |
5664 - PARSE_OPT_KEEP_UNKNOWN |
5664 + PARSE_OPT_KEEP_UNKNOWN_OPT |
5665 PARSE_OPT_NO_INTERNAL_HELP |
5666 PARSE_OPT_ONE_SHOT |
5667 PARSE_OPT_STOP_AT_NON_OPTION);
parse-options.c
+3 -3
@@ -332,7 +332,7 @@ again:
332 rest = NULL;
333 if (!rest) {
334 /* abbreviated? */
335 - if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
335 + if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN_OPT) &&
336 !strncmp(long_name, arg, arg_end - arg)) {
337 is_abbreviated:
338 if (abbrev_option &&
@@ -515,7 +515,7 @@ static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
515 ctx->prefix = prefix;
516 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
517 ctx->flags = flags;
518 - if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
518 + if ((flags & PARSE_OPT_KEEP_UNKNOWN_OPT) &&
519 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
520 !(flags & PARSE_OPT_ONE_SHOT))
521 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,
839 unknown:
840 if (ctx->flags & PARSE_OPT_ONE_SHOT)
841 break;
842 - if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
842 + if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT))
843 return PARSE_OPT_UNKNOWN;
844 ctx->out[ctx->cpidx++] = ctx->argv[0];
845 ctx->opt = NULL;
parse-options.h
+1 -1
@@ -30,7 +30,7 @@ enum parse_opt_flags {
30 PARSE_OPT_KEEP_DASHDASH = 1 << 0,
31 PARSE_OPT_STOP_AT_NON_OPTION = 1 << 1,
32 PARSE_OPT_KEEP_ARGV0 = 1 << 2,
33 - PARSE_OPT_KEEP_UNKNOWN = 1 << 3,
33 + PARSE_OPT_KEEP_UNKNOWN_OPT = 1 << 3,
34 PARSE_OPT_NO_INTERNAL_HELP = 1 << 4,
35 PARSE_OPT_ONE_SHOT = 1 << 5,
36 PARSE_OPT_SHELL_EVAL = 1 << 6,
t/helper/test-parse-options.c
+3 -3
@@ -232,9 +232,9 @@ static const struct option test_flag_options[] = {
232 OPT_BIT(0, "keep-argv0", &test_flags,
233 "pass PARSE_OPT_KEEP_ARGV0 to parse_options()",
234 PARSE_OPT_KEEP_ARGV0),
235 - OPT_BIT(0, "keep-unknown", &test_flags,
236 - "pass PARSE_OPT_KEEP_UNKNOWN to parse_options()",
237 - PARSE_OPT_KEEP_UNKNOWN),
235 + OPT_BIT(0, "keep-unknown-opt", &test_flags,
236 + "pass PARSE_OPT_KEEP_UNKNOWN_OPT to parse_options()",
237 + PARSE_OPT_KEEP_UNKNOWN_OPT),
238 OPT_BIT(0, "no-internal-help", &test_flags,
239 "pass PARSE_OPT_NO_INTERNAL_HELP to parse_options()",
240 PARSE_OPT_NO_INTERNAL_HELP),
t/helper/test-serve-v2.c
+1 -1
@@ -24,7 +24,7 @@ int cmd__serve_v2(int argc, const char **argv)
24 /* ignore all unknown cmdline switches for now */
25 argc = parse_options(argc, argv, prefix, options, serve_usage,
26 PARSE_OPT_KEEP_DASHDASH |
27 - PARSE_OPT_KEEP_UNKNOWN);
27 + PARSE_OPT_KEEP_UNKNOWN_OPT);
28
29 if (advertise_capabilities)
30 protocol_v2_advertise_capabilities();
t/t0040-parse-options.sh
+4 -4
@@ -488,8 +488,8 @@ test_expect_success 'STOP_AT_NON_OPTION works' '
488 test_cmp expect actual
489 '
490
491 -test_expect_success 'KEEP_UNKNOWN works' '
492 - test-tool parse-options-flags --keep-unknown cmd --unknown=1 --opt=6 -u2 >actual &&
491 +test_expect_success 'KEEP_UNKNOWN_OPT works' '
492 + test-tool parse-options-flags --keep-unknown-opt cmd --unknown=1 --opt=6 -u2 >actual &&
493 cat >expect <<-\EOF &&
494 opt: 6
495 arg 00: --unknown=1
@@ -515,8 +515,8 @@ do
515 "
516 done
517
518 -test_expect_success 'KEEP_UNKNOWN | NO_INTERNAL_HELP works' '
519 - test-tool parse-options-flags --keep-unknown --no-internal-help cmd -h --help --help-all >actual &&
518 +test_expect_success 'KEEP_UNKNOWN_OPT | NO_INTERNAL_HELP works' '
519 + test-tool parse-options-flags --keep-unknown-opt --no-internal-help cmd -h --help --help-all >actual &&
520 cat >expect <<-\EOF &&
521 opt: 0
522 arg 00: -h