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 -L, --[no-
]length
<str
>
33 -F, --[no-
]file <file>
37 -s, --[no-
]string
<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
44 --[no-
]list
<str
> add str to list
47 -NUM set integer to NUM
49 --ambiguous positive ambiguity
50 --no-ambiguous negative ambiguity
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
61 -A, --[no-
]alias-source
<string
>
63 -Z, --[no-
]alias-target
<string
>
64 alias of
--alias-source
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
81 test-tool parse-options
--expect="$what $expect" "$@"
84 check_unknown_i18n
() {
87 echo error
: unknown option \
`${1#--}\' >expect ;;
89 echo error: unknown switch \`${1#-}\' >expect
;;
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
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'
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'
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'
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'
112 test_expect_success
'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
114 test_expect_success
'OPT_INT() negative' 'check integer: -2345 -i -2345'
116 test_expect_success
'OPT_MAGNITUDE() simple' '
117 check magnitude: 2345678 -m 2345678
120 test_expect_success
'OPT_MAGNITUDE() kilo' '
121 check magnitude: 239616 -m 234k
124 test_expect_success
'OPT_MAGNITUDE() mega' '
125 check magnitude: 104857600 -m 100m
128 test_expect_success
'OPT_MAGNITUDE() giga' '
129 check magnitude: 1073741824 -m 1g
132 test_expect_success
'OPT_MAGNITUDE() 3giga' '
133 check magnitude: 3221225472 -m 3g
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
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
178 test_expect_success
'missing required value' '
179 cat >expect <<-\EOF &&
180 error: switch `s'\'' requires a value
182 test_expect_code 129 test-tool parse-options -s 2>actual &&
183 test_cmp expect actual &&
185 cat >expect <<-\EOF &&
186 error: option `string'\'' requires a value
188 test_expect_code 129 test-tool parse-options --string 2>actual &&
189 test_cmp expect actual &&
191 cat >expect <<-\EOF &&
192 error: option `file'\'' requires a value
194 test_expect_code 129 test-tool parse-options --file 2>actual &&
195 test_cmp expect actual
198 test_expect_success
'superfluous value provided: boolean' '
199 cat >expect <<-\EOF &&
200 error: option `yes'\'' takes no value
202 test_expect_code 129 test-tool parse-options --yes=hi 2>actual &&
203 test_cmp expect actual &&
205 cat >expect <<-\EOF &&
206 error: option `no-yes'\'' takes no value
208 test_expect_code 129 test-tool parse-options --no-yes=hi 2>actual &&
209 test_cmp expect actual
212 test_expect_success
'superfluous value provided: cmdmode' '
213 cat >expect <<-\EOF &&
214 error: option `mode1'\'' takes no value
216 test_expect_code 129 test-tool parse-options --mode1=hi 2>actual &&
217 test_cmp expect actual
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
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
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
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
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
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
290 error
: did you mean
`--boolean` (with two dashes
)?
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
300 error
: did you mean
`--ambiguous` (with two dashes
)?
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
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
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
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
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
360 test_expect_success
'OPT_BIT() works' '
361 test-tool parse-options --expect="boolean: 6" -bb --or4
364 test_expect_success
'OPT_NEGBIT() works' '
365 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
368 test_expect_success
'OPT_CMDMODE() works' '
369 test-tool parse-options --expect="integer: 1" --mode1
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
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
384 test_expect_success
'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
385 test-tool parse-options --expect="boolean: 6" + + + + + +
388 test_expect_success
'OPT_NUMBER_CALLBACK() works' '
389 test-tool parse-options --expect="integer: 12345" -12345
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
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
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
428 test_expect_success
'multiple quiet levels' '
429 test-tool parse-options --expect="quiet: 3" -q -q -q
432 test_expect_success
'multiple verbose levels' '
433 test-tool parse-options --expect="verbose: 3" -v -v -v
436 test_expect_success
'--no-quiet sets --quiet to 0' '
437 test-tool parse-options --expect="quiet: 0" --no-quiet
440 test_expect_success
'--no-quiet resets multiple -q to 0' '
441 test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet
444 test_expect_success
'--no-verbose sets verbose to 0' '
445 test-tool parse-options --expect="verbose: 0" --no-verbose
448 test_expect_success
'--no-verbose resets multiple verbose to 0' '
449 test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose
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
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
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 &&
474 test_cmp expect actual
477 test_expect_success
'KEEP_ARGV0 works' '
478 test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual &&
479 cat >expect <<-\EOF &&
484 test_cmp expect actual
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 &&
495 test_cmp expect actual
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 &&
505 test_cmp expect actual
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 &&
514 for help_opt
in help help-all
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 &&
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 &&
531 test_cmp expect actual
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
570 test_expect_success
'subcommand - simple' '
571 test-tool parse-subcommand cmd subcmd-two >actual &&
572 cat >expect <<-\EOF &&
577 test_cmp expect actual
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 &&
589 test_cmp expect actual
592 test_expect_success
'subcommand - KEEP_ARGV0' '
593 test-tool parse-subcommand --keep-argv0 cmd subcmd-two >actual &&
594 cat >expect <<-\EOF &&
600 test_cmp expect actual
603 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given' '
604 test-tool parse-subcommand --subcommand-optional cmd >actual &&
605 cat >expect <<-\EOF &&
609 test_cmp expect actual
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 &&
621 test_cmp expect actual
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 &&
632 test_cmp expect actual
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 &&
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 &&
646 arg 00: --subcommand-opt
648 test_cmp expect actual
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 &&
656 arg 00: --subcommand-opt
659 test_cmp expect actual
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 &&
667 arg 00: --subcommand-opt
671 test_cmp expect actual
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 &&
680 arg 01: --subcommand-opt
683 test_cmp expect actual
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 &&
692 arg 01: --subcommand-opt
695 test_cmp expect actual
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
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 &&
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 &&
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 &&
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
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