]> git.ipfire.org Git - thirdparty/git.git/commitdiff
t0040-parse-options: test parse_options() with various 'parse_opt_flags'
authorSZEDER Gábor <szeder.dev@gmail.com>
Fri, 19 Aug 2022 16:03:55 +0000 (18:03 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 19 Aug 2022 18:13:14 +0000 (11:13 -0700)
In 't0040-parse-options.sh' we thoroughly test the parsing of all
types and forms of options, but in all those tests parse_options() is
always invoked with a 0 flags parameter.

Add a few tests to demonstrate how various 'enum parse_opt_flags'
values are supposed to influence option parsing.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/helper/test-parse-options.c
t/helper/test-tool.c
t/helper/test-tool.h
t/t0040-parse-options.sh

index 48d3cf6692da0612eff2f8c61051780ef944def6..88919785d37109a54e3153e0fd93ee98bceaf019 100644 (file)
@@ -192,3 +192,69 @@ int cmd__parse_options(int argc, const char **argv)
 
        return ret;
 }
+
+static void print_args(int argc, const char **argv)
+{
+       for (int i = 0; i < argc; i++)
+               printf("arg %02d: %s\n", i, argv[i]);
+}
+
+static int parse_options_flags__cmd(int argc, const char **argv,
+                                   enum parse_opt_flags test_flags)
+{
+       const char *usage[] = {
+               "<...> cmd [options]",
+               NULL
+       };
+       int opt = 0;
+       const struct option options[] = {
+               OPT_INTEGER('o', "opt", &opt, "an integer option"),
+               OPT_END()
+       };
+
+       argc = parse_options(argc, argv, NULL, options, usage, test_flags);
+
+       printf("opt: %d\n", opt);
+       print_args(argc, argv);
+
+       return 0;
+}
+
+static enum parse_opt_flags test_flags = 0;
+static const struct option test_flag_options[] = {
+       OPT_GROUP("flag-options:"),
+       OPT_BIT(0, "keep-dashdash", &test_flags,
+               "pass PARSE_OPT_KEEP_DASHDASH to parse_options()",
+               PARSE_OPT_KEEP_DASHDASH),
+       OPT_BIT(0, "stop-at-non-option", &test_flags,
+               "pass PARSE_OPT_STOP_AT_NON_OPTION to parse_options()",
+               PARSE_OPT_STOP_AT_NON_OPTION),
+       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, "no-internal-help", &test_flags,
+               "pass PARSE_OPT_NO_INTERNAL_HELP to parse_options()",
+               PARSE_OPT_NO_INTERNAL_HELP),
+       OPT_END()
+};
+
+int cmd__parse_options_flags(int argc, const char **argv)
+{
+       const char *usage[] = {
+               "test-tool parse-options-flags [flag-options] cmd [options]",
+               NULL
+       };
+
+       argc = parse_options(argc, argv, NULL, test_flag_options, usage,
+                            PARSE_OPT_STOP_AT_NON_OPTION);
+
+       if (argc == 0 || strcmp(argv[0], "cmd")) {
+               error("'cmd' is mandatory");
+               usage_with_options(usage, test_flag_options);
+       }
+
+       return parse_options_flags__cmd(argc, argv, test_flags);
+}
index 318fdbab0c315cd0823b76de2864d9689dc5c377..6e62282b60c1bde18ef204ed10287def0f89e073 100644 (file)
@@ -51,6 +51,7 @@ static struct test_cmd cmds[] = {
        { "online-cpus", cmd__online_cpus },
        { "pack-mtimes", cmd__pack_mtimes },
        { "parse-options", cmd__parse_options },
+       { "parse-options-flags", cmd__parse_options_flags },
        { "parse-pathspec-file", cmd__parse_pathspec_file },
        { "partial-clone", cmd__partial_clone },
        { "path-utils", cmd__path_utils },
index bb79927163196cd4815d32fb45ee82a6e1f0bdfc..d8e8403d707bc859c64385f79a44e3b013dcf35f 100644 (file)
@@ -41,6 +41,7 @@ int cmd__oidtree(int argc, const char **argv);
 int cmd__online_cpus(int argc, const char **argv);
 int cmd__pack_mtimes(int argc, const char **argv);
 int cmd__parse_options(int argc, const char **argv);
+int cmd__parse_options_flags(int argc, const char **argv);
 int cmd__parse_pathspec_file(int argc, const char** argv);
 int cmd__partial_clone(int argc, const char **argv);
 int cmd__path_utils(int argc, const char **argv);
index ed2fb620a9d62d80b6fadb7fdbce76aff40cccc4..8511ce24bb476827d5b85fdd903336eefadeb788 100755 (executable)
@@ -456,4 +456,74 @@ test_expect_success '--end-of-options treats remainder as args' '
            --end-of-options --verbose
 '
 
+test_expect_success 'KEEP_DASHDASH works' '
+       test-tool parse-options-flags --keep-dashdash cmd --opt=1 -- --opt=2 --unknown >actual &&
+       cat >expect <<-\EOF &&
+       opt: 1
+       arg 00: --
+       arg 01: --opt=2
+       arg 02: --unknown
+       EOF
+       test_cmp expect actual
+'
+
+test_expect_success 'KEEP_ARGV0 works' '
+       test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual &&
+       cat >expect <<-\EOF &&
+       opt: 3
+       arg 00: cmd
+       arg 01: arg0
+       EOF
+       test_cmp expect actual
+'
+
+test_expect_success 'STOP_AT_NON_OPTION works' '
+       test-tool parse-options-flags --stop-at-non-option cmd --opt=4 arg0 --opt=5 --unknown >actual &&
+       cat >expect <<-\EOF &&
+       opt: 4
+       arg 00: arg0
+       arg 01: --opt=5
+       arg 02: --unknown
+       EOF
+       test_cmp expect actual
+'
+
+test_expect_success 'KEEP_UNKNOWN works' '
+       test-tool parse-options-flags --keep-unknown cmd --unknown=1 --opt=6 -u2 >actual &&
+       cat >expect <<-\EOF &&
+       opt: 6
+       arg 00: --unknown=1
+       arg 01: -u2
+       EOF
+       test_cmp expect actual
+'
+
+test_expect_success 'NO_INTERNAL_HELP works for -h' '
+       test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd -h 2>err &&
+       cat err &&
+       grep "^error: unknown switch \`h$SQ" err &&
+       grep "^usage: " err
+'
+
+for help_opt in help help-all
+do
+       test_expect_success "NO_INTERNAL_HELP works for --$help_opt" "
+               test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd --$help_opt 2>err &&
+               cat err &&
+               grep '^error: unknown option \`'$help_opt\' err &&
+               grep '^usage: ' err
+       "
+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 &&
+       cat >expect <<-\EOF &&
+       opt: 0
+       arg 00: -h
+       arg 01: --help
+       arg 02: --help-all
+       EOF
+       test_cmp expect actual
+'
+
 test_done