interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t0040-parse-options.sh

864 lines · 23903 bytes Raw Blame History
1#!/bin/sh
2#
3# Copyright (c) 2007 Johannes Schindelin
4#
5
6test_description='our own option parser'
7
8. ./test-lib.sh
9
10cat >expect-part1 <<\EOF
11usage: test-tool parse-options <options>
12
13 A helper function for the parse-options API.
14
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
22
23 -i, --[no-]integer <n>
24 get a integer
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>
34 get length of <str>
35 -F, --[no-]file <file>
36 set file to <file>
37
38String options
39 -s, --[no-]string <string>
40 get a string
41 --[no-]string2 <str> get another string
42 --[no-]st <st> get another string (pervert ordering)
43 -o <str> get another string
44EOF
45
46cat >expect-part2 <<\EOF
47 --longhelp help text of this entry
48 spans multiple lines
49 --[no-]list <str> add str to list
50
51Magic arguments
52 -NUM set integer to NUM
53 + same as -b
54 --ambiguous positive ambiguity
55 --no-ambiguous negative ambiguity
56
57Standard options
58 --[no-]abbrev[=<n>] use <n> digits to display object names
59 -v, --[no-]verbose be verbose
60 -n, --[no-]dry-run dry run
61 -q, --[no-]quiet be quiet
62 --[no-]expect <string>
63 expected output in the variable dump
64
65Alias
66 -A, --[no-]alias-source <string>
67 get a string
68 -Z, --[no-]alias-target <string>
69 alias of --alias-source
70
71EOF
72
73cat >expect-noop <<\EOF
74 --[no-]obsolete no-op (backward compatibility)
75EOF
76
77cat >expect-hidden <<\EOF
78Hidden options
79 --[no-]hidden-bool get a boolean
80 -k, --[no-]hidden-integer <n>
81 get a integer
82
83EOF
84
85test_expect_success 'test help' '
86 cat expect-part1 expect-part2 >expect &&
87 test-tool parse-options -h >output 2>output.err &&
88 test_must_be_empty output.err &&
89 test_cmp expect output
90'
91
92test_expect_success 'test --help-all shows hidden group and options' '
93 cat expect-part1 expect-noop expect-part2 expect-hidden >expect-help-all &&
94 test-tool parse-options --help-all >output 2>output.err &&
95 test_must_be_empty output.err &&
96 test_cmp expect-help-all output
97'
98
99mv expect expect.err
100
101check () {
102 what="$1" &&
103 shift &&
104 expect="$1" &&
105 shift &&
106 test-tool parse-options --expect="$what $expect" "$@"
107}
108
109check_unknown_i18n() {
110 case "$1" in
111 --*)
112 echo error: unknown option \`${1#--}\' >expect ;;
113 -*)
114 echo error: unknown switch \`${1#-}\' >expect ;;
115 esac &&
116 cat expect.err >>expect &&
117 test_must_fail test-tool parse-options $* >output 2>output.err &&
118 test_must_be_empty output &&
119 test_cmp expect output.err
120}
121
122test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
123test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
124test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
125test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
126test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
127
128test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
129test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
130
131test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
132test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
133
134test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
135test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
136
137test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
138
139test_expect_success 'OPT_INTEGER() negative' 'check integer: -2345 -i -2345'
140test_expect_success 'OPT_INTEGER() kilo' 'check integer: 239616 -i 234k'
141test_expect_success 'OPT_INTEGER() negative kilo' 'check integer: -239616 -i -234k'
142
143test_expect_success 'OPT_UNSIGNED() simple' '
144 check unsigned: 2345678 -u 2345678
145'
146
147test_expect_success 'OPT_UNSIGNED() kilo' '
148 check unsigned: 239616 -u 234k
149'
150
151test_expect_success 'OPT_UNSIGNED() mega' '
152 check unsigned: 104857600 -u 100m
153'
154
155test_expect_success 'OPT_UNSIGNED() giga' '
156 check unsigned: 1073741824 -u 1g
157'
158
159test_expect_success 'OPT_UNSIGNED() 3giga' '
160 check unsigned: 3221225472 -u 3g
161'
162
163cat >expect <<\EOF
164boolean: 2
165integer: 1729
166i16: 0
167unsigned: 16384
168u16: 0
169timestamp: 0
170string: 123
171abbrev: 7
172verbose: 2
173quiet: 0
174dry run: yes
175file: prefix/my.file
176EOF
177
178test_expect_success 'short options' '
179 test-tool parse-options -s123 -b -i 1729 -u 16k -b -vv -n -F my.file \
180 >output 2>output.err &&
181 test_cmp expect output &&
182 test_must_be_empty output.err
183'
184
185cat >expect <<\EOF
186boolean: 2
187integer: 1729
188i16: 9000
189unsigned: 16384
190u16: 32768
191timestamp: 0
192string: 321
193abbrev: 10
194verbose: 2
195quiet: 0
196dry run: no
197file: prefix/fi.le
198EOF
199
200test_expect_success 'long options' '
201 test-tool parse-options --boolean --integer 1729 --i16 9000 --unsigned 16k \
202 --u16 32k --boolean --string2=321 --verbose --verbose --no-dry-run \
203 --abbrev=10 --file fi.le --obsolete \
204 >output 2>output.err &&
205 test_must_be_empty output.err &&
206 test_cmp expect output
207'
208
209test_expect_success 'abbreviate to something longer than SHA1 length' '
210 cat >expect <<-EOF &&
211 boolean: 0
212 integer: 0
213 i16: 0
214 unsigned: 0
215 u16: 0
216 timestamp: 0
217 string: (not set)
218 abbrev: 100
219 verbose: -1
220 quiet: 0
221 dry run: no
222 file: (not set)
223 EOF
224 test-tool parse-options --abbrev=100 >output &&
225 test_cmp expect output
226'
227
228test_expect_success 'missing required value' '
229 cat >expect <<-\EOF &&
230 error: switch `s'\'' requires a value
231 EOF
232 test_expect_code 129 test-tool parse-options -s 2>actual &&
233 test_cmp expect actual &&
234
235 cat >expect <<-\EOF &&
236 error: option `string'\'' requires a value
237 EOF
238 test_expect_code 129 test-tool parse-options --string 2>actual &&
239 test_cmp expect actual &&
240
241 cat >expect <<-\EOF &&
242 error: option `file'\'' requires a value
243 EOF
244 test_expect_code 129 test-tool parse-options --file 2>actual &&
245 test_cmp expect actual
246'
247
248test_expect_success 'superfluous value provided: boolean' '
249 cat >expect <<-\EOF &&
250 error: option `yes'\'' takes no value
251 EOF
252 test_expect_code 129 test-tool parse-options --yes=hi 2>actual &&
253 test_cmp expect actual &&
254
255 cat >expect <<-\EOF &&
256 error: option `no-yes'\'' takes no value
257 EOF
258 test_expect_code 129 test-tool parse-options --no-yes=hi 2>actual &&
259 test_cmp expect actual
260'
261
262test_expect_success 'superfluous value provided: boolean, abbreviated' '
263 cat >expect <<-\EOF &&
264 error: option `yes'\'' takes no value
265 EOF
266 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
267 test-tool parse-options --ye=hi 2>actual &&
268 test_cmp expect actual &&
269
270 cat >expect <<-\EOF &&
271 error: option `no-yes'\'' takes no value
272 EOF
273 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
274 test-tool parse-options --no-ye=hi 2>actual &&
275 test_cmp expect actual
276'
277
278test_expect_success 'superfluous value provided: cmdmode' '
279 cat >expect <<-\EOF &&
280 error: option `mode1'\'' takes no value
281 EOF
282 test_expect_code 129 test-tool parse-options --mode1=hi 2>actual &&
283 test_cmp expect actual
284'
285
286cat >expect <<\EOF
287boolean: 1
288integer: 13
289i16: 0
290unsigned: 0
291u16: 0
292timestamp: 0
293string: 123
294abbrev: 7
295verbose: -1
296quiet: 0
297dry run: no
298file: (not set)
299arg 00: a1
300arg 01: b1
301arg 02: --boolean
302EOF
303
304test_expect_success 'intermingled arguments' '
305 test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
306 >output 2>output.err &&
307 test_must_be_empty output.err &&
308 test_cmp expect output
309'
310
311cat >expect <<\EOF
312boolean: 0
313integer: 2
314i16: 0
315unsigned: 0
316u16: 0
317timestamp: 0
318string: (not set)
319abbrev: 7
320verbose: -1
321quiet: 0
322dry run: no
323file: (not set)
324EOF
325
326test_expect_success 'unambiguously abbreviated option' '
327 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
328 test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err &&
329 test_must_be_empty output.err &&
330 test_cmp expect output
331'
332
333test_expect_success 'unambiguously abbreviated option with "="' '
334 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
335 test-tool parse-options --expect="integer: 2" --int=2
336'
337
338test_expect_success 'ambiguously abbreviated option' '
339 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
340 test-tool parse-options --strin 123
341'
342
343test_expect_success 'non ambiguous option (after two options it abbreviates)' '
344 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
345 test-tool parse-options --expect="string: 123" --st 123
346'
347
348test_expect_success 'Alias options do not contribute to abbreviation' '
349 test-tool parse-options --alias-source 123 >output &&
350 test_grep "^string: 123" output &&
351 test-tool parse-options --alias-target 123 >output &&
352 test_grep "^string: 123" output &&
353 test_must_fail test-tool parse-options --alias &&
354 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
355 test-tool parse-options --alias 123 >output &&
356 test_grep "^string: 123" output
357'
358
359cat >typo.err <<\EOF
360error: did you mean `--boolean` (with two dashes)?
361EOF
362
363test_expect_success 'detect possible typos' '
364 test_must_fail test-tool parse-options -boolean >output 2>output.err &&
365 test_must_be_empty output &&
366 test_cmp typo.err output.err
367'
368
369cat >typo.err <<\EOF
370error: did you mean `--ambiguous` (with two dashes)?
371EOF
372
373test_expect_success 'detect possible typos' '
374 test_must_fail test-tool parse-options -ambiguous >output 2>output.err &&
375 test_must_be_empty output &&
376 test_cmp typo.err output.err
377'
378
379cat >expect <<\EOF
380Callback: "four", 0
381boolean: 5
382integer: 4
383i16: 0
384unsigned: 0
385u16: 0
386timestamp: 0
387string: (not set)
388abbrev: 7
389verbose: -1
390quiet: 0
391dry run: no
392file: (not set)
393EOF
394
395test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
396 test-tool parse-options --length=four -b -4 >output 2>output.err &&
397 test_must_be_empty output.err &&
398 test_cmp expect output
399'
400
401test_expect_success 'OPT_CALLBACK() and callback errors work' '
402 test_must_fail test-tool parse-options --no-length >output 2>output.err &&
403 test_must_be_empty output &&
404 test_must_be_empty output.err
405'
406
407cat >expect <<\EOF
408boolean: 1
409integer: 23
410i16: 0
411unsigned: 0
412u16: 0
413timestamp: 0
414string: (not set)
415abbrev: 7
416verbose: -1
417quiet: 0
418dry run: no
419file: (not set)
420EOF
421
422test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
423 test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
424 test_must_be_empty output.err &&
425 test_cmp expect output
426'
427
428test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
429 test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
430 test_must_be_empty output.err &&
431 test_cmp expect output
432'
433
434test_expect_success 'OPT_BIT() works' '
435 test-tool parse-options --expect="boolean: 6" -bb --or4
436'
437
438test_expect_success 'OPT_NEGBIT() works' '
439 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
440'
441
442test_expect_success 'OPT_CMDMODE() works' '
443 test-tool parse-options --expect="integer: 1" --mode1 &&
444 test-tool parse-options --expect="integer: 3" --mode34=3
445'
446
447test_expect_success 'OPT_CMDMODE() detects incompatibility (1)' '
448 test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err &&
449 test_must_be_empty output &&
450 test_grep "mode1" output.err &&
451 test_grep "mode2" output.err &&
452 test_grep "cannot be used together" output.err
453'
454
455test_expect_success 'OPT_CMDMODE() detects incompatibility (2)' '
456 test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err &&
457 test_must_be_empty output &&
458 test_grep "mode2" output.err &&
459 test_grep "set23" output.err &&
460 test_grep "cannot be used together" output.err
461'
462
463test_expect_success 'OPT_CMDMODE() detects incompatibility (3)' '
464 test_must_fail test-tool parse-options --mode2 --set23 >output 2>output.err &&
465 test_must_be_empty output &&
466 test_grep "mode2" output.err &&
467 test_grep "set23" output.err &&
468 test_grep "cannot be used together" output.err
469'
470
471test_expect_success 'OPT_CMDMODE() detects incompatibility (4)' '
472 test_must_fail test-tool parse-options --mode2 --mode34=3 \
473 >output 2>output.err &&
474 test_must_be_empty output &&
475 test_grep "mode2" output.err &&
476 test_grep "mode34.3" output.err &&
477 test_grep "cannot be used together" output.err
478'
479
480test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
481 test-tool parse-options --expect="boolean: 6" + + + + + +
482'
483
484test_expect_success 'OPT_NUMBER_CALLBACK() works' '
485 test-tool parse-options --expect="integer: 12345" -12345
486'
487
488cat >expect <<\EOF
489boolean: 0
490integer: 0
491i16: 0
492unsigned: 0
493u16: 0
494timestamp: 0
495string: (not set)
496abbrev: 7
497verbose: -1
498quiet: 0
499dry run: no
500file: (not set)
501EOF
502
503test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
504 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
505 test-tool parse-options --no-ambig >output 2>output.err &&
506 test_must_be_empty output.err &&
507 test_cmp expect output
508'
509
510cat >>expect <<\EOF
511list: foo
512list: bar
513list: baz
514EOF
515test_expect_success '--list keeps list of strings' '
516 test-tool parse-options --list foo --list=bar --list=baz >output &&
517 test_cmp expect output
518'
519
520test_expect_success '--no-list resets list' '
521 test-tool parse-options --list=other --list=irrelevant --list=options \
522 --no-list --list=foo --list=bar --list=baz >output &&
523 test_cmp expect output
524'
525
526test_expect_success 'multiple quiet levels' '
527 test-tool parse-options --expect="quiet: 3" -q -q -q
528'
529
530test_expect_success 'multiple verbose levels' '
531 test-tool parse-options --expect="verbose: 3" -v -v -v
532'
533
534test_expect_success '--no-quiet sets --quiet to 0' '
535 test-tool parse-options --expect="quiet: 0" --no-quiet
536'
537
538test_expect_success '--no-quiet resets multiple -q to 0' '
539 test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet
540'
541
542test_expect_success '--no-verbose sets verbose to 0' '
543 test-tool parse-options --expect="verbose: 0" --no-verbose
544'
545
546test_expect_success '--no-verbose resets multiple verbose to 0' '
547 test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose
548'
549
550test_expect_success 'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' '
551 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
552 test-tool parse-options --ye &&
553 test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \
554 test-tool parse-options --ye
555'
556
557test_expect_success '--end-of-options treats remainder as args' '
558 test-tool parse-options \
559 --expect="verbose: -1" \
560 --expect="arg 00: --verbose" \
561 --end-of-options --verbose
562'
563
564test_expect_success 'KEEP_DASHDASH works' '
565 test-tool parse-options-flags --keep-dashdash cmd --opt=1 -- --opt=2 --unknown >actual &&
566 cat >expect <<-\EOF &&
567 opt: 1
568 arg 00: --
569 arg 01: --opt=2
570 arg 02: --unknown
571 EOF
572 test_cmp expect actual
573'
574
575test_expect_success 'KEEP_ARGV0 works' '
576 test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual &&
577 cat >expect <<-\EOF &&
578 opt: 3
579 arg 00: cmd
580 arg 01: arg0
581 EOF
582 test_cmp expect actual
583'
584
585test_expect_success 'STOP_AT_NON_OPTION works' '
586 test-tool parse-options-flags --stop-at-non-option cmd --opt=4 arg0 --opt=5 --unknown >actual &&
587 cat >expect <<-\EOF &&
588 opt: 4
589 arg 00: arg0
590 arg 01: --opt=5
591 arg 02: --unknown
592 EOF
593 test_cmp expect actual
594'
595
596test_expect_success 'KEEP_UNKNOWN_OPT works' '
597 test-tool parse-options-flags --keep-unknown-opt cmd --unknown=1 --opt=6 -u2 >actual &&
598 cat >expect <<-\EOF &&
599 opt: 6
600 arg 00: --unknown=1
601 arg 01: -u2
602 EOF
603 test_cmp expect actual
604'
605
606test_expect_success 'NO_INTERNAL_HELP works for -h' '
607 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd -h 2>err &&
608 test_grep "^error: unknown switch \`h$SQ" err &&
609 test_grep "^usage: " err
610'
611
612for help_opt in help help-all
613do
614 test_expect_success "NO_INTERNAL_HELP works for --$help_opt" "
615 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd --$help_opt 2>err &&
616 test_grep '^error: unknown option \`'$help_opt\' err &&
617 test_grep '^usage: ' err
618 "
619done
620
621test_expect_success 'KEEP_UNKNOWN_OPT | NO_INTERNAL_HELP works' '
622 test-tool parse-options-flags --keep-unknown-opt --no-internal-help cmd -h --help --help-all >actual &&
623 cat >expect <<-\EOF &&
624 opt: 0
625 arg 00: -h
626 arg 01: --help
627 arg 02: --help-all
628 EOF
629 test_cmp expect actual
630'
631
632test_expect_success 'subcommand - usage lists subcommands with help text under their group' '
633 test-tool parse-subcommand cmd -h >actual &&
634 cat >expect <<-\EOF &&
635 usage: <...> cmd subcmd-one
636 or: <...> cmd subcmd-two
637
638 Subcommands
639 subcmd-one the first subcommand
640
641 Options
642 -o, --[no-]opt <n> an integer option
643
644 EOF
645 test_cmp expect actual
646'
647
648test_expect_success 'subcommand - no subcommand shows error and usage' '
649 test_expect_code 129 test-tool parse-subcommand cmd 2>err &&
650 test_grep "^error: need a subcommand" err &&
651 test_grep ^usage: err
652'
653
654test_expect_success 'subcommand - subcommand after -- shows error and usage' '
655 test_expect_code 129 test-tool parse-subcommand cmd -- subcmd-one 2>err &&
656 test_grep "^error: need a subcommand" err &&
657 test_grep ^usage: err
658'
659
660test_expect_success 'subcommand - subcommand after --end-of-options shows error and usage' '
661 test_expect_code 129 test-tool parse-subcommand cmd --end-of-options subcmd-one 2>err &&
662 test_grep "^error: need a subcommand" err &&
663 test_grep ^usage: err
664'
665
666test_expect_success 'subcommand - unknown subcommand shows error and usage' '
667 test_expect_code 129 test-tool parse-subcommand cmd nope 2>err &&
668 test_grep "^error: unknown subcommand: \`nope$SQ" err &&
669 test_grep ^usage: err
670'
671
672test_expect_success 'subcommand - subcommands cannot be abbreviated' '
673 test_expect_code 129 test-tool parse-subcommand cmd subcmd-o 2>err &&
674 test_grep "^error: unknown subcommand: \`subcmd-o$SQ$" err &&
675 test_grep ^usage: err
676'
677
678test_expect_success 'subcommand - no negated subcommands' '
679 test_expect_code 129 test-tool parse-subcommand cmd no-subcmd-one 2>err &&
680 test_grep "^error: unknown subcommand: \`no-subcmd-one$SQ" err &&
681 test_grep ^usage: err
682'
683
684test_expect_success 'subcommand - simple' '
685 test-tool parse-subcommand cmd subcmd-two >actual &&
686 cat >expect <<-\EOF &&
687 opt: 0
688 fn: subcmd_two
689 arg 00: subcmd-two
690 EOF
691 test_cmp expect actual
692'
693
694test_expect_success 'subcommand - stop parsing at the first subcommand' '
695 test-tool parse-subcommand cmd --opt=1 subcmd-two subcmd-one --opt=2 >actual &&
696 cat >expect <<-\EOF &&
697 opt: 1
698 fn: subcmd_two
699 arg 00: subcmd-two
700 arg 01: subcmd-one
701 arg 02: --opt=2
702 EOF
703 test_cmp expect actual
704'
705
706test_expect_success 'subcommand - KEEP_ARGV0' '
707 test-tool parse-subcommand --keep-argv0 cmd subcmd-two >actual &&
708 cat >expect <<-\EOF &&
709 opt: 0
710 fn: subcmd_two
711 arg 00: cmd
712 arg 01: subcmd-two
713 EOF
714 test_cmp expect actual
715'
716
717test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given' '
718 test-tool parse-subcommand --subcommand-optional cmd >actual &&
719 cat >expect <<-\EOF &&
720 opt: 0
721 fn: subcmd_one
722 EOF
723 test_cmp expect actual
724'
725
726test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + given subcommand' '
727 test-tool parse-subcommand --subcommand-optional cmd subcmd-two branch file >actual &&
728 cat >expect <<-\EOF &&
729 opt: 0
730 fn: subcmd_two
731 arg 00: subcmd-two
732 arg 01: branch
733 arg 02: file
734 EOF
735 test_cmp expect actual
736'
737
738test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown dashless args' '
739 test-tool parse-subcommand --subcommand-optional cmd branch file >actual &&
740 cat >expect <<-\EOF &&
741 opt: 0
742 fn: subcmd_one
743 arg 00: branch
744 arg 01: file
745 EOF
746 test_cmp expect actual
747'
748
749test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown option' '
750 test_expect_code 129 test-tool parse-subcommand --subcommand-optional cmd --subcommand-opt 2>err &&
751 test_grep "^error: unknown option" err &&
752 test_grep ^usage: err
753'
754
755test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand not given + unknown option' '
756 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt >actual &&
757 cat >expect <<-\EOF &&
758 opt: 0
759 fn: subcmd_one
760 arg 00: --subcommand-opt
761 EOF
762 test_cmp expect actual
763'
764
765test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand ignored after unknown option' '
766 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt subcmd-two >actual &&
767 cat >expect <<-\EOF &&
768 opt: 0
769 fn: subcmd_one
770 arg 00: --subcommand-opt
771 arg 01: subcmd-two
772 EOF
773 test_cmp expect actual
774'
775
776test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + command and subcommand options cannot be mixed' '
777 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt branch --opt=1 >actual &&
778 cat >expect <<-\EOF &&
779 opt: 0
780 fn: subcmd_one
781 arg 00: --subcommand-opt
782 arg 01: branch
783 arg 02: --opt=1
784 EOF
785 test_cmp expect actual
786'
787
788test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_ARGV0' '
789 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-argv0 cmd --subcommand-opt branch >actual &&
790 cat >expect <<-\EOF &&
791 opt: 0
792 fn: subcmd_one
793 arg 00: cmd
794 arg 01: --subcommand-opt
795 arg 02: branch
796 EOF
797 test_cmp expect actual
798'
799
800test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_DASHDASH' '
801 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-dashdash cmd -- --subcommand-opt file >actual &&
802 cat >expect <<-\EOF &&
803 opt: 0
804 fn: subcmd_one
805 arg 00: --
806 arg 01: --subcommand-opt
807 arg 02: file
808 EOF
809 test_cmp expect actual
810'
811
812test_expect_success 'subcommand - completion helper' '
813 test-tool parse-subcommand cmd --git-completion-helper >actual &&
814 echo "subcmd-one subcmd-two --opt= --no-opt" >expect &&
815 test_cmp expect actual
816'
817
818test_expect_success 'subcommands are incompatible with STOP_AT_NON_OPTION' '
819 test_must_fail test-tool parse-subcommand --stop-at-non-option cmd subcmd-one 2>err &&
820 test_grep ^BUG err
821'
822
823test_expect_success 'subcommands are incompatible with KEEP_UNKNOWN_OPT unless in combination with SUBCOMMAND_OPTIONAL' '
824 test_must_fail test-tool parse-subcommand --keep-unknown-opt cmd subcmd-two 2>err &&
825 test_grep ^BUG err
826'
827
828test_expect_success 'subcommands are incompatible with KEEP_DASHDASH unless in combination with SUBCOMMAND_OPTIONAL' '
829 test_must_fail test-tool parse-subcommand --keep-dashdash cmd subcmd-two 2>err &&
830 test_grep ^BUG err
831'
832
833test_expect_success 'negative unsigned' '
834 test_must_fail test-tool parse-options --unsigned -1 >out 2>err &&
835 test_grep "non-negative integer" err &&
836 test_must_be_empty out
837'
838
839test_expect_success 'unsigned with units but no numbers' '
840 test_must_fail test-tool parse-options --unsigned m >out 2>err &&
841 test_grep "non-negative integer" err &&
842 test_must_be_empty out
843'
844
845test_expect_success 'i16 limits range' '
846 test-tool parse-options --i16 32767 >out &&
847 test_grep "i16: 32767" out &&
848 test_must_fail test-tool parse-options --i16 32768 2>err &&
849 test_grep "value 32768 for option .i16. not in range \[-32768,32767\]" err &&
850
851 test-tool parse-options --i16 -32768 >out &&
852 test_grep "i16: -32768" out &&
853 test_must_fail test-tool parse-options --i16 -32769 2>err &&
854 test_grep "value -32769 for option .i16. not in range \[-32768,32767\]" err
855'
856
857test_expect_success 'u16 limits range' '
858 test-tool parse-options --u16 65535 >out &&
859 test_grep "u16: 65535" out &&
860 test_must_fail test-tool parse-options --u16 65536 2>err &&
861 test_grep "value 65536 for option .u16. not in range \[0,65535\]" err
862'
863
864test_done