t0040-parse-options: test parse_options() with various 'parse_opt_flags'

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>
This commit is contained in:
SZEDER Gábor
2022-08-19 18:03:55 +02:00
committed by Junio C Hamano
parent 31a66c1964
commit c1b117d31c
4 changed files with 138 additions and 0 deletions

View 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);
}