Sync with 'maint'
[alt-git.git] / t / t0040-parse-options.sh
blob45a773642fd167e9190b9b693a08001bb5939883
1 #!/bin/sh
3 # Copyright (c) 2007 Johannes Schindelin
6 test_description='our own option parser'
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 cat >expect <<\EOF
12 usage: test-tool parse-options <options>
14 A helper function for the parse-options API.
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
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 --[no-]mode34 (3|4) set integer to 3 or 4 (cmdmode option)
32 -L, --[no-]length <str>
33 get length of <str>
34 -F, --[no-]file <file>
35 set file to <file>
37 String options
38 -s, --[no-]string <string>
39 get a string
40 --[no-]string2 <str> get another string
41 --[no-]st <st> get another string (pervert ordering)
42 -o <str> get another string
43 --longhelp help text of this entry
44 spans multiple lines
45 --[no-]list <str> add str to list
47 Magic arguments
48 -NUM set integer to NUM
49 + same as -b
50 --ambiguous positive ambiguity
51 --no-ambiguous negative ambiguity
53 Standard options
54 --[no-]abbrev[=<n>] use <n> digits to display object names
55 -v, --[no-]verbose be verbose
56 -n, --[no-]dry-run dry run
57 -q, --[no-]quiet be quiet
58 --[no-]expect <string>
59 expected output in the variable dump
61 Alias
62 -A, --[no-]alias-source <string>
63 get a string
64 -Z, --[no-]alias-target <string>
65 alias of --alias-source
67 EOF
69 test_expect_success 'test help' '
70 test_must_fail test-tool parse-options -h >output 2>output.err &&
71 test_must_be_empty output.err &&
72 test_cmp expect output
75 mv expect expect.err
77 check () {
78 what="$1" &&
79 shift &&
80 expect="$1" &&
81 shift &&
82 test-tool parse-options --expect="$what $expect" "$@"
85 check_unknown_i18n() {
86 case "$1" in
87 --*)
88 echo error: unknown option \`${1#--}\' >expect ;;
89 -*)
90 echo error: unknown switch \`${1#-}\' >expect ;;
91 esac &&
92 cat expect.err >>expect &&
93 test_must_fail test-tool parse-options $* >output 2>output.err &&
94 test_must_be_empty output &&
95 test_cmp expect output.err
98 test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
99 test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
100 test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
101 test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
102 test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
104 test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
105 test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
107 test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
108 test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
110 test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
111 test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
113 test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
115 test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
117 test_expect_success 'OPT_MAGNITUDE() simple' '
118 check magnitude: 2345678 -m 2345678
121 test_expect_success 'OPT_MAGNITUDE() kilo' '
122 check magnitude: 239616 -m 234k
125 test_expect_success 'OPT_MAGNITUDE() mega' '
126 check magnitude: 104857600 -m 100m
129 test_expect_success 'OPT_MAGNITUDE() giga' '
130 check magnitude: 1073741824 -m 1g
133 test_expect_success 'OPT_MAGNITUDE() 3giga' '
134 check magnitude: 3221225472 -m 3g
137 cat >expect <<\EOF
138 boolean: 2
139 integer: 1729
140 magnitude: 16384
141 timestamp: 0
142 string: 123
143 abbrev: 7
144 verbose: 2
145 quiet: 0
146 dry run: yes
147 file: prefix/my.file
150 test_expect_success 'short options' '
151 test-tool parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
152 >output 2>output.err &&
153 test_cmp expect output &&
154 test_must_be_empty output.err
157 cat >expect <<\EOF
158 boolean: 2
159 integer: 1729
160 magnitude: 16384
161 timestamp: 0
162 string: 321
163 abbrev: 10
164 verbose: 2
165 quiet: 0
166 dry run: no
167 file: prefix/fi.le
170 test_expect_success 'long options' '
171 test-tool parse-options --boolean --integer 1729 --magnitude 16k \
172 --boolean --string2=321 --verbose --verbose --no-dry-run \
173 --abbrev=10 --file fi.le --obsolete \
174 >output 2>output.err &&
175 test_must_be_empty output.err &&
176 test_cmp expect output
179 test_expect_success 'abbreviate to something longer than SHA1 length' '
180 cat >expect <<-EOF &&
181 boolean: 0
182 integer: 0
183 magnitude: 0
184 timestamp: 0
185 string: (not set)
186 abbrev: 100
187 verbose: -1
188 quiet: 0
189 dry run: no
190 file: (not set)
192 test-tool parse-options --abbrev=100 >output &&
193 test_cmp expect output
196 test_expect_success 'missing required value' '
197 cat >expect <<-\EOF &&
198 error: switch `s'\'' requires a value
200 test_expect_code 129 test-tool parse-options -s 2>actual &&
201 test_cmp expect actual &&
203 cat >expect <<-\EOF &&
204 error: option `string'\'' requires a value
206 test_expect_code 129 test-tool parse-options --string 2>actual &&
207 test_cmp expect actual &&
209 cat >expect <<-\EOF &&
210 error: option `file'\'' requires a value
212 test_expect_code 129 test-tool parse-options --file 2>actual &&
213 test_cmp expect actual
216 test_expect_success 'superfluous value provided: boolean' '
217 cat >expect <<-\EOF &&
218 error: option `yes'\'' takes no value
220 test_expect_code 129 test-tool parse-options --yes=hi 2>actual &&
221 test_cmp expect actual &&
223 cat >expect <<-\EOF &&
224 error: option `no-yes'\'' takes no value
226 test_expect_code 129 test-tool parse-options --no-yes=hi 2>actual &&
227 test_cmp expect actual
230 test_expect_success 'superfluous value provided: boolean, abbreviated' '
231 cat >expect <<-\EOF &&
232 error: option `yes'\'' takes no value
234 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
235 test-tool parse-options --ye=hi 2>actual &&
236 test_cmp expect actual &&
238 cat >expect <<-\EOF &&
239 error: option `no-yes'\'' takes no value
241 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
242 test-tool parse-options --no-ye=hi 2>actual &&
243 test_cmp expect actual
246 test_expect_success 'superfluous value provided: cmdmode' '
247 cat >expect <<-\EOF &&
248 error: option `mode1'\'' takes no value
250 test_expect_code 129 test-tool parse-options --mode1=hi 2>actual &&
251 test_cmp expect actual
254 cat >expect <<\EOF
255 boolean: 1
256 integer: 13
257 magnitude: 0
258 timestamp: 0
259 string: 123
260 abbrev: 7
261 verbose: -1
262 quiet: 0
263 dry run: no
264 file: (not set)
265 arg 00: a1
266 arg 01: b1
267 arg 02: --boolean
270 test_expect_success 'intermingled arguments' '
271 test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
272 >output 2>output.err &&
273 test_must_be_empty output.err &&
274 test_cmp expect output
277 cat >expect <<\EOF
278 boolean: 0
279 integer: 2
280 magnitude: 0
281 timestamp: 0
282 string: (not set)
283 abbrev: 7
284 verbose: -1
285 quiet: 0
286 dry run: no
287 file: (not set)
290 test_expect_success 'unambiguously abbreviated option' '
291 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
292 test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err &&
293 test_must_be_empty output.err &&
294 test_cmp expect output
297 test_expect_success 'unambiguously abbreviated option with "="' '
298 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
299 test-tool parse-options --expect="integer: 2" --int=2
302 test_expect_success 'ambiguously abbreviated option' '
303 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
304 test-tool parse-options --strin 123
307 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
308 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
309 test-tool parse-options --expect="string: 123" --st 123
312 test_expect_success 'Alias options do not contribute to abbreviation' '
313 test-tool parse-options --alias-source 123 >output &&
314 grep "^string: 123" output &&
315 test-tool parse-options --alias-target 123 >output &&
316 grep "^string: 123" output &&
317 test_must_fail test-tool parse-options --alias &&
318 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
319 test-tool parse-options --alias 123 >output &&
320 grep "^string: 123" output
323 cat >typo.err <<\EOF
324 error: did you mean `--boolean` (with two dashes)?
327 test_expect_success 'detect possible typos' '
328 test_must_fail test-tool parse-options -boolean >output 2>output.err &&
329 test_must_be_empty output &&
330 test_cmp typo.err output.err
333 cat >typo.err <<\EOF
334 error: did you mean `--ambiguous` (with two dashes)?
337 test_expect_success 'detect possible typos' '
338 test_must_fail test-tool parse-options -ambiguous >output 2>output.err &&
339 test_must_be_empty output &&
340 test_cmp typo.err output.err
343 cat >expect <<\EOF
344 Callback: "four", 0
345 boolean: 5
346 integer: 4
347 magnitude: 0
348 timestamp: 0
349 string: (not set)
350 abbrev: 7
351 verbose: -1
352 quiet: 0
353 dry run: no
354 file: (not set)
357 test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
358 test-tool parse-options --length=four -b -4 >output 2>output.err &&
359 test_must_be_empty output.err &&
360 test_cmp expect output
363 test_expect_success 'OPT_CALLBACK() and callback errors work' '
364 test_must_fail test-tool parse-options --no-length >output 2>output.err &&
365 test_must_be_empty output &&
366 test_must_be_empty output.err
369 cat >expect <<\EOF
370 boolean: 1
371 integer: 23
372 magnitude: 0
373 timestamp: 0
374 string: (not set)
375 abbrev: 7
376 verbose: -1
377 quiet: 0
378 dry run: no
379 file: (not set)
382 test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
383 test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
384 test_must_be_empty output.err &&
385 test_cmp expect output
388 test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
389 test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
390 test_must_be_empty output.err &&
391 test_cmp expect output
394 test_expect_success 'OPT_BIT() works' '
395 test-tool parse-options --expect="boolean: 6" -bb --or4
398 test_expect_success 'OPT_NEGBIT() works' '
399 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
402 test_expect_success 'OPT_CMDMODE() works' '
403 test-tool parse-options --expect="integer: 1" --mode1 &&
404 test-tool parse-options --expect="integer: 3" --mode34=3
407 test_expect_success 'OPT_CMDMODE() detects incompatibility (1)' '
408 test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err &&
409 test_must_be_empty output &&
410 test_grep "mode1" output.err &&
411 test_grep "mode2" output.err &&
412 test_grep "cannot be used together" output.err
415 test_expect_success 'OPT_CMDMODE() detects incompatibility (2)' '
416 test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err &&
417 test_must_be_empty output &&
418 test_grep "mode2" output.err &&
419 test_grep "set23" output.err &&
420 test_grep "cannot be used together" output.err
423 test_expect_success 'OPT_CMDMODE() detects incompatibility (3)' '
424 test_must_fail test-tool parse-options --mode2 --set23 >output 2>output.err &&
425 test_must_be_empty output &&
426 test_grep "mode2" output.err &&
427 test_grep "set23" output.err &&
428 test_grep "cannot be used together" output.err
431 test_expect_success 'OPT_CMDMODE() detects incompatibility (4)' '
432 test_must_fail test-tool parse-options --mode2 --mode34=3 \
433 >output 2>output.err &&
434 test_must_be_empty output &&
435 test_grep "mode2" output.err &&
436 test_grep "mode34.3" output.err &&
437 test_grep "cannot be used together" output.err
440 test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
441 test-tool parse-options --expect="boolean: 6" + + + + + +
444 test_expect_success 'OPT_NUMBER_CALLBACK() works' '
445 test-tool parse-options --expect="integer: 12345" -12345
448 cat >expect <<\EOF
449 boolean: 0
450 integer: 0
451 magnitude: 0
452 timestamp: 0
453 string: (not set)
454 abbrev: 7
455 verbose: -1
456 quiet: 0
457 dry run: no
458 file: (not set)
461 test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
462 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
463 test-tool parse-options --no-ambig >output 2>output.err &&
464 test_must_be_empty output.err &&
465 test_cmp expect output
468 cat >>expect <<\EOF
469 list: foo
470 list: bar
471 list: baz
473 test_expect_success '--list keeps list of strings' '
474 test-tool parse-options --list foo --list=bar --list=baz >output &&
475 test_cmp expect output
478 test_expect_success '--no-list resets list' '
479 test-tool parse-options --list=other --list=irrelevant --list=options \
480 --no-list --list=foo --list=bar --list=baz >output &&
481 test_cmp expect output
484 test_expect_success 'multiple quiet levels' '
485 test-tool parse-options --expect="quiet: 3" -q -q -q
488 test_expect_success 'multiple verbose levels' '
489 test-tool parse-options --expect="verbose: 3" -v -v -v
492 test_expect_success '--no-quiet sets --quiet to 0' '
493 test-tool parse-options --expect="quiet: 0" --no-quiet
496 test_expect_success '--no-quiet resets multiple -q to 0' '
497 test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet
500 test_expect_success '--no-verbose sets verbose to 0' '
501 test-tool parse-options --expect="verbose: 0" --no-verbose
504 test_expect_success '--no-verbose resets multiple verbose to 0' '
505 test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose
508 test_expect_success 'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' '
509 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
510 test-tool parse-options --ye &&
511 test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \
512 test-tool parse-options --ye
515 test_expect_success '--end-of-options treats remainder as args' '
516 test-tool parse-options \
517 --expect="verbose: -1" \
518 --expect="arg 00: --verbose" \
519 --end-of-options --verbose
522 test_expect_success 'KEEP_DASHDASH works' '
523 test-tool parse-options-flags --keep-dashdash cmd --opt=1 -- --opt=2 --unknown >actual &&
524 cat >expect <<-\EOF &&
525 opt: 1
526 arg 00: --
527 arg 01: --opt=2
528 arg 02: --unknown
530 test_cmp expect actual
533 test_expect_success 'KEEP_ARGV0 works' '
534 test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual &&
535 cat >expect <<-\EOF &&
536 opt: 3
537 arg 00: cmd
538 arg 01: arg0
540 test_cmp expect actual
543 test_expect_success 'STOP_AT_NON_OPTION works' '
544 test-tool parse-options-flags --stop-at-non-option cmd --opt=4 arg0 --opt=5 --unknown >actual &&
545 cat >expect <<-\EOF &&
546 opt: 4
547 arg 00: arg0
548 arg 01: --opt=5
549 arg 02: --unknown
551 test_cmp expect actual
554 test_expect_success 'KEEP_UNKNOWN_OPT works' '
555 test-tool parse-options-flags --keep-unknown-opt cmd --unknown=1 --opt=6 -u2 >actual &&
556 cat >expect <<-\EOF &&
557 opt: 6
558 arg 00: --unknown=1
559 arg 01: -u2
561 test_cmp expect actual
564 test_expect_success 'NO_INTERNAL_HELP works for -h' '
565 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd -h 2>err &&
566 grep "^error: unknown switch \`h$SQ" err &&
567 grep "^usage: " err
570 for help_opt in help help-all
572 test_expect_success "NO_INTERNAL_HELP works for --$help_opt" "
573 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd --$help_opt 2>err &&
574 grep '^error: unknown option \`'$help_opt\' err &&
575 grep '^usage: ' err
577 done
579 test_expect_success 'KEEP_UNKNOWN_OPT | NO_INTERNAL_HELP works' '
580 test-tool parse-options-flags --keep-unknown-opt --no-internal-help cmd -h --help --help-all >actual &&
581 cat >expect <<-\EOF &&
582 opt: 0
583 arg 00: -h
584 arg 01: --help
585 arg 02: --help-all
587 test_cmp expect actual
590 test_expect_success 'subcommand - no subcommand shows error and usage' '
591 test_expect_code 129 test-tool parse-subcommand cmd 2>err &&
592 grep "^error: need a subcommand" err &&
593 grep ^usage: err
596 test_expect_success 'subcommand - subcommand after -- shows error and usage' '
597 test_expect_code 129 test-tool parse-subcommand cmd -- subcmd-one 2>err &&
598 grep "^error: need a subcommand" err &&
599 grep ^usage: err
602 test_expect_success 'subcommand - subcommand after --end-of-options shows error and usage' '
603 test_expect_code 129 test-tool parse-subcommand cmd --end-of-options subcmd-one 2>err &&
604 grep "^error: need a subcommand" err &&
605 grep ^usage: err
608 test_expect_success 'subcommand - unknown subcommand shows error and usage' '
609 test_expect_code 129 test-tool parse-subcommand cmd nope 2>err &&
610 grep "^error: unknown subcommand: \`nope$SQ" err &&
611 grep ^usage: err
614 test_expect_success 'subcommand - subcommands cannot be abbreviated' '
615 test_expect_code 129 test-tool parse-subcommand cmd subcmd-o 2>err &&
616 grep "^error: unknown subcommand: \`subcmd-o$SQ$" err &&
617 grep ^usage: err
620 test_expect_success 'subcommand - no negated subcommands' '
621 test_expect_code 129 test-tool parse-subcommand cmd no-subcmd-one 2>err &&
622 grep "^error: unknown subcommand: \`no-subcmd-one$SQ" err &&
623 grep ^usage: err
626 test_expect_success 'subcommand - simple' '
627 test-tool parse-subcommand cmd subcmd-two >actual &&
628 cat >expect <<-\EOF &&
629 opt: 0
630 fn: subcmd_two
631 arg 00: subcmd-two
633 test_cmp expect actual
636 test_expect_success 'subcommand - stop parsing at the first subcommand' '
637 test-tool parse-subcommand cmd --opt=1 subcmd-two subcmd-one --opt=2 >actual &&
638 cat >expect <<-\EOF &&
639 opt: 1
640 fn: subcmd_two
641 arg 00: subcmd-two
642 arg 01: subcmd-one
643 arg 02: --opt=2
645 test_cmp expect actual
648 test_expect_success 'subcommand - KEEP_ARGV0' '
649 test-tool parse-subcommand --keep-argv0 cmd subcmd-two >actual &&
650 cat >expect <<-\EOF &&
651 opt: 0
652 fn: subcmd_two
653 arg 00: cmd
654 arg 01: subcmd-two
656 test_cmp expect actual
659 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given' '
660 test-tool parse-subcommand --subcommand-optional cmd >actual &&
661 cat >expect <<-\EOF &&
662 opt: 0
663 fn: subcmd_one
665 test_cmp expect actual
668 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + given subcommand' '
669 test-tool parse-subcommand --subcommand-optional cmd subcmd-two branch file >actual &&
670 cat >expect <<-\EOF &&
671 opt: 0
672 fn: subcmd_two
673 arg 00: subcmd-two
674 arg 01: branch
675 arg 02: file
677 test_cmp expect actual
680 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown dashless args' '
681 test-tool parse-subcommand --subcommand-optional cmd branch file >actual &&
682 cat >expect <<-\EOF &&
683 opt: 0
684 fn: subcmd_one
685 arg 00: branch
686 arg 01: file
688 test_cmp expect actual
691 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown option' '
692 test_expect_code 129 test-tool parse-subcommand --subcommand-optional cmd --subcommand-opt 2>err &&
693 grep "^error: unknown option" err &&
694 grep ^usage: err
697 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand not given + unknown option' '
698 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt >actual &&
699 cat >expect <<-\EOF &&
700 opt: 0
701 fn: subcmd_one
702 arg 00: --subcommand-opt
704 test_cmp expect actual
707 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand ignored after unknown option' '
708 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt subcmd-two >actual &&
709 cat >expect <<-\EOF &&
710 opt: 0
711 fn: subcmd_one
712 arg 00: --subcommand-opt
713 arg 01: subcmd-two
715 test_cmp expect actual
718 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + command and subcommand options cannot be mixed' '
719 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt branch --opt=1 >actual &&
720 cat >expect <<-\EOF &&
721 opt: 0
722 fn: subcmd_one
723 arg 00: --subcommand-opt
724 arg 01: branch
725 arg 02: --opt=1
727 test_cmp expect actual
730 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_ARGV0' '
731 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-argv0 cmd --subcommand-opt branch >actual &&
732 cat >expect <<-\EOF &&
733 opt: 0
734 fn: subcmd_one
735 arg 00: cmd
736 arg 01: --subcommand-opt
737 arg 02: branch
739 test_cmp expect actual
742 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_DASHDASH' '
743 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-dashdash cmd -- --subcommand-opt file >actual &&
744 cat >expect <<-\EOF &&
745 opt: 0
746 fn: subcmd_one
747 arg 00: --
748 arg 01: --subcommand-opt
749 arg 02: file
751 test_cmp expect actual
754 test_expect_success 'subcommand - completion helper' '
755 test-tool parse-subcommand cmd --git-completion-helper >actual &&
756 echo "subcmd-one subcmd-two --opt= --no-opt" >expect &&
757 test_cmp expect actual
760 test_expect_success 'subcommands are incompatible with STOP_AT_NON_OPTION' '
761 test_must_fail test-tool parse-subcommand --stop-at-non-option cmd subcmd-one 2>err &&
762 grep ^BUG err
765 test_expect_success 'subcommands are incompatible with KEEP_UNKNOWN_OPT unless in combination with SUBCOMMAND_OPTIONAL' '
766 test_must_fail test-tool parse-subcommand --keep-unknown-opt cmd subcmd-two 2>err &&
767 grep ^BUG err
770 test_expect_success 'subcommands are incompatible with KEEP_DASHDASH unless in combination with SUBCOMMAND_OPTIONAL' '
771 test_must_fail test-tool parse-subcommand --keep-dashdash cmd subcmd-two 2>err &&
772 grep ^BUG err
775 test_expect_success 'negative magnitude' '
776 test_must_fail test-tool parse-options --magnitude -1 >out 2>err &&
777 grep "non-negative integer" err &&
778 test_must_be_empty out
781 test_expect_success 'magnitude with units but no numbers' '
782 test_must_fail test-tool parse-options --magnitude m >out 2>err &&
783 grep "non-negative integer" err &&
784 test_must_be_empty out
787 test_done