3 # Copyright (c) 2007 Johannes Schindelin
6 test_description
='our own option parser'
8 TEST_PASSES_SANITIZE_LEAK
=true
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
>
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
>
34 -F, --[no-
]file <file>
38 -s, --[no-
]string
<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
45 --[no-
]list
<str
> add str to list
48 -NUM set integer to NUM
50 --ambiguous positive ambiguity
51 --no-ambiguous negative ambiguity
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
62 -A, --[no-
]alias-source
<string
>
64 -Z, --[no-
]alias-target
<string
>
65 alias of
--alias-source
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
82 test-tool parse-options
--expect="$what $expect" "$@"
85 check_unknown_i18n
() {
88 echo error
: unknown option \
`${1#--}\' >expect ;;
90 echo error: unknown switch \`${1#-}\' >expect
;;
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
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
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 &&
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
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
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
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
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
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
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
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
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
626 test_expect_success
'subcommand - simple' '
627 test-tool parse-subcommand cmd subcmd-two >actual &&
628 cat >expect <<-\EOF &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
712 arg 00: --subcommand-opt
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 &&
723 arg 00: --subcommand-opt
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 &&
736 arg 01: --subcommand-opt
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 &&
748 arg 01: --subcommand-opt
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 &&
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 &&
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 &&
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