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>
SZEDER Gábor committed
Aug 19, 2022 at 18:03 UTC
c1b117d31ca9b09444f14092c35d092f3330823e
4 files changed
+138
t/helper/test-parse-options.c
+66
@@ -192,3 +192,69 @@ int cmd__parse_options(int argc, const char **argv)
192
193
return ret;
194
}
195
+
196
+static void print_args(int argc, const char **argv)
197
+{
198
+ for (int i = 0; i < argc; i++)
199
+ printf("arg %02d: %s\n", i, argv[i]);
200
+}
201
+
202
+static int parse_options_flags__cmd(int argc, const char **argv,
203
+ enum parse_opt_flags test_flags)
204
+{
205
+ const char *usage[] = {
206
+ "<...> cmd [options]",
207
+ NULL
208
+ };
209
+ int opt = 0;
210
+ const struct option options[] = {
211
+ OPT_INTEGER('o', "opt", &opt, "an integer option"),
212
+ OPT_END()
213
+ };
214
+
215
+ argc = parse_options(argc, argv, NULL, options, usage, test_flags);
216
+
217
+ printf("opt: %d\n", opt);
218
+ print_args(argc, argv);
219
+
220
+ return 0;
221
+}
222
+
223
+static enum parse_opt_flags test_flags = 0;
224
+static const struct option test_flag_options[] = {
225
+ OPT_GROUP("flag-options:"),
226
+ OPT_BIT(0, "keep-dashdash", &test_flags,
227
+ "pass PARSE_OPT_KEEP_DASHDASH to parse_options()",
228
+ PARSE_OPT_KEEP_DASHDASH),
229
+ OPT_BIT(0, "stop-at-non-option", &test_flags,
230
+ "pass PARSE_OPT_STOP_AT_NON_OPTION to parse_options()",
231
+ PARSE_OPT_STOP_AT_NON_OPTION),
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),
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),
241
+ OPT_END()
242
+};
243
+
244
+int cmd__parse_options_flags(int argc, const char **argv)
245
+{
246
+ const char *usage[] = {
247
+ "test-tool parse-options-flags [flag-options] cmd [options]",
248
+ NULL
249
+ };
250
+
251
+ argc = parse_options(argc, argv, NULL, test_flag_options, usage,
252
+ PARSE_OPT_STOP_AT_NON_OPTION);
253
+
254
+ if (argc == 0 || strcmp(argv[0], "cmd")) {
255
+ error("'cmd' is mandatory");
256
+ usage_with_options(usage, test_flag_options);
257
+ }
258
+
259
+ return parse_options_flags__cmd(argc, argv, test_flags);
260
+}
t/helper/test-tool.c
+1
@@ -51,6 +51,7 @@ static struct test_cmd cmds[] = {
51
{ "online-cpus", cmd__online_cpus },
52
{ "pack-mtimes", cmd__pack_mtimes },
53
{ "parse-options", cmd__parse_options },
54
+ { "parse-options-flags", cmd__parse_options_flags },
55
{ "parse-pathspec-file", cmd__parse_pathspec_file },
56
{ "partial-clone", cmd__partial_clone },
57
{ "path-utils", cmd__path_utils },
t/helper/test-tool.h
+1
@@ -41,6 +41,7 @@ int cmd__oidtree(int argc, const char **argv);
41
int cmd__online_cpus(int argc, const char **argv);
42
int cmd__pack_mtimes(int argc, const char **argv);
43
int cmd__parse_options(int argc, const char **argv);
44
+int cmd__parse_options_flags(int argc, const char **argv);
45
int cmd__parse_pathspec_file(int argc, const char** argv);
46
int cmd__partial_clone(int argc, const char **argv);
47
int cmd__path_utils(int argc, const char **argv);
t/t0040-parse-options.sh
+70
@@ -456,4 +456,74 @@ test_expect_success '--end-of-options treats remainder as args' '
456
--end-of-options --verbose
457
'
458
459
+test_expect_success 'KEEP_DASHDASH works' '
460
+ test-tool parse-options-flags --keep-dashdash cmd --opt=1 -- --opt=2 --unknown >actual &&
461
+ cat >expect <<-\EOF &&
462
+ opt: 1
463
+ arg 00: --
464
+ arg 01: --opt=2
465
+ arg 02: --unknown
466
+ EOF
467
+ test_cmp expect actual
468
+'
469
+
470
+test_expect_success 'KEEP_ARGV0 works' '
471
+ test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual &&
472
+ cat >expect <<-\EOF &&
473
+ opt: 3
474
+ arg 00: cmd
475
+ arg 01: arg0
476
+ EOF
477
+ test_cmp expect actual
478
+'
479
+
480
+test_expect_success 'STOP_AT_NON_OPTION works' '
481
+ test-tool parse-options-flags --stop-at-non-option cmd --opt=4 arg0 --opt=5 --unknown >actual &&
482
+ cat >expect <<-\EOF &&
483
+ opt: 4
484
+ arg 00: arg0
485
+ arg 01: --opt=5
486
+ arg 02: --unknown
487
+ EOF
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 &&
493
+ cat >expect <<-\EOF &&
494
+ opt: 6
495
+ arg 00: --unknown=1
496
+ arg 01: -u2
497
+ EOF
498
+ test_cmp expect actual
499
+'
500
+
501
+test_expect_success 'NO_INTERNAL_HELP works for -h' '
502
+ test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd -h 2>err &&
503
+ cat err &&
504
+ grep "^error: unknown switch \`h$SQ" err &&
505
+ grep "^usage: " err
506
+'
507
+
508
+for help_opt in help help-all
509
+do
510
+ test_expect_success "NO_INTERNAL_HELP works for --$help_opt" "
511
+ test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd --$help_opt 2>err &&
512
+ cat err &&
513
+ grep '^error: unknown option \`'$help_opt\' err &&
514
+ grep '^usage: ' err
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 &&
520
+ cat >expect <<-\EOF &&
521
+ opt: 0
522
+ arg 00: -h
523
+ arg 01: --help
524
+ arg 02: --help-all
525
+ EOF
526
+ test_cmp expect actual
527
+'
528
+
529
test_done