3 # Copyright (c) 2007 Johannes Schindelin
6 test_description
='our own option parser'
11 usage
: test-tool parse-options
<options
>
13 A helper
function for the parse-options API.
15 --[no-
]yes get a boolean
16 -D, --no-doubt begins with
'no-'
17 --doubt opposite of
--no-doubt
18 -B, --no-fear be brave
19 -b, --[no-
]boolean increment by one
20 -4, --[no-
]or4 bitwise-or boolean with ..
.0100
21 --[no-
]neg-or4 same as
--no-or4
23 -i, --[no-
]integer
<n
>
25 --[no-
]i16
<n
> get a
16 bit integer
26 -j <n
> get a integer
, too
27 -u, --unsigned <n
> get an unsigned integer
28 --u16 <n
> get a
16 bit unsigned integer
29 --[no-
]set23
set integer to
23
30 --mode1 set integer to
1 (cmdmode option
)
31 --mode2 set integer to
2 (cmdmode option
)
32 --[no-
]mode34
(3|
4) set integer to
3 or
4 (cmdmode option
)
33 -L, --[no-
]length
<str
>
35 -F, --[no-
]file <file>
39 -s, --[no-
]string
<string
>
41 --[no-
]string2
<str
> get another string
42 --[no-
]st
<st
> get another string
(pervert ordering
)
43 -o <str
> get another string
44 --longhelp help text of this entry
46 --[no-
]list
<str
> add str to list
49 -NUM set integer to NUM
51 --ambiguous positive ambiguity
52 --no-ambiguous negative ambiguity
55 --[no-
]abbrev
[=<n
>] use
<n
> digits to display object names
56 -v, --[no-
]verbose be verbose
57 -n, --[no-
]dry-run dry run
58 -q, --[no-
]quiet be quiet
59 --[no-
]expect
<string
>
60 expected output
in the variable dump
63 -A, --[no-
]alias-source
<string
>
65 -Z, --[no-
]alias-target
<string
>
66 alias of
--alias-source
70 test_expect_success
'test help' '
71 test_must_fail test-tool parse-options -h >output 2>output.err &&
72 test_must_be_empty output.err &&
73 test_cmp expect output
83 test-tool parse-options
--expect="$what $expect" "$@"
86 check_unknown_i18n
() {
89 echo error
: unknown option \
`${1#--}\' >expect ;;
91 echo error: unknown switch \`${1#-}\' >expect
;;
93 cat expect.err
>>expect
&&
94 test_must_fail test-tool parse-options $
* >output
2>output.err
&&
95 test_must_be_empty output
&&
96 test_cmp expect output.err
99 test_expect_success
'OPT_BOOL() #1' 'check boolean: 1 --yes'
100 test_expect_success
'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
101 test_expect_success
'OPT_BOOL() #3' 'check boolean: 1 -D'
102 test_expect_success
'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
103 test_expect_success
'OPT_BOOL() #5' 'check boolean: 1 -B'
105 test_expect_success
'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
106 test_expect_success
'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
108 test_expect_success
'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
109 test_expect_success
'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
111 test_expect_success
'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
112 test_expect_success
'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
114 test_expect_success
'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
116 test_expect_success
'OPT_INTEGER() negative' 'check integer: -2345 -i -2345'
117 test_expect_success
'OPT_INTEGER() kilo' 'check integer: 239616 -i 234k'
118 test_expect_success
'OPT_INTEGER() negative kilo' 'check integer: -239616 -i -234k'
120 test_expect_success
'OPT_UNSIGNED() simple' '
121 check unsigned: 2345678 -u 2345678
124 test_expect_success
'OPT_UNSIGNED() kilo' '
125 check unsigned: 239616 -u 234k
128 test_expect_success
'OPT_UNSIGNED() mega' '
129 check unsigned: 104857600 -u 100m
132 test_expect_success
'OPT_UNSIGNED() giga' '
133 check unsigned: 1073741824 -u 1g
136 test_expect_success
'OPT_UNSIGNED() 3giga' '
137 check unsigned: 3221225472 -u 3g
155 test_expect_success
'short options' '
156 test-tool parse-options -s123 -b -i 1729 -u 16k -b -vv -n -F my.file \
157 >output 2>output.err &&
158 test_cmp expect output &&
159 test_must_be_empty output.err
177 test_expect_success
'long options' '
178 test-tool parse-options --boolean --integer 1729 --i16 9000 --unsigned 16k \
179 --u16 32k --boolean --string2=321 --verbose --verbose --no-dry-run \
180 --abbrev=10 --file fi.le --obsolete \
181 >output 2>output.err &&
182 test_must_be_empty output.err &&
183 test_cmp expect output
186 test_expect_success
'abbreviate to something longer than SHA1 length' '
187 cat >expect <<-EOF &&
201 test-tool parse-options --abbrev=100 >output &&
202 test_cmp expect output
205 test_expect_success
'missing required value' '
206 cat >expect <<-\EOF &&
207 error: switch `s'\'' requires a value
209 test_expect_code 129 test-tool parse-options -s 2>actual &&
210 test_cmp expect actual &&
212 cat >expect <<-\EOF &&
213 error: option `string'\'' requires a value
215 test_expect_code 129 test-tool parse-options --string 2>actual &&
216 test_cmp expect actual &&
218 cat >expect <<-\EOF &&
219 error: option `file'\'' requires a value
221 test_expect_code 129 test-tool parse-options --file 2>actual &&
222 test_cmp expect actual
225 test_expect_success
'superfluous value provided: boolean' '
226 cat >expect <<-\EOF &&
227 error: option `yes'\'' takes no value
229 test_expect_code 129 test-tool parse-options --yes=hi 2>actual &&
230 test_cmp expect actual &&
232 cat >expect <<-\EOF &&
233 error: option `no-yes'\'' takes no value
235 test_expect_code 129 test-tool parse-options --no-yes=hi 2>actual &&
236 test_cmp expect actual
239 test_expect_success
'superfluous value provided: boolean, abbreviated' '
240 cat >expect <<-\EOF &&
241 error: option `yes'\'' takes no value
243 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
244 test-tool parse-options --ye=hi 2>actual &&
245 test_cmp expect actual &&
247 cat >expect <<-\EOF &&
248 error: option `no-yes'\'' takes no value
250 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
251 test-tool parse-options --no-ye=hi 2>actual &&
252 test_cmp expect actual
255 test_expect_success
'superfluous value provided: cmdmode' '
256 cat >expect <<-\EOF &&
257 error: option `mode1'\'' takes no value
259 test_expect_code 129 test-tool parse-options --mode1=hi 2>actual &&
260 test_cmp expect actual
281 test_expect_success
'intermingled arguments' '
282 test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
283 >output 2>output.err &&
284 test_must_be_empty output.err &&
285 test_cmp expect output
303 test_expect_success
'unambiguously abbreviated option' '
304 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
305 test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err &&
306 test_must_be_empty output.err &&
307 test_cmp expect output
310 test_expect_success
'unambiguously abbreviated option with "="' '
311 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
312 test-tool parse-options --expect="integer: 2" --int=2
315 test_expect_success
'ambiguously abbreviated option' '
316 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
317 test-tool parse-options --strin 123
320 test_expect_success
'non ambiguous option (after two options it abbreviates)' '
321 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
322 test-tool parse-options --expect="string: 123" --st 123
325 test_expect_success
'Alias options do not contribute to abbreviation' '
326 test-tool parse-options --alias-source 123 >output &&
327 grep "^string: 123" output &&
328 test-tool parse-options --alias-target 123 >output &&
329 grep "^string: 123" output &&
330 test_must_fail test-tool parse-options --alias &&
331 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
332 test-tool parse-options --alias 123 >output &&
333 grep "^string: 123" output
337 error
: did you mean
`--boolean` (with two dashes
)?
340 test_expect_success
'detect possible typos' '
341 test_must_fail test-tool parse-options -boolean >output 2>output.err &&
342 test_must_be_empty output &&
343 test_cmp typo.err output.err
347 error
: did you mean
`--ambiguous` (with two dashes
)?
350 test_expect_success
'detect possible typos' '
351 test_must_fail test-tool parse-options -ambiguous >output 2>output.err &&
352 test_must_be_empty output &&
353 test_cmp typo.err output.err
372 test_expect_success
'OPT_CALLBACK() and OPT_BIT() work' '
373 test-tool parse-options --length=four -b -4 >output 2>output.err &&
374 test_must_be_empty output.err &&
375 test_cmp expect output
378 test_expect_success
'OPT_CALLBACK() and callback errors work' '
379 test_must_fail test-tool parse-options --no-length >output 2>output.err &&
380 test_must_be_empty output &&
381 test_must_be_empty output.err
399 test_expect_success
'OPT_BIT() and OPT_SET_INT() work' '
400 test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
401 test_must_be_empty output.err &&
402 test_cmp expect output
405 test_expect_success
'OPT_NEGBIT() and OPT_SET_INT() work' '
406 test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
407 test_must_be_empty output.err &&
408 test_cmp expect output
411 test_expect_success
'OPT_BIT() works' '
412 test-tool parse-options --expect="boolean: 6" -bb --or4
415 test_expect_success
'OPT_NEGBIT() works' '
416 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
419 test_expect_success
'OPT_CMDMODE() works' '
420 test-tool parse-options --expect="integer: 1" --mode1 &&
421 test-tool parse-options --expect="integer: 3" --mode34=3
424 test_expect_success
'OPT_CMDMODE() detects incompatibility (1)' '
425 test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err &&
426 test_must_be_empty output &&
427 test_grep "mode1" output.err &&
428 test_grep "mode2" output.err &&
429 test_grep "cannot be used together" output.err
432 test_expect_success
'OPT_CMDMODE() detects incompatibility (2)' '
433 test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err &&
434 test_must_be_empty output &&
435 test_grep "mode2" output.err &&
436 test_grep "set23" output.err &&
437 test_grep "cannot be used together" output.err
440 test_expect_success
'OPT_CMDMODE() detects incompatibility (3)' '
441 test_must_fail test-tool parse-options --mode2 --set23 >output 2>output.err &&
442 test_must_be_empty output &&
443 test_grep "mode2" output.err &&
444 test_grep "set23" output.err &&
445 test_grep "cannot be used together" output.err
448 test_expect_success
'OPT_CMDMODE() detects incompatibility (4)' '
449 test_must_fail test-tool parse-options --mode2 --mode34=3 \
450 >output 2>output.err &&
451 test_must_be_empty output &&
452 test_grep "mode2" output.err &&
453 test_grep "mode34.3" output.err &&
454 test_grep "cannot be used together" output.err
457 test_expect_success
'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
458 test-tool parse-options --expect="boolean: 6" + + + + + +
461 test_expect_success
'OPT_NUMBER_CALLBACK() works' '
462 test-tool parse-options --expect="integer: 12345" -12345
480 test_expect_success
'negation of OPT_NONEG flags is not ambiguous' '
481 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
482 test-tool parse-options --no-ambig >output 2>output.err &&
483 test_must_be_empty output.err &&
484 test_cmp expect output
492 test_expect_success
'--list keeps list of strings' '
493 test-tool parse-options --list foo --list=bar --list=baz >output &&
494 test_cmp expect output
497 test_expect_success
'--no-list resets list' '
498 test-tool parse-options --list=other --list=irrelevant --list=options \
499 --no-list --list=foo --list=bar --list=baz >output &&
500 test_cmp expect output
503 test_expect_success
'multiple quiet levels' '
504 test-tool parse-options --expect="quiet: 3" -q -q -q
507 test_expect_success
'multiple verbose levels' '
508 test-tool parse-options --expect="verbose: 3" -v -v -v
511 test_expect_success
'--no-quiet sets --quiet to 0' '
512 test-tool parse-options --expect="quiet: 0" --no-quiet
515 test_expect_success
'--no-quiet resets multiple -q to 0' '
516 test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet
519 test_expect_success
'--no-verbose sets verbose to 0' '
520 test-tool parse-options --expect="verbose: 0" --no-verbose
523 test_expect_success
'--no-verbose resets multiple verbose to 0' '
524 test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose
527 test_expect_success
'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' '
528 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
529 test-tool parse-options --ye &&
530 test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \
531 test-tool parse-options --ye
534 test_expect_success
'--end-of-options treats remainder as args' '
535 test-tool parse-options \
536 --expect="verbose: -1" \
537 --expect="arg 00: --verbose" \
538 --end-of-options --verbose
541 test_expect_success
'KEEP_DASHDASH works' '
542 test-tool parse-options-flags --keep-dashdash cmd --opt=1 -- --opt=2 --unknown >actual &&
543 cat >expect <<-\EOF &&
549 test_cmp expect actual
552 test_expect_success
'KEEP_ARGV0 works' '
553 test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual &&
554 cat >expect <<-\EOF &&
559 test_cmp expect actual
562 test_expect_success
'STOP_AT_NON_OPTION works' '
563 test-tool parse-options-flags --stop-at-non-option cmd --opt=4 arg0 --opt=5 --unknown >actual &&
564 cat >expect <<-\EOF &&
570 test_cmp expect actual
573 test_expect_success
'KEEP_UNKNOWN_OPT works' '
574 test-tool parse-options-flags --keep-unknown-opt cmd --unknown=1 --opt=6 -u2 >actual &&
575 cat >expect <<-\EOF &&
580 test_cmp expect actual
583 test_expect_success
'NO_INTERNAL_HELP works for -h' '
584 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd -h 2>err &&
585 grep "^error: unknown switch \`h$SQ" err &&
589 for help_opt
in help help-all
591 test_expect_success
"NO_INTERNAL_HELP works for --$help_opt" "
592 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd --$help_opt 2>err &&
593 grep '^error: unknown option \`'$help_opt\' err &&
598 test_expect_success
'KEEP_UNKNOWN_OPT | NO_INTERNAL_HELP works' '
599 test-tool parse-options-flags --keep-unknown-opt --no-internal-help cmd -h --help --help-all >actual &&
600 cat >expect <<-\EOF &&
606 test_cmp expect actual
609 test_expect_success
'subcommand - no subcommand shows error and usage' '
610 test_expect_code 129 test-tool parse-subcommand cmd 2>err &&
611 grep "^error: need a subcommand" err &&
615 test_expect_success
'subcommand - subcommand after -- shows error and usage' '
616 test_expect_code 129 test-tool parse-subcommand cmd -- subcmd-one 2>err &&
617 grep "^error: need a subcommand" err &&
621 test_expect_success
'subcommand - subcommand after --end-of-options shows error and usage' '
622 test_expect_code 129 test-tool parse-subcommand cmd --end-of-options subcmd-one 2>err &&
623 grep "^error: need a subcommand" err &&
627 test_expect_success
'subcommand - unknown subcommand shows error and usage' '
628 test_expect_code 129 test-tool parse-subcommand cmd nope 2>err &&
629 grep "^error: unknown subcommand: \`nope$SQ" err &&
633 test_expect_success
'subcommand - subcommands cannot be abbreviated' '
634 test_expect_code 129 test-tool parse-subcommand cmd subcmd-o 2>err &&
635 grep "^error: unknown subcommand: \`subcmd-o$SQ$" err &&
639 test_expect_success
'subcommand - no negated subcommands' '
640 test_expect_code 129 test-tool parse-subcommand cmd no-subcmd-one 2>err &&
641 grep "^error: unknown subcommand: \`no-subcmd-one$SQ" err &&
645 test_expect_success
'subcommand - simple' '
646 test-tool parse-subcommand cmd subcmd-two >actual &&
647 cat >expect <<-\EOF &&
652 test_cmp expect actual
655 test_expect_success
'subcommand - stop parsing at the first subcommand' '
656 test-tool parse-subcommand cmd --opt=1 subcmd-two subcmd-one --opt=2 >actual &&
657 cat >expect <<-\EOF &&
664 test_cmp expect actual
667 test_expect_success
'subcommand - KEEP_ARGV0' '
668 test-tool parse-subcommand --keep-argv0 cmd subcmd-two >actual &&
669 cat >expect <<-\EOF &&
675 test_cmp expect actual
678 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given' '
679 test-tool parse-subcommand --subcommand-optional cmd >actual &&
680 cat >expect <<-\EOF &&
684 test_cmp expect actual
687 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL + given subcommand' '
688 test-tool parse-subcommand --subcommand-optional cmd subcmd-two branch file >actual &&
689 cat >expect <<-\EOF &&
696 test_cmp expect actual
699 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown dashless args' '
700 test-tool parse-subcommand --subcommand-optional cmd branch file >actual &&
701 cat >expect <<-\EOF &&
707 test_cmp expect actual
710 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown option' '
711 test_expect_code 129 test-tool parse-subcommand --subcommand-optional cmd --subcommand-opt 2>err &&
712 grep "^error: unknown option" err &&
716 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand not given + unknown option' '
717 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt >actual &&
718 cat >expect <<-\EOF &&
721 arg 00: --subcommand-opt
723 test_cmp expect actual
726 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand ignored after unknown option' '
727 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt subcmd-two >actual &&
728 cat >expect <<-\EOF &&
731 arg 00: --subcommand-opt
734 test_cmp expect actual
737 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + command and subcommand options cannot be mixed' '
738 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt branch --opt=1 >actual &&
739 cat >expect <<-\EOF &&
742 arg 00: --subcommand-opt
746 test_cmp expect actual
749 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_ARGV0' '
750 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-argv0 cmd --subcommand-opt branch >actual &&
751 cat >expect <<-\EOF &&
755 arg 01: --subcommand-opt
758 test_cmp expect actual
761 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_DASHDASH' '
762 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-dashdash cmd -- --subcommand-opt file >actual &&
763 cat >expect <<-\EOF &&
767 arg 01: --subcommand-opt
770 test_cmp expect actual
773 test_expect_success
'subcommand - completion helper' '
774 test-tool parse-subcommand cmd --git-completion-helper >actual &&
775 echo "subcmd-one subcmd-two --opt= --no-opt" >expect &&
776 test_cmp expect actual
779 test_expect_success
'subcommands are incompatible with STOP_AT_NON_OPTION' '
780 test_must_fail test-tool parse-subcommand --stop-at-non-option cmd subcmd-one 2>err &&
784 test_expect_success
'subcommands are incompatible with KEEP_UNKNOWN_OPT unless in combination with SUBCOMMAND_OPTIONAL' '
785 test_must_fail test-tool parse-subcommand --keep-unknown-opt cmd subcmd-two 2>err &&
789 test_expect_success
'subcommands are incompatible with KEEP_DASHDASH unless in combination with SUBCOMMAND_OPTIONAL' '
790 test_must_fail test-tool parse-subcommand --keep-dashdash cmd subcmd-two 2>err &&
794 test_expect_success
'negative unsigned' '
795 test_must_fail test-tool parse-options --unsigned -1 >out 2>err &&
796 grep "non-negative integer" err &&
797 test_must_be_empty out
800 test_expect_success
'unsigned with units but no numbers' '
801 test_must_fail test-tool parse-options --unsigned m >out 2>err &&
802 grep "non-negative integer" err &&
803 test_must_be_empty out
806 test_expect_success
'i16 limits range' '
807 test-tool parse-options --i16 32767 >out &&
808 test_grep "i16: 32767" out &&
809 test_must_fail test-tool parse-options --i16 32768 2>err &&
810 test_grep "value 32768 for option .i16. not in range \[-32768,32767\]" err &&
812 test-tool parse-options --i16 -32768 >out &&
813 test_grep "i16: -32768" out &&
814 test_must_fail test-tool parse-options --i16 -32769 2>err &&
815 test_grep "value -32769 for option .i16. not in range \[-32768,32767\]" err
818 test_expect_success
'u16 limits range' '
819 test-tool parse-options --u16 65535 >out &&
820 test_grep "u16: 65535" out &&
821 test_must_fail test-tool parse-options --u16 65536 2>err &&
822 test_grep "value 65536 for option .u16. not in range \[0,65535\]" err