]> git.ipfire.org Git - thirdparty/git.git/blob - t/t0040-parse-options.sh
Merge branch 'jk/ci-retire-allow-ref'
[thirdparty/git.git] / t / t0040-parse-options.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Johannes Schindelin
4 #
5
6 test_description='our own option parser'
7
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10
11 cat >expect <<\EOF
12 usage: test-tool parse-options <options>
13
14 A helper function for the parse-options API.
15
16 --[no-]yes get a boolean
17 -D, --no-doubt begins with 'no-'
18 --doubt opposite of --no-doubt
19 -B, --no-fear be brave
20 -b, --[no-]boolean increment by one
21 -4, --[no-]or4 bitwise-or boolean with ...0100
22 --[no-]neg-or4 same as --no-or4
23
24 -i, --[no-]integer <n>
25 get a integer
26 -j <n> get a integer, too
27 -m, --magnitude <n> get a magnitude
28 --[no-]set23 set integer to 23
29 --mode1 set integer to 1 (cmdmode option)
30 --mode2 set integer to 2 (cmdmode option)
31 -L, --[no-]length <str>
32 get length of <str>
33 -F, --[no-]file <file>
34 set file to <file>
35
36 String options
37 -s, --[no-]string <string>
38 get a string
39 --[no-]string2 <str> get another string
40 --[no-]st <st> get another string (pervert ordering)
41 -o <str> get another string
42 --longhelp help text of this entry
43 spans multiple lines
44 --[no-]list <str> add str to list
45
46 Magic arguments
47 -NUM set integer to NUM
48 + same as -b
49 --ambiguous positive ambiguity
50 --no-ambiguous negative ambiguity
51
52 Standard options
53 --[no-]abbrev[=<n>] use <n> digits to display object names
54 -v, --[no-]verbose be verbose
55 -n, --[no-]dry-run dry run
56 -q, --[no-]quiet be quiet
57 --[no-]expect <string>
58 expected output in the variable dump
59
60 Alias
61 -A, --[no-]alias-source <string>
62 get a string
63 -Z, --[no-]alias-target <string>
64 alias of --alias-source
65
66 EOF
67
68 test_expect_success 'test help' '
69 test_must_fail test-tool parse-options -h >output 2>output.err &&
70 test_must_be_empty output.err &&
71 test_cmp expect output
72 '
73
74 mv expect expect.err
75
76 check () {
77 what="$1" &&
78 shift &&
79 expect="$1" &&
80 shift &&
81 test-tool parse-options --expect="$what $expect" "$@"
82 }
83
84 check_unknown_i18n() {
85 case "$1" in
86 --*)
87 echo error: unknown option \`${1#--}\' >expect ;;
88 -*)
89 echo error: unknown switch \`${1#-}\' >expect ;;
90 esac &&
91 cat expect.err >>expect &&
92 test_must_fail test-tool parse-options $* >output 2>output.err &&
93 test_must_be_empty output &&
94 test_cmp expect output.err
95 }
96
97 test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
98 test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
99 test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
100 test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
101 test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
102
103 test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
104 test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
105
106 test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
107 test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
108
109 test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
110 test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
111
112 test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
113
114 test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
115
116 test_expect_success 'OPT_MAGNITUDE() simple' '
117 check magnitude: 2345678 -m 2345678
118 '
119
120 test_expect_success 'OPT_MAGNITUDE() kilo' '
121 check magnitude: 239616 -m 234k
122 '
123
124 test_expect_success 'OPT_MAGNITUDE() mega' '
125 check magnitude: 104857600 -m 100m
126 '
127
128 test_expect_success 'OPT_MAGNITUDE() giga' '
129 check magnitude: 1073741824 -m 1g
130 '
131
132 test_expect_success 'OPT_MAGNITUDE() 3giga' '
133 check magnitude: 3221225472 -m 3g
134 '
135
136 cat >expect <<\EOF
137 boolean: 2
138 integer: 1729
139 magnitude: 16384
140 timestamp: 0
141 string: 123
142 abbrev: 7
143 verbose: 2
144 quiet: 0
145 dry run: yes
146 file: prefix/my.file
147 EOF
148
149 test_expect_success 'short options' '
150 test-tool parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
151 >output 2>output.err &&
152 test_cmp expect output &&
153 test_must_be_empty output.err
154 '
155
156 cat >expect <<\EOF
157 boolean: 2
158 integer: 1729
159 magnitude: 16384
160 timestamp: 0
161 string: 321
162 abbrev: 10
163 verbose: 2
164 quiet: 0
165 dry run: no
166 file: prefix/fi.le
167 EOF
168
169 test_expect_success 'long options' '
170 test-tool parse-options --boolean --integer 1729 --magnitude 16k \
171 --boolean --string2=321 --verbose --verbose --no-dry-run \
172 --abbrev=10 --file fi.le --obsolete \
173 >output 2>output.err &&
174 test_must_be_empty output.err &&
175 test_cmp expect output
176 '
177
178 test_expect_success 'missing required value' '
179 cat >expect <<-\EOF &&
180 error: switch `s'\'' requires a value
181 EOF
182 test_expect_code 129 test-tool parse-options -s 2>actual &&
183 test_cmp expect actual &&
184
185 cat >expect <<-\EOF &&
186 error: option `string'\'' requires a value
187 EOF
188 test_expect_code 129 test-tool parse-options --string 2>actual &&
189 test_cmp expect actual &&
190
191 cat >expect <<-\EOF &&
192 error: option `file'\'' requires a value
193 EOF
194 test_expect_code 129 test-tool parse-options --file 2>actual &&
195 test_cmp expect actual
196 '
197
198 test_expect_success 'superfluous value provided: boolean' '
199 cat >expect <<-\EOF &&
200 error: option `yes'\'' takes no value
201 EOF
202 test_expect_code 129 test-tool parse-options --yes=hi 2>actual &&
203 test_cmp expect actual &&
204
205 cat >expect <<-\EOF &&
206 error: option `no-yes'\'' takes no value
207 EOF
208 test_expect_code 129 test-tool parse-options --no-yes=hi 2>actual &&
209 test_cmp expect actual
210 '
211
212 test_expect_success 'superfluous value provided: cmdmode' '
213 cat >expect <<-\EOF &&
214 error: option `mode1'\'' takes no value
215 EOF
216 test_expect_code 129 test-tool parse-options --mode1=hi 2>actual &&
217 test_cmp expect actual
218 '
219
220 cat >expect <<\EOF
221 boolean: 1
222 integer: 13
223 magnitude: 0
224 timestamp: 0
225 string: 123
226 abbrev: 7
227 verbose: -1
228 quiet: 0
229 dry run: no
230 file: (not set)
231 arg 00: a1
232 arg 01: b1
233 arg 02: --boolean
234 EOF
235
236 test_expect_success 'intermingled arguments' '
237 test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
238 >output 2>output.err &&
239 test_must_be_empty output.err &&
240 test_cmp expect output
241 '
242
243 cat >expect <<\EOF
244 boolean: 0
245 integer: 2
246 magnitude: 0
247 timestamp: 0
248 string: (not set)
249 abbrev: 7
250 verbose: -1
251 quiet: 0
252 dry run: no
253 file: (not set)
254 EOF
255
256 test_expect_success 'unambiguously abbreviated option' '
257 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
258 test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err &&
259 test_must_be_empty output.err &&
260 test_cmp expect output
261 '
262
263 test_expect_success 'unambiguously abbreviated option with "="' '
264 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
265 test-tool parse-options --expect="integer: 2" --int=2
266 '
267
268 test_expect_success 'ambiguously abbreviated option' '
269 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
270 test-tool parse-options --strin 123
271 '
272
273 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
274 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
275 test-tool parse-options --expect="string: 123" --st 123
276 '
277
278 test_expect_success 'Alias options do not contribute to abbreviation' '
279 test-tool parse-options --alias-source 123 >output &&
280 grep "^string: 123" output &&
281 test-tool parse-options --alias-target 123 >output &&
282 grep "^string: 123" output &&
283 test_must_fail test-tool parse-options --alias &&
284 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
285 test-tool parse-options --alias 123 >output &&
286 grep "^string: 123" output
287 '
288
289 cat >typo.err <<\EOF
290 error: did you mean `--boolean` (with two dashes)?
291 EOF
292
293 test_expect_success 'detect possible typos' '
294 test_must_fail test-tool parse-options -boolean >output 2>output.err &&
295 test_must_be_empty output &&
296 test_cmp typo.err output.err
297 '
298
299 cat >typo.err <<\EOF
300 error: did you mean `--ambiguous` (with two dashes)?
301 EOF
302
303 test_expect_success 'detect possible typos' '
304 test_must_fail test-tool parse-options -ambiguous >output 2>output.err &&
305 test_must_be_empty output &&
306 test_cmp typo.err output.err
307 '
308
309 cat >expect <<\EOF
310 Callback: "four", 0
311 boolean: 5
312 integer: 4
313 magnitude: 0
314 timestamp: 0
315 string: (not set)
316 abbrev: 7
317 verbose: -1
318 quiet: 0
319 dry run: no
320 file: (not set)
321 EOF
322
323 test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
324 test-tool parse-options --length=four -b -4 >output 2>output.err &&
325 test_must_be_empty output.err &&
326 test_cmp expect output
327 '
328
329 test_expect_success 'OPT_CALLBACK() and callback errors work' '
330 test_must_fail test-tool parse-options --no-length >output 2>output.err &&
331 test_must_be_empty output &&
332 test_must_be_empty output.err
333 '
334
335 cat >expect <<\EOF
336 boolean: 1
337 integer: 23
338 magnitude: 0
339 timestamp: 0
340 string: (not set)
341 abbrev: 7
342 verbose: -1
343 quiet: 0
344 dry run: no
345 file: (not set)
346 EOF
347
348 test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
349 test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
350 test_must_be_empty output.err &&
351 test_cmp expect output
352 '
353
354 test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
355 test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
356 test_must_be_empty output.err &&
357 test_cmp expect output
358 '
359
360 test_expect_success 'OPT_BIT() works' '
361 test-tool parse-options --expect="boolean: 6" -bb --or4
362 '
363
364 test_expect_success 'OPT_NEGBIT() works' '
365 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
366 '
367
368 test_expect_success 'OPT_CMDMODE() works' '
369 test-tool parse-options --expect="integer: 1" --mode1
370 '
371
372 test_expect_success 'OPT_CMDMODE() detects incompatibility' '
373 test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err &&
374 test_must_be_empty output &&
375 test_i18ngrep "incompatible with --mode" output.err
376 '
377
378 test_expect_success 'OPT_CMDMODE() detects incompatibility with something else' '
379 test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err &&
380 test_must_be_empty output &&
381 test_i18ngrep "incompatible with something else" output.err
382 '
383
384 test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
385 test-tool parse-options --expect="boolean: 6" + + + + + +
386 '
387
388 test_expect_success 'OPT_NUMBER_CALLBACK() works' '
389 test-tool parse-options --expect="integer: 12345" -12345
390 '
391
392 cat >expect <<\EOF
393 boolean: 0
394 integer: 0
395 magnitude: 0
396 timestamp: 0
397 string: (not set)
398 abbrev: 7
399 verbose: -1
400 quiet: 0
401 dry run: no
402 file: (not set)
403 EOF
404
405 test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
406 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
407 test-tool parse-options --no-ambig >output 2>output.err &&
408 test_must_be_empty output.err &&
409 test_cmp expect output
410 '
411
412 cat >>expect <<\EOF
413 list: foo
414 list: bar
415 list: baz
416 EOF
417 test_expect_success '--list keeps list of strings' '
418 test-tool parse-options --list foo --list=bar --list=baz >output &&
419 test_cmp expect output
420 '
421
422 test_expect_success '--no-list resets list' '
423 test-tool parse-options --list=other --list=irrelevant --list=options \
424 --no-list --list=foo --list=bar --list=baz >output &&
425 test_cmp expect output
426 '
427
428 test_expect_success 'multiple quiet levels' '
429 test-tool parse-options --expect="quiet: 3" -q -q -q
430 '
431
432 test_expect_success 'multiple verbose levels' '
433 test-tool parse-options --expect="verbose: 3" -v -v -v
434 '
435
436 test_expect_success '--no-quiet sets --quiet to 0' '
437 test-tool parse-options --expect="quiet: 0" --no-quiet
438 '
439
440 test_expect_success '--no-quiet resets multiple -q to 0' '
441 test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet
442 '
443
444 test_expect_success '--no-verbose sets verbose to 0' '
445 test-tool parse-options --expect="verbose: 0" --no-verbose
446 '
447
448 test_expect_success '--no-verbose resets multiple verbose to 0' '
449 test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose
450 '
451
452 test_expect_success 'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' '
453 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
454 test-tool parse-options --ye &&
455 test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \
456 test-tool parse-options --ye
457 '
458
459 test_expect_success '--end-of-options treats remainder as args' '
460 test-tool parse-options \
461 --expect="verbose: -1" \
462 --expect="arg 00: --verbose" \
463 --end-of-options --verbose
464 '
465
466 test_expect_success 'KEEP_DASHDASH works' '
467 test-tool parse-options-flags --keep-dashdash cmd --opt=1 -- --opt=2 --unknown >actual &&
468 cat >expect <<-\EOF &&
469 opt: 1
470 arg 00: --
471 arg 01: --opt=2
472 arg 02: --unknown
473 EOF
474 test_cmp expect actual
475 '
476
477 test_expect_success 'KEEP_ARGV0 works' '
478 test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual &&
479 cat >expect <<-\EOF &&
480 opt: 3
481 arg 00: cmd
482 arg 01: arg0
483 EOF
484 test_cmp expect actual
485 '
486
487 test_expect_success 'STOP_AT_NON_OPTION works' '
488 test-tool parse-options-flags --stop-at-non-option cmd --opt=4 arg0 --opt=5 --unknown >actual &&
489 cat >expect <<-\EOF &&
490 opt: 4
491 arg 00: arg0
492 arg 01: --opt=5
493 arg 02: --unknown
494 EOF
495 test_cmp expect actual
496 '
497
498 test_expect_success 'KEEP_UNKNOWN_OPT works' '
499 test-tool parse-options-flags --keep-unknown-opt cmd --unknown=1 --opt=6 -u2 >actual &&
500 cat >expect <<-\EOF &&
501 opt: 6
502 arg 00: --unknown=1
503 arg 01: -u2
504 EOF
505 test_cmp expect actual
506 '
507
508 test_expect_success 'NO_INTERNAL_HELP works for -h' '
509 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd -h 2>err &&
510 grep "^error: unknown switch \`h$SQ" err &&
511 grep "^usage: " err
512 '
513
514 for help_opt in help help-all
515 do
516 test_expect_success "NO_INTERNAL_HELP works for --$help_opt" "
517 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd --$help_opt 2>err &&
518 grep '^error: unknown option \`'$help_opt\' err &&
519 grep '^usage: ' err
520 "
521 done
522
523 test_expect_success 'KEEP_UNKNOWN_OPT | NO_INTERNAL_HELP works' '
524 test-tool parse-options-flags --keep-unknown-opt --no-internal-help cmd -h --help --help-all >actual &&
525 cat >expect <<-\EOF &&
526 opt: 0
527 arg 00: -h
528 arg 01: --help
529 arg 02: --help-all
530 EOF
531 test_cmp expect actual
532 '
533
534 test_expect_success 'subcommand - no subcommand shows error and usage' '
535 test_expect_code 129 test-tool parse-subcommand cmd 2>err &&
536 grep "^error: need a subcommand" err &&
537 grep ^usage: err
538 '
539
540 test_expect_success 'subcommand - subcommand after -- shows error and usage' '
541 test_expect_code 129 test-tool parse-subcommand cmd -- subcmd-one 2>err &&
542 grep "^error: need a subcommand" err &&
543 grep ^usage: err
544 '
545
546 test_expect_success 'subcommand - subcommand after --end-of-options shows error and usage' '
547 test_expect_code 129 test-tool parse-subcommand cmd --end-of-options subcmd-one 2>err &&
548 grep "^error: need a subcommand" err &&
549 grep ^usage: err
550 '
551
552 test_expect_success 'subcommand - unknown subcommand shows error and usage' '
553 test_expect_code 129 test-tool parse-subcommand cmd nope 2>err &&
554 grep "^error: unknown subcommand: \`nope$SQ" err &&
555 grep ^usage: err
556 '
557
558 test_expect_success 'subcommand - subcommands cannot be abbreviated' '
559 test_expect_code 129 test-tool parse-subcommand cmd subcmd-o 2>err &&
560 grep "^error: unknown subcommand: \`subcmd-o$SQ$" err &&
561 grep ^usage: err
562 '
563
564 test_expect_success 'subcommand - no negated subcommands' '
565 test_expect_code 129 test-tool parse-subcommand cmd no-subcmd-one 2>err &&
566 grep "^error: unknown subcommand: \`no-subcmd-one$SQ" err &&
567 grep ^usage: err
568 '
569
570 test_expect_success 'subcommand - simple' '
571 test-tool parse-subcommand cmd subcmd-two >actual &&
572 cat >expect <<-\EOF &&
573 opt: 0
574 fn: subcmd_two
575 arg 00: subcmd-two
576 EOF
577 test_cmp expect actual
578 '
579
580 test_expect_success 'subcommand - stop parsing at the first subcommand' '
581 test-tool parse-subcommand cmd --opt=1 subcmd-two subcmd-one --opt=2 >actual &&
582 cat >expect <<-\EOF &&
583 opt: 1
584 fn: subcmd_two
585 arg 00: subcmd-two
586 arg 01: subcmd-one
587 arg 02: --opt=2
588 EOF
589 test_cmp expect actual
590 '
591
592 test_expect_success 'subcommand - KEEP_ARGV0' '
593 test-tool parse-subcommand --keep-argv0 cmd subcmd-two >actual &&
594 cat >expect <<-\EOF &&
595 opt: 0
596 fn: subcmd_two
597 arg 00: cmd
598 arg 01: subcmd-two
599 EOF
600 test_cmp expect actual
601 '
602
603 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given' '
604 test-tool parse-subcommand --subcommand-optional cmd >actual &&
605 cat >expect <<-\EOF &&
606 opt: 0
607 fn: subcmd_one
608 EOF
609 test_cmp expect actual
610 '
611
612 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + given subcommand' '
613 test-tool parse-subcommand --subcommand-optional cmd subcmd-two branch file >actual &&
614 cat >expect <<-\EOF &&
615 opt: 0
616 fn: subcmd_two
617 arg 00: subcmd-two
618 arg 01: branch
619 arg 02: file
620 EOF
621 test_cmp expect actual
622 '
623
624 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown dashless args' '
625 test-tool parse-subcommand --subcommand-optional cmd branch file >actual &&
626 cat >expect <<-\EOF &&
627 opt: 0
628 fn: subcmd_one
629 arg 00: branch
630 arg 01: file
631 EOF
632 test_cmp expect actual
633 '
634
635 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown option' '
636 test_expect_code 129 test-tool parse-subcommand --subcommand-optional cmd --subcommand-opt 2>err &&
637 grep "^error: unknown option" err &&
638 grep ^usage: err
639 '
640
641 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand not given + unknown option' '
642 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt >actual &&
643 cat >expect <<-\EOF &&
644 opt: 0
645 fn: subcmd_one
646 arg 00: --subcommand-opt
647 EOF
648 test_cmp expect actual
649 '
650
651 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand ignored after unknown option' '
652 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt subcmd-two >actual &&
653 cat >expect <<-\EOF &&
654 opt: 0
655 fn: subcmd_one
656 arg 00: --subcommand-opt
657 arg 01: subcmd-two
658 EOF
659 test_cmp expect actual
660 '
661
662 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + command and subcommand options cannot be mixed' '
663 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt branch --opt=1 >actual &&
664 cat >expect <<-\EOF &&
665 opt: 0
666 fn: subcmd_one
667 arg 00: --subcommand-opt
668 arg 01: branch
669 arg 02: --opt=1
670 EOF
671 test_cmp expect actual
672 '
673
674 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_ARGV0' '
675 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-argv0 cmd --subcommand-opt branch >actual &&
676 cat >expect <<-\EOF &&
677 opt: 0
678 fn: subcmd_one
679 arg 00: cmd
680 arg 01: --subcommand-opt
681 arg 02: branch
682 EOF
683 test_cmp expect actual
684 '
685
686 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_DASHDASH' '
687 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-dashdash cmd -- --subcommand-opt file >actual &&
688 cat >expect <<-\EOF &&
689 opt: 0
690 fn: subcmd_one
691 arg 00: --
692 arg 01: --subcommand-opt
693 arg 02: file
694 EOF
695 test_cmp expect actual
696 '
697
698 test_expect_success 'subcommand - completion helper' '
699 test-tool parse-subcommand cmd --git-completion-helper >actual &&
700 echo "subcmd-one subcmd-two --opt= --no-opt" >expect &&
701 test_cmp expect actual
702 '
703
704 test_expect_success 'subcommands are incompatible with STOP_AT_NON_OPTION' '
705 test_must_fail test-tool parse-subcommand --stop-at-non-option cmd subcmd-one 2>err &&
706 grep ^BUG err
707 '
708
709 test_expect_success 'subcommands are incompatible with KEEP_UNKNOWN_OPT unless in combination with SUBCOMMAND_OPTIONAL' '
710 test_must_fail test-tool parse-subcommand --keep-unknown-opt cmd subcmd-two 2>err &&
711 grep ^BUG err
712 '
713
714 test_expect_success 'subcommands are incompatible with KEEP_DASHDASH unless in combination with SUBCOMMAND_OPTIONAL' '
715 test_must_fail test-tool parse-subcommand --keep-dashdash cmd subcmd-two 2>err &&
716 grep ^BUG err
717 '
718
719 test_expect_success 'negative magnitude' '
720 test_must_fail test-tool parse-options --magnitude -1 >out 2>err &&
721 grep "non-negative integer" err &&
722 test_must_be_empty out
723 '
724
725 test_expect_success 'magnitude with units but no numbers' '
726 test_must_fail test-tool parse-options --magnitude m >out 2>err &&
727 grep "non-negative integer" err &&
728 test_must_be_empty out
729 '
730
731 test_done