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
'missing required value' '
180 cat >expect <<-\EOF &&
181 error: switch `s'\'' requires a value
183 test_expect_code 129 test-tool parse-options -s 2>actual &&
184 test_cmp expect actual &&
186 cat >expect <<-\EOF &&
187 error: option `string'\'' requires a value
189 test_expect_code 129 test-tool parse-options --string 2>actual &&
190 test_cmp expect actual &&
192 cat >expect <<-\EOF &&
193 error: option `file'\'' requires a value
195 test_expect_code 129 test-tool parse-options --file 2>actual &&
196 test_cmp expect actual
199 test_expect_success
'superfluous value provided: boolean' '
200 cat >expect <<-\EOF &&
201 error: option `yes'\'' takes no value
203 test_expect_code 129 test-tool parse-options --yes=hi 2>actual &&
204 test_cmp expect actual &&
206 cat >expect <<-\EOF &&
207 error: option `no-yes'\'' takes no value
209 test_expect_code 129 test-tool parse-options --no-yes=hi 2>actual &&
210 test_cmp expect actual
213 test_expect_success
'superfluous value provided: cmdmode' '
214 cat >expect <<-\EOF &&
215 error: option `mode1'\'' takes no value
217 test_expect_code 129 test-tool parse-options --mode1=hi 2>actual &&
218 test_cmp expect actual
237 test_expect_success
'intermingled arguments' '
238 test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
239 >output 2>output.err &&
240 test_must_be_empty output.err &&
241 test_cmp expect output
257 test_expect_success
'unambiguously abbreviated option' '
258 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
259 test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err &&
260 test_must_be_empty output.err &&
261 test_cmp expect output
264 test_expect_success
'unambiguously abbreviated option with "="' '
265 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
266 test-tool parse-options --expect="integer: 2" --int=2
269 test_expect_success
'ambiguously abbreviated option' '
270 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
271 test-tool parse-options --strin 123
274 test_expect_success
'non ambiguous option (after two options it abbreviates)' '
275 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
276 test-tool parse-options --expect="string: 123" --st 123
279 test_expect_success
'Alias options do not contribute to abbreviation' '
280 test-tool parse-options --alias-source 123 >output &&
281 grep "^string: 123" output &&
282 test-tool parse-options --alias-target 123 >output &&
283 grep "^string: 123" output &&
284 test_must_fail test-tool parse-options --alias &&
285 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
286 test-tool parse-options --alias 123 >output &&
287 grep "^string: 123" output
291 error
: did you mean
`--boolean` (with two dashes
)?
294 test_expect_success
'detect possible typos' '
295 test_must_fail test-tool parse-options -boolean >output 2>output.err &&
296 test_must_be_empty output &&
297 test_cmp typo.err output.err
301 error
: did you mean
`--ambiguous` (with two dashes
)?
304 test_expect_success
'detect possible typos' '
305 test_must_fail test-tool parse-options -ambiguous >output 2>output.err &&
306 test_must_be_empty output &&
307 test_cmp typo.err output.err
324 test_expect_success
'OPT_CALLBACK() and OPT_BIT() work' '
325 test-tool parse-options --length=four -b -4 >output 2>output.err &&
326 test_must_be_empty output.err &&
327 test_cmp expect output
330 test_expect_success
'OPT_CALLBACK() and callback errors work' '
331 test_must_fail test-tool parse-options --no-length >output 2>output.err &&
332 test_must_be_empty output &&
333 test_must_be_empty output.err
349 test_expect_success
'OPT_BIT() and OPT_SET_INT() work' '
350 test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
351 test_must_be_empty output.err &&
352 test_cmp expect output
355 test_expect_success
'OPT_NEGBIT() and OPT_SET_INT() work' '
356 test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
357 test_must_be_empty output.err &&
358 test_cmp expect output
361 test_expect_success
'OPT_BIT() works' '
362 test-tool parse-options --expect="boolean: 6" -bb --or4
365 test_expect_success
'OPT_NEGBIT() works' '
366 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
369 test_expect_success
'OPT_CMDMODE() works' '
370 test-tool parse-options --expect="integer: 1" --mode1 &&
371 test-tool parse-options --expect="integer: 3" --mode34=3
374 test_expect_success
'OPT_CMDMODE() detects incompatibility (1)' '
375 test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err &&
376 test_must_be_empty output &&
377 test_grep "mode1" output.err &&
378 test_grep "mode2" output.err &&
379 test_grep "cannot be used together" output.err
382 test_expect_success
'OPT_CMDMODE() detects incompatibility (2)' '
383 test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err &&
384 test_must_be_empty output &&
385 test_grep "mode2" output.err &&
386 test_grep "set23" output.err &&
387 test_grep "cannot be used together" output.err
390 test_expect_success
'OPT_CMDMODE() detects incompatibility (3)' '
391 test_must_fail test-tool parse-options --mode2 --set23 >output 2>output.err &&
392 test_must_be_empty output &&
393 test_grep "mode2" output.err &&
394 test_grep "set23" output.err &&
395 test_grep "cannot be used together" output.err
398 test_expect_success
'OPT_CMDMODE() detects incompatibility (4)' '
399 test_must_fail test-tool parse-options --mode2 --mode34=3 \
400 >output 2>output.err &&
401 test_must_be_empty output &&
402 test_grep "mode2" output.err &&
403 test_grep "mode34.3" output.err &&
404 test_grep "cannot be used together" output.err
407 test_expect_success
'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
408 test-tool parse-options --expect="boolean: 6" + + + + + +
411 test_expect_success
'OPT_NUMBER_CALLBACK() works' '
412 test-tool parse-options --expect="integer: 12345" -12345
428 test_expect_success
'negation of OPT_NONEG flags is not ambiguous' '
429 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
430 test-tool parse-options --no-ambig >output 2>output.err &&
431 test_must_be_empty output.err &&
432 test_cmp expect output
440 test_expect_success
'--list keeps list of strings' '
441 test-tool parse-options --list foo --list=bar --list=baz >output &&
442 test_cmp expect output
445 test_expect_success
'--no-list resets list' '
446 test-tool parse-options --list=other --list=irrelevant --list=options \
447 --no-list --list=foo --list=bar --list=baz >output &&
448 test_cmp expect output
451 test_expect_success
'multiple quiet levels' '
452 test-tool parse-options --expect="quiet: 3" -q -q -q
455 test_expect_success
'multiple verbose levels' '
456 test-tool parse-options --expect="verbose: 3" -v -v -v
459 test_expect_success
'--no-quiet sets --quiet to 0' '
460 test-tool parse-options --expect="quiet: 0" --no-quiet
463 test_expect_success
'--no-quiet resets multiple -q to 0' '
464 test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet
467 test_expect_success
'--no-verbose sets verbose to 0' '
468 test-tool parse-options --expect="verbose: 0" --no-verbose
471 test_expect_success
'--no-verbose resets multiple verbose to 0' '
472 test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose
475 test_expect_success
'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' '
476 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
477 test-tool parse-options --ye &&
478 test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \
479 test-tool parse-options --ye
482 test_expect_success
'--end-of-options treats remainder as args' '
483 test-tool parse-options \
484 --expect="verbose: -1" \
485 --expect="arg 00: --verbose" \
486 --end-of-options --verbose
489 test_expect_success
'KEEP_DASHDASH works' '
490 test-tool parse-options-flags --keep-dashdash cmd --opt=1 -- --opt=2 --unknown >actual &&
491 cat >expect <<-\EOF &&
497 test_cmp expect actual
500 test_expect_success
'KEEP_ARGV0 works' '
501 test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual &&
502 cat >expect <<-\EOF &&
507 test_cmp expect actual
510 test_expect_success
'STOP_AT_NON_OPTION works' '
511 test-tool parse-options-flags --stop-at-non-option cmd --opt=4 arg0 --opt=5 --unknown >actual &&
512 cat >expect <<-\EOF &&
518 test_cmp expect actual
521 test_expect_success
'KEEP_UNKNOWN_OPT works' '
522 test-tool parse-options-flags --keep-unknown-opt cmd --unknown=1 --opt=6 -u2 >actual &&
523 cat >expect <<-\EOF &&
528 test_cmp expect actual
531 test_expect_success
'NO_INTERNAL_HELP works for -h' '
532 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd -h 2>err &&
533 grep "^error: unknown switch \`h$SQ" err &&
537 for help_opt
in help help-all
539 test_expect_success
"NO_INTERNAL_HELP works for --$help_opt" "
540 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd --$help_opt 2>err &&
541 grep '^error: unknown option \`'$help_opt\' err &&
546 test_expect_success
'KEEP_UNKNOWN_OPT | NO_INTERNAL_HELP works' '
547 test-tool parse-options-flags --keep-unknown-opt --no-internal-help cmd -h --help --help-all >actual &&
548 cat >expect <<-\EOF &&
554 test_cmp expect actual
557 test_expect_success
'subcommand - no subcommand shows error and usage' '
558 test_expect_code 129 test-tool parse-subcommand cmd 2>err &&
559 grep "^error: need a subcommand" err &&
563 test_expect_success
'subcommand - subcommand after -- shows error and usage' '
564 test_expect_code 129 test-tool parse-subcommand cmd -- subcmd-one 2>err &&
565 grep "^error: need a subcommand" err &&
569 test_expect_success
'subcommand - subcommand after --end-of-options shows error and usage' '
570 test_expect_code 129 test-tool parse-subcommand cmd --end-of-options subcmd-one 2>err &&
571 grep "^error: need a subcommand" err &&
575 test_expect_success
'subcommand - unknown subcommand shows error and usage' '
576 test_expect_code 129 test-tool parse-subcommand cmd nope 2>err &&
577 grep "^error: unknown subcommand: \`nope$SQ" err &&
581 test_expect_success
'subcommand - subcommands cannot be abbreviated' '
582 test_expect_code 129 test-tool parse-subcommand cmd subcmd-o 2>err &&
583 grep "^error: unknown subcommand: \`subcmd-o$SQ$" err &&
587 test_expect_success
'subcommand - no negated subcommands' '
588 test_expect_code 129 test-tool parse-subcommand cmd no-subcmd-one 2>err &&
589 grep "^error: unknown subcommand: \`no-subcmd-one$SQ" err &&
593 test_expect_success
'subcommand - simple' '
594 test-tool parse-subcommand cmd subcmd-two >actual &&
595 cat >expect <<-\EOF &&
600 test_cmp expect actual
603 test_expect_success
'subcommand - stop parsing at the first subcommand' '
604 test-tool parse-subcommand cmd --opt=1 subcmd-two subcmd-one --opt=2 >actual &&
605 cat >expect <<-\EOF &&
612 test_cmp expect actual
615 test_expect_success
'subcommand - KEEP_ARGV0' '
616 test-tool parse-subcommand --keep-argv0 cmd subcmd-two >actual &&
617 cat >expect <<-\EOF &&
623 test_cmp expect actual
626 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given' '
627 test-tool parse-subcommand --subcommand-optional cmd >actual &&
628 cat >expect <<-\EOF &&
632 test_cmp expect actual
635 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL + given subcommand' '
636 test-tool parse-subcommand --subcommand-optional cmd subcmd-two branch file >actual &&
637 cat >expect <<-\EOF &&
644 test_cmp expect actual
647 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown dashless args' '
648 test-tool parse-subcommand --subcommand-optional cmd branch file >actual &&
649 cat >expect <<-\EOF &&
655 test_cmp expect actual
658 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown option' '
659 test_expect_code 129 test-tool parse-subcommand --subcommand-optional cmd --subcommand-opt 2>err &&
660 grep "^error: unknown option" err &&
664 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand not given + unknown option' '
665 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt >actual &&
666 cat >expect <<-\EOF &&
669 arg 00: --subcommand-opt
671 test_cmp expect actual
674 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand ignored after unknown option' '
675 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt subcmd-two >actual &&
676 cat >expect <<-\EOF &&
679 arg 00: --subcommand-opt
682 test_cmp expect actual
685 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + command and subcommand options cannot be mixed' '
686 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt branch --opt=1 >actual &&
687 cat >expect <<-\EOF &&
690 arg 00: --subcommand-opt
694 test_cmp expect actual
697 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_ARGV0' '
698 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-argv0 cmd --subcommand-opt branch >actual &&
699 cat >expect <<-\EOF &&
703 arg 01: --subcommand-opt
706 test_cmp expect actual
709 test_expect_success
'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_DASHDASH' '
710 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-dashdash cmd -- --subcommand-opt file >actual &&
711 cat >expect <<-\EOF &&
715 arg 01: --subcommand-opt
718 test_cmp expect actual
721 test_expect_success
'subcommand - completion helper' '
722 test-tool parse-subcommand cmd --git-completion-helper >actual &&
723 echo "subcmd-one subcmd-two --opt= --no-opt" >expect &&
724 test_cmp expect actual
727 test_expect_success
'subcommands are incompatible with STOP_AT_NON_OPTION' '
728 test_must_fail test-tool parse-subcommand --stop-at-non-option cmd subcmd-one 2>err &&
732 test_expect_success
'subcommands are incompatible with KEEP_UNKNOWN_OPT unless in combination with SUBCOMMAND_OPTIONAL' '
733 test_must_fail test-tool parse-subcommand --keep-unknown-opt cmd subcmd-two 2>err &&
737 test_expect_success
'subcommands are incompatible with KEEP_DASHDASH unless in combination with SUBCOMMAND_OPTIONAL' '
738 test_must_fail test-tool parse-subcommand --keep-dashdash cmd subcmd-two 2>err &&
742 test_expect_success
'negative magnitude' '
743 test_must_fail test-tool parse-options --magnitude -1 >out 2>err &&
744 grep "non-negative integer" err &&
745 test_must_be_empty out
748 test_expect_success
'magnitude with units but no numbers' '
749 test_must_fail test-tool parse-options --magnitude m >out 2>err &&
750 grep "non-negative integer" err &&
751 test_must_be_empty out