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.
17 -D, --no-doubt begins with
'no-'
18 -B, --no-fear be brave
19 -b, --boolean increment by one
20 -4, --or4 bitwise-or boolean with ..
.0100
21 --neg-or4 same as
--no-or4
23 -i, --integer <n
> get a integer
24 -j <n
> get a integer
, too
25 -m, --magnitude <n
> get a magnitude
26 --set23 set integer to
23
27 --mode1 set integer to
1 (cmdmode option
)
28 --mode2 set integer to
2 (cmdmode option
)
29 -L, --length <str
> get length of
<str
>
30 -F, --file <file> set file to
<file>
33 -s, --string <string
> get a string
34 --string2 <str
> get another string
35 --st <st
> get another string
(pervert ordering
)
36 -o <str
> get another string
37 --longhelp help text of this entry
39 --list <str
> add str to list
42 -NUM set integer to NUM
44 --ambiguous positive ambiguity
45 --no-ambiguous negative ambiguity
48 --abbrev[=<n
>] use
<n
> digits to display object names
49 -v, --verbose be verbose
52 --expect <string
> expected output
in the variable dump
55 -A, --alias-source <string
>
57 -Z, --alias-target <string
>
58 alias of
--alias-source
62 test_expect_success
'test help' '
63 test_must_fail test-tool parse-options -h >output 2>output.err &&
64 test_must_be_empty output.err &&
65 test_cmp expect output
75 test-tool parse-options
--expect="$what $expect" "$@"
78 check_unknown_i18n
() {
81 echo error
: unknown option \
`${1#--}\' >expect ;;
83 echo error: unknown switch \`${1#-}\' >expect
;;
85 cat expect.err
>>expect
&&
86 test_must_fail test-tool parse-options $
* >output
2>output.err
&&
87 test_must_be_empty output
&&
88 test_cmp expect output.err
91 test_expect_success
'OPT_BOOL() #1' 'check boolean: 1 --yes'
92 test_expect_success
'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
93 test_expect_success
'OPT_BOOL() #3' 'check boolean: 1 -D'
94 test_expect_success
'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
95 test_expect_success
'OPT_BOOL() #5' 'check boolean: 1 -B'
97 test_expect_success
'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
98 test_expect_success
'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
100 test_expect_success
'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
101 test_expect_success
'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
103 test_expect_success
'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
104 test_expect_success
'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
106 test_expect_success
'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
108 test_expect_success
'OPT_INT() negative' 'check integer: -2345 -i -2345'
110 test_expect_success
'OPT_MAGNITUDE() simple' '
111 check magnitude: 2345678 -m 2345678
114 test_expect_success
'OPT_MAGNITUDE() kilo' '
115 check magnitude: 239616 -m 234k
118 test_expect_success
'OPT_MAGNITUDE() mega' '
119 check magnitude: 104857600 -m 100m
122 test_expect_success
'OPT_MAGNITUDE() giga' '
123 check magnitude: 1073741824 -m 1g
126 test_expect_success
'OPT_MAGNITUDE() 3giga' '
127 check magnitude: 3221225472 -m 3g
143 test_expect_success
'short options' '
144 test-tool parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
145 >output 2>output.err &&
146 test_cmp expect output &&
147 test_must_be_empty output.err
163 test_expect_success
'long options' '
164 test-tool parse-options --boolean --integer 1729 --magnitude 16k \
165 --boolean --string2=321 --verbose --verbose --no-dry-run \
166 --abbrev=10 --file fi.le --obsolete \
167 >output 2>output.err &&
168 test_must_be_empty output.err &&
169 test_cmp expect output
172 test_expect_success
'missing required value' '
173 cat >expect <<-\EOF &&
174 error: switch `s'\'' requires a value
176 test_expect_code 129 test-tool parse-options -s 2>actual &&
177 test_cmp expect actual &&
179 cat >expect <<-\EOF &&
180 error: option `string'\'' requires a value
182 test_expect_code 129 test-tool parse-options --string 2>actual &&
183 test_cmp expect actual &&
185 cat >expect <<-\EOF &&
186 error: option `file'\'' requires a value
188 test_expect_code 129 test-tool parse-options --file 2>actual &&
189 test_cmp expect actual
192 test_expect_success
'superfluous value provided: boolean' '
193 cat >expect <<-\EOF &&
194 error: option `yes'\'' takes no value
196 test_expect_code 129 test-tool parse-options --yes=hi 2>actual &&
197 test_cmp expect actual &&
199 cat >expect <<-\EOF &&
200 error: option `no-yes'\'' takes no value
202 test_expect_code 129 test-tool parse-options --no-yes=hi 2>actual &&
203 test_cmp expect actual
206 test_expect_success
'superfluous value provided: cmdmode' '
207 cat >expect <<-\EOF &&
208 error: option `mode1'\'' takes no value
210 test_expect_code 129 test-tool parse-options --mode1=hi 2>actual &&
211 test_cmp expect actual
230 test_expect_success
'intermingled arguments' '
231 test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
232 >output 2>output.err &&
233 test_must_be_empty output.err &&
234 test_cmp expect output
250 test_expect_success
'unambiguously abbreviated option' '
251 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
252 test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err &&
253 test_must_be_empty output.err &&
254 test_cmp expect output
257 test_expect_success
'unambiguously abbreviated option with "="' '
258 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
259 test-tool parse-options --expect="integer: 2" --int=2
262 test_expect_success
'ambiguously abbreviated option' '
263 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
264 test-tool parse-options --strin 123
267 test_expect_success
'non ambiguous option (after two options it abbreviates)' '
268 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
269 test-tool parse-options --expect="string: 123" --st 123
272 test_expect_success
'Alias options do not contribute to abbreviation' '
273 test-tool parse-options --alias-source 123 >output &&
274 grep "^string: 123" output &&
275 test-tool parse-options --alias-target 123 >output &&
276 grep "^string: 123" output &&
277 test_must_fail test-tool parse-options --alias &&
278 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
279 test-tool parse-options --alias 123 >output &&
280 grep "^string: 123" output
284 error
: did you mean
`--boolean` (with two dashes
)?
287 test_expect_success
'detect possible typos' '
288 test_must_fail test-tool parse-options -boolean >output 2>output.err &&
289 test_must_be_empty output &&
290 test_cmp typo.err output.err
294 error
: did you mean
`--ambiguous` (with two dashes
)?
297 test_expect_success
'detect possible typos' '
298 test_must_fail test-tool parse-options -ambiguous >output 2>output.err &&
299 test_must_be_empty output &&
300 test_cmp typo.err output.err
317 test_expect_success
'OPT_CALLBACK() and OPT_BIT() work' '
318 test-tool parse-options --length=four -b -4 >output 2>output.err &&
319 test_must_be_empty output.err &&
320 test_cmp expect output
323 test_expect_success
'OPT_CALLBACK() and callback errors work' '
324 test_must_fail test-tool parse-options --no-length >output 2>output.err &&
325 test_must_be_empty output &&
326 test_must_be_empty output.err
342 test_expect_success
'OPT_BIT() and OPT_SET_INT() work' '
343 test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
344 test_must_be_empty output.err &&
345 test_cmp expect output
348 test_expect_success
'OPT_NEGBIT() and OPT_SET_INT() work' '
349 test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
350 test_must_be_empty output.err &&
351 test_cmp expect output
354 test_expect_success
'OPT_BIT() works' '
355 test-tool parse-options --expect="boolean: 6" -bb --or4
358 test_expect_success
'OPT_NEGBIT() works' '
359 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
362 test_expect_success
'OPT_CMDMODE() works' '
363 test-tool parse-options --expect="integer: 1" --mode1
366 test_expect_success
'OPT_CMDMODE() detects incompatibility' '
367 test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err &&
368 test_must_be_empty output &&
369 test_i18ngrep "incompatible with --mode" output.err
372 test_expect_success
'OPT_CMDMODE() detects incompatibility with something else' '
373 test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err &&
374 test_must_be_empty output &&
375 test_i18ngrep "incompatible with something else" output.err
378 test_expect_success
'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
379 test-tool parse-options --expect="boolean: 6" + + + + + +
382 test_expect_success
'OPT_NUMBER_CALLBACK() works' '
383 test-tool parse-options --expect="integer: 12345" -12345
399 test_expect_success
'negation of OPT_NONEG flags is not ambiguous' '
400 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
401 test-tool parse-options --no-ambig >output 2>output.err &&
402 test_must_be_empty output.err &&
403 test_cmp expect output
411 test_expect_success
'--list keeps list of strings' '
412 test-tool parse-options --list foo --list=bar --list=baz >output &&
413 test_cmp expect output
416 test_expect_success
'--no-list resets list' '
417 test-tool parse-options --list=other --list=irrelevant --list=options \
418 --no-list --list=foo --list=bar --list=baz >output &&
419 test_cmp expect output
422 test_expect_success
'multiple quiet levels' '
423 test-tool parse-options --expect="quiet: 3" -q -q -q
426 test_expect_success
'multiple verbose levels' '
427 test-tool parse-options --expect="verbose: 3" -v -v -v
430 test_expect_success
'--no-quiet sets --quiet to 0' '
431 test-tool parse-options --expect="quiet: 0" --no-quiet
434 test_expect_success
'--no-quiet resets multiple -q to 0' '
435 test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet
438 test_expect_success
'--no-verbose sets verbose to 0' '
439 test-tool parse-options --expect="verbose: 0" --no-verbose
442 test_expect_success
'--no-verbose resets multiple verbose to 0' '
443 test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose
446 test_expect_success
'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' '
447 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
448 test-tool parse-options --ye &&
449 test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \
450 test-tool parse-options --ye
453 test_expect_success
'--end-of-options treats remainder as args' '
454 test-tool parse-options \
455 --expect="verbose: -1" \
456 --expect="arg 00: --verbose" \
457 --end-of-options --verbose
460 test_expect_success
'KEEP_DASHDASH works' '
461 test-tool parse-options-flags --keep-dashdash cmd --opt=1 -- --opt=2 --unknown >actual &&
462 cat >expect <<-\EOF &&
468 test_cmp expect actual
471 test_expect_success
'KEEP_ARGV0 works' '
472 test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual &&
473 cat >expect <<-\EOF &&
478 test_cmp expect actual
481 test_expect_success
'STOP_AT_NON_OPTION works' '
482 test-tool parse-options-flags --stop-at-non-option cmd --opt=4 arg0 --opt=5 --unknown >actual &&
483 cat >expect <<-\EOF &&
489 test_cmp expect actual
492 test_expect_success
'KEEP_UNKNOWN_OPT works' '
493 test-tool parse-options-flags --keep-unknown-opt cmd --unknown=1 --opt=6 -u2 >actual &&
494 cat >expect <<-\EOF &&
499 test_cmp expect actual
502 test_expect_success
'NO_INTERNAL_HELP works for -h' '
503 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd -h 2>err &&
504 grep "^error: unknown switch \`h$SQ" err &&
508 for help_opt
in help help-all
510 test_expect_success
"NO_INTERNAL_HELP works for --$help_opt" "
511 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd --$help_opt 2>err &&
512 grep '^error: unknown option \`'$help_opt\' err &&
517 test_expect_success
'KEEP_UNKNOWN_OPT | NO_INTERNAL_HELP works' '
518 test-tool parse-options-flags --keep-unknown-opt --no-internal-help cmd -h --help --help-all >actual &&
519 cat >expect <<-\EOF &&
525 test_cmp expect actual
528 test_expect_success
'subcommand - no subcommand shows error and usage' '
529 test_expect_code 129 test-tool parse-subcommand cmd 2>err &&
530 grep "^error: need a subcommand" err &&
534 test_expect_success
'subcommand - subcommand after -- shows error and usage' '
535 test_expect_code 129 test-tool parse-subcommand cmd -- subcmd-one 2>err &&
536 grep "^error: need a subcommand" err &&
540 test_expect_success
'subcommand - subcommand after --end-of-options shows error and usage' '
541 test_expect_code 129 test-tool parse-subcommand cmd --end-of-options subcmd-one 2>err &&
542 grep "^error: need a subcommand" err &&
546 test_expect_success
'subcommand - unknown subcommand shows error and usage' '
547 test_expect_code 129 test-tool parse-subcommand cmd nope 2>err &&
548 grep "^error: unknown subcommand: \`nope$SQ" err &&
552 test_expect_success
'subcommand - subcommands cannot be abbreviated' '
553 test_expect_code 129 test-tool parse-subcommand cmd subcmd-o 2>err &&
554 grep "^error: unknown subcommand: \`subcmd-o$SQ$" err &&
558 test_expect_success
'subcommand - no negated subcommands' '
559 test_expect_code 129 test-tool parse-subcommand cmd no-subcmd-one 2>err &&
560 grep "^error: unknown subcommand: \`no-subcmd-one$SQ" err &&
564 test_expect_success
'subcommand - simple' '
565 test-tool parse-subcommand cmd subcmd-two >actual &&
566 cat >expect <<-\EOF &&
571 test_cmp expect actual
574 test_expect_success
'subcommand - stop parsing at the first subcommand' '
575 test-tool parse-subcommand cmd --opt=1 subcmd-two subcmd-one --opt=2 >actual &&
576 cat >expect <<-\EOF &&
583 test_cmp expect actual
586 test_expect_success
'subcommand - KEEP_ARGV0' '
587 test-tool parse-subcommand --keep-argv0 cmd subcmd-two >actual &&
588 cat >expect <<-\EOF &&
594 test_cmp expect actual
597 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given' '
598 test-tool parse-subcommand --subcommand-optional cmd >actual &&
599 cat >expect <<-\EOF &&
603 test_cmp expect actual
606 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL + given subcommand' '
607 test-tool parse-subcommand --subcommand-optional cmd subcmd-two branch file >actual &&
608 cat >expect <<-\EOF &&
615 test_cmp expect actual
618 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown dashless args' '
619 test-tool parse-subcommand --subcommand-optional cmd branch file >actual &&
620 cat >expect <<-\EOF &&
626 test_cmp expect actual
629 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown option' '
630 test_expect_code 129 test-tool parse-subcommand --subcommand-optional cmd --subcommand-opt 2>err &&
631 grep "^error: unknown option" err &&
635 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand not given + unknown option' '
636 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt >actual &&
637 cat >expect <<-\EOF &&
640 arg 00: --subcommand-opt
642 test_cmp expect actual
645 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand ignored after unknown option' '
646 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt subcmd-two >actual &&
647 cat >expect <<-\EOF &&
650 arg 00: --subcommand-opt
653 test_cmp expect actual
656 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + command and subcommand options cannot be mixed' '
657 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt branch --opt=1 >actual &&
658 cat >expect <<-\EOF &&
661 arg 00: --subcommand-opt
665 test_cmp expect actual
668 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_ARGV0' '
669 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-argv0 cmd --subcommand-opt branch >actual &&
670 cat >expect <<-\EOF &&
674 arg 01: --subcommand-opt
677 test_cmp expect actual
680 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_DASHDASH' '
681 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-dashdash cmd -- --subcommand-opt file >actual &&
682 cat >expect <<-\EOF &&
686 arg 01: --subcommand-opt
689 test_cmp expect actual
692 test_expect_success
'subcommand - completion helper' '
693 test-tool parse-subcommand cmd --git-completion-helper >actual &&
694 echo "subcmd-one subcmd-two --opt= --no-opt" >expect &&
695 test_cmp expect actual
698 test_expect_success
'subcommands are incompatible with STOP_AT_NON_OPTION' '
699 test_must_fail test-tool parse-subcommand --stop-at-non-option cmd subcmd-one 2>err &&
703 test_expect_success
'subcommands are incompatible with KEEP_UNKNOWN_OPT unless in combination with SUBCOMMAND_OPTIONAL' '
704 test_must_fail test-tool parse-subcommand --keep-unknown-opt cmd subcmd-two 2>err &&
708 test_expect_success
'subcommands are incompatible with KEEP_DASHDASH unless in combination with SUBCOMMAND_OPTIONAL' '
709 test_must_fail test-tool parse-subcommand --keep-dashdash cmd subcmd-two 2>err &&
713 test_expect_success
'negative magnitude' '
714 test_must_fail test-tool parse-options --magnitude -1 >out 2>err &&
715 grep "non-negative integer" err &&
716 test_must_be_empty out
719 test_expect_success
'magnitude with units but no numbers' '
720 test_must_fail test-tool parse-options --magnitude m >out 2>err &&
721 grep "non-negative integer" err &&
722 test_must_be_empty out