]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0040-parse-options.sh
parse-options: simplify positivation handling
[thirdparty/git.git] / t / t0040-parse-options.sh
CommitLineData
beb47437
JS
1#!/bin/sh
2#
3# Copyright (c) 2007 Johannes Schindelin
4#
5
6test_description='our own option parser'
7
c0b80e05 8TEST_PASSES_SANITIZE_LEAK=true
beb47437
JS
9. ./test-lib.sh
10
8425b7ea 11cat >expect <<\EOF
2f17c78c 12usage: test-tool parse-options <options>
beb47437 13
c97ee171
BC
14 A helper function for the parse-options API.
15
e8e5d294 16 --[no-]yes get a boolean
2a409a1d
RS
17 -D, --no-doubt begins with 'no-'
18 --doubt opposite of --no-doubt
b9e63ddd 19 -B, --no-fear be brave
e8e5d294
RS
20 -b, --[no-]boolean increment by one
21 -4, --[no-]or4 bitwise-or boolean with ...0100
22 --[no-]neg-or4 same as --no-or4
010a2dac 23
e8e5d294
RS
24 -i, --[no-]integer <n>
25 get a integer
beb47437 26 -j <n> get a integer, too
2a514ed8 27 -m, --magnitude <n> get a magnitude
e8e5d294 28 --[no-]set23 set integer to 23
62e7a6f7
PB
29 --mode1 set integer to 1 (cmdmode option)
30 --mode2 set integer to 2 (cmdmode option)
0025dde7 31 --[no-]mode34 (3|4) set integer to 3 or 4 (cmdmode option)
e8e5d294
RS
32 -L, --[no-]length <str>
33 get length of <str>
34 -F, --[no-]file <file>
35 set file to <file>
beb47437 36
010a2dac 37String options
e8e5d294
RS
38 -s, --[no-]string <string>
39 get a string
40 --[no-]string2 <str> get another string
41 --[no-]st <st> get another string (pervert ordering)
3a9f0f41 42 -o <str> get another string
448abbba
JH
43 --longhelp help text of this entry
44 spans multiple lines
e8e5d294 45 --[no-]list <str> add str to list
beb47437 46
010a2dac 47Magic arguments
e0319ff5 48 -NUM set integer to NUM
51a9949e 49 + same as -b
6bbfd1fa
AS
50 --ambiguous positive ambiguity
51 --no-ambiguous negative ambiguity
580d5bff 52
010a2dac 53Standard options
e8e5d294
RS
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
010a2dac 60
5c387428 61Alias
e8e5d294 62 -A, --[no-]alias-source <string>
5c387428 63 get a string
e8e5d294 64 -Z, --[no-]alias-target <string>
7c280589 65 alias of --alias-source
5c387428 66
beb47437
JS
67EOF
68
69test_expect_success 'test help' '
2f17c78c 70 test_must_fail test-tool parse-options -h >output 2>output.err &&
ca8d148d 71 test_must_be_empty output.err &&
1108cea7 72 test_cmp expect output
beb47437
JS
73'
74
9c7304e3
GS
75mv expect expect.err
76
8ca65aeb 77check () {
b9e63ddd
RS
78 what="$1" &&
79 shift &&
80 expect="$1" &&
81 shift &&
2f17c78c 82 test-tool parse-options --expect="$what $expect" "$@"
b9e63ddd
RS
83}
84
9a001381
JX
85check_unknown_i18n() {
86 case "$1" in
87 --*)
88 echo error: unknown option \`${1#--}\' >expect ;;
89 -*)
90 echo error: unknown switch \`${1#-}\' >expect ;;
91 esac &&
92 cat expect.err >>expect &&
2f17c78c 93 test_must_fail test-tool parse-options $* >output 2>output.err &&
ca8d148d 94 test_must_be_empty output &&
1108cea7 95 test_cmp expect output.err
9a001381
JX
96}
97
b9e63ddd
RS
98test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
99test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
100test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
101test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
102test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
103
104test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
105test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
106
107test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
108test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
109
1a9bf1e1
BC
110test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
111test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
b9e63ddd 112
0f1930c5 113test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
b9e63ddd 114
81a48cc0
CB
115test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
116
2a514ed8
CB
117test_expect_success 'OPT_MAGNITUDE() simple' '
118 check magnitude: 2345678 -m 2345678
119'
120
121test_expect_success 'OPT_MAGNITUDE() kilo' '
122 check magnitude: 239616 -m 234k
123'
124
125test_expect_success 'OPT_MAGNITUDE() mega' '
126 check magnitude: 104857600 -m 100m
127'
128
129test_expect_success 'OPT_MAGNITUDE() giga' '
130 check magnitude: 1073741824 -m 1g
131'
132
133test_expect_success 'OPT_MAGNITUDE() 3giga' '
134 check magnitude: 3221225472 -m 3g
135'
136
8425b7ea 137cat >expect <<\EOF
beb47437
JS
138boolean: 2
139integer: 1729
2a514ed8 140magnitude: 16384
c4aca9cc 141timestamp: 0
beb47437 142string: 123
010a2dac
SB
143abbrev: 7
144verbose: 2
36e6a5ba 145quiet: 0
010a2dac 146dry run: yes
df217ed6 147file: prefix/my.file
beb47437
JS
148EOF
149
150test_expect_success 'short options' '
2f17c78c 151 test-tool parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
2a514ed8 152 >output 2>output.err &&
3af82863 153 test_cmp expect output &&
ca8d148d 154 test_must_be_empty output.err
beb47437 155'
010a2dac 156
8425b7ea 157cat >expect <<\EOF
beb47437
JS
158boolean: 2
159integer: 1729
2a514ed8 160magnitude: 16384
c4aca9cc 161timestamp: 0
beb47437 162string: 321
010a2dac
SB
163abbrev: 10
164verbose: 2
36e6a5ba 165quiet: 0
010a2dac 166dry run: no
df217ed6 167file: prefix/fi.le
beb47437
JS
168EOF
169
170test_expect_success 'long options' '
2f17c78c 171 test-tool parse-options --boolean --integer 1729 --magnitude 16k \
2a514ed8
CB
172 --boolean --string2=321 --verbose --verbose --no-dry-run \
173 --abbrev=10 --file fi.le --obsolete \
174 >output 2>output.err &&
ca8d148d 175 test_must_be_empty output.err &&
3af82863 176 test_cmp expect output
beb47437
JS
177'
178
d5d745f9 179test_expect_success 'missing required value' '
62f2ffc5
ÆAB
180 cat >expect <<-\EOF &&
181 error: switch `s'\'' requires a value
182 EOF
183 test_expect_code 129 test-tool parse-options -s 2>actual &&
184 test_cmp expect actual &&
185
186 cat >expect <<-\EOF &&
187 error: option `string'\'' requires a value
188 EOF
189 test_expect_code 129 test-tool parse-options --string 2>actual &&
190 test_cmp expect actual &&
191
192 cat >expect <<-\EOF &&
193 error: option `file'\'' requires a value
194 EOF
195 test_expect_code 129 test-tool parse-options --file 2>actual &&
196 test_cmp expect actual
197'
198
199test_expect_success 'superfluous value provided: boolean' '
200 cat >expect <<-\EOF &&
201 error: option `yes'\'' takes no value
202 EOF
203 test_expect_code 129 test-tool parse-options --yes=hi 2>actual &&
204 test_cmp expect actual &&
205
206 cat >expect <<-\EOF &&
207 error: option `no-yes'\'' takes no value
208 EOF
209 test_expect_code 129 test-tool parse-options --no-yes=hi 2>actual &&
210 test_cmp expect actual
211'
212
213test_expect_success 'superfluous value provided: cmdmode' '
214 cat >expect <<-\EOF &&
215 error: option `mode1'\'' takes no value
216 EOF
217 test_expect_code 129 test-tool parse-options --mode1=hi 2>actual &&
218 test_cmp expect actual
d5d745f9
OM
219'
220
8425b7ea 221cat >expect <<\EOF
beb47437
JS
222boolean: 1
223integer: 13
2a514ed8 224magnitude: 0
c4aca9cc 225timestamp: 0
beb47437 226string: 123
010a2dac 227abbrev: 7
e0070e8b 228verbose: -1
36e6a5ba 229quiet: 0
010a2dac 230dry run: no
df217ed6 231file: (not set)
beb47437
JS
232arg 00: a1
233arg 01: b1
234arg 02: --boolean
235EOF
236
237test_expect_success 'intermingled arguments' '
2f17c78c 238 test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
8425b7ea 239 >output 2>output.err &&
ca8d148d 240 test_must_be_empty output.err &&
3af82863 241 test_cmp expect output
beb47437
JS
242'
243
8425b7ea 244cat >expect <<\EOF
7f275b91
JS
245boolean: 0
246integer: 2
2a514ed8 247magnitude: 0
c4aca9cc 248timestamp: 0
7f275b91 249string: (not set)
010a2dac 250abbrev: 7
e0070e8b 251verbose: -1
36e6a5ba 252quiet: 0
010a2dac 253dry run: no
df217ed6 254file: (not set)
7f275b91
JS
255EOF
256
257test_expect_success 'unambiguously abbreviated option' '
b02e7d5d 258 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
2f17c78c 259 test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err &&
ca8d148d 260 test_must_be_empty output.err &&
3af82863 261 test_cmp expect output
7f275b91
JS
262'
263
264test_expect_success 'unambiguously abbreviated option with "="' '
b02e7d5d 265 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
2f17c78c 266 test-tool parse-options --expect="integer: 2" --int=2
7f275b91
JS
267'
268
41ac414e 269test_expect_success 'ambiguously abbreviated option' '
b02e7d5d
JS
270 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
271 test-tool parse-options --strin 123
7f275b91
JS
272'
273
243e0614 274test_expect_success 'non ambiguous option (after two options it abbreviates)' '
b02e7d5d 275 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
2f17c78c 276 test-tool parse-options --expect="string: 123" --st 123
243e0614
JS
277'
278
5c387428
NTND
279test_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
288'
289
8425b7ea 290cat >typo.err <<\EOF
395518cf 291error: did you mean `--boolean` (with two dashes)?
3a9f0f41
PH
292EOF
293
294test_expect_success 'detect possible typos' '
2f17c78c 295 test_must_fail test-tool parse-options -boolean >output 2>output.err &&
ca8d148d 296 test_must_be_empty output &&
1108cea7 297 test_cmp typo.err output.err
3a9f0f41
PH
298'
299
8425b7ea 300cat >typo.err <<\EOF
395518cf 301error: did you mean `--ambiguous` (with two dashes)?
38916c5b
RS
302EOF
303
304test_expect_success 'detect possible typos' '
2f17c78c 305 test_must_fail test-tool parse-options -ambiguous >output 2>output.err &&
ca8d148d 306 test_must_be_empty output &&
1108cea7 307 test_cmp typo.err output.err
38916c5b
RS
308'
309
8425b7ea 310cat >expect <<\EOF
010a2dac
SB
311Callback: "four", 0
312boolean: 5
313integer: 4
2a514ed8 314magnitude: 0
c4aca9cc 315timestamp: 0
010a2dac
SB
316string: (not set)
317abbrev: 7
e0070e8b 318verbose: -1
36e6a5ba 319quiet: 0
010a2dac 320dry run: no
df217ed6 321file: (not set)
010a2dac
SB
322EOF
323
324test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
2f17c78c 325 test-tool parse-options --length=four -b -4 >output 2>output.err &&
ca8d148d 326 test_must_be_empty output.err &&
010a2dac
SB
327 test_cmp expect output
328'
329
1a9bf1e1 330test_expect_success 'OPT_CALLBACK() and callback errors work' '
2f17c78c 331 test_must_fail test-tool parse-options --no-length >output 2>output.err &&
1c5e94f4 332 test_must_be_empty output &&
3bb0923f 333 test_must_be_empty output.err
010a2dac
SB
334'
335
8425b7ea 336cat >expect <<\EOF
010a2dac
SB
337boolean: 1
338integer: 23
2a514ed8 339magnitude: 0
c4aca9cc 340timestamp: 0
010a2dac
SB
341string: (not set)
342abbrev: 7
e0070e8b 343verbose: -1
36e6a5ba 344quiet: 0
010a2dac 345dry run: no
df217ed6 346file: (not set)
010a2dac
SB
347EOF
348
349test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
2f17c78c 350 test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
ca8d148d 351 test_must_be_empty output.err &&
010a2dac
SB
352 test_cmp expect output
353'
354
2f4b97f9 355test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
2f17c78c 356 test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
ca8d148d 357 test_must_be_empty output.err &&
2f4b97f9
RS
358 test_cmp expect output
359'
360
2f4b97f9 361test_expect_success 'OPT_BIT() works' '
2f17c78c 362 test-tool parse-options --expect="boolean: 6" -bb --or4
2f4b97f9
RS
363'
364
365test_expect_success 'OPT_NEGBIT() works' '
2f17c78c 366 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
2f4b97f9 367'
010a2dac 368
62e7a6f7 369test_expect_success 'OPT_CMDMODE() works' '
0025dde7
RS
370 test-tool parse-options --expect="integer: 1" --mode1 &&
371 test-tool parse-options --expect="integer: 3" --mode34=3
62e7a6f7
PB
372'
373
0025dde7 374test_expect_success 'OPT_CMDMODE() detects incompatibility (1)' '
62e7a6f7
PB
375 test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err &&
376 test_must_be_empty output &&
a8e23947
JH
377 test_grep "mode1" output.err &&
378 test_grep "mode2" output.err &&
379 test_grep "is incompatible with" output.err
62e7a6f7
PB
380'
381
0025dde7 382test_expect_success 'OPT_CMDMODE() detects incompatibility (2)' '
62e7a6f7
PB
383 test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err &&
384 test_must_be_empty output &&
a8e23947
JH
385 test_grep "mode2" output.err &&
386 test_grep "set23" output.err &&
387 test_grep "is incompatible with" output.err
0025dde7
RS
388'
389
390test_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 &&
a8e23947
JH
393 test_grep "mode2" output.err &&
394 test_grep "set23" output.err &&
395 test_grep "is incompatible with" output.err
0025dde7
RS
396'
397
398test_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 &&
a8e23947
JH
402 test_grep "mode2" output.err &&
403 test_grep "mode34.3" output.err &&
404 test_grep "is incompatible with" output.err
62e7a6f7
PB
405'
406
b9e63ddd 407test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
2f17c78c 408 test-tool parse-options --expect="boolean: 6" + + + + + +
51a9949e
RS
409'
410
e0319ff5 411test_expect_success 'OPT_NUMBER_CALLBACK() works' '
2f17c78c 412 test-tool parse-options --expect="integer: 12345" -12345
e0319ff5
RS
413'
414
8425b7ea 415cat >expect <<\EOF
6bbfd1fa
AS
416boolean: 0
417integer: 0
2a514ed8 418magnitude: 0
6bbfd1fa
AS
419timestamp: 0
420string: (not set)
421abbrev: 7
e0070e8b 422verbose: -1
36e6a5ba 423quiet: 0
6bbfd1fa
AS
424dry run: no
425file: (not set)
426EOF
427
428test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
b02e7d5d 429 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
2f17c78c 430 test-tool parse-options --no-ambig >output 2>output.err &&
ca8d148d 431 test_must_be_empty output.err &&
6bbfd1fa
AS
432 test_cmp expect output
433'
434
8425b7ea 435cat >>expect <<\EOF
c8ba1639
JK
436list: foo
437list: bar
438list: baz
439EOF
440test_expect_success '--list keeps list of strings' '
2f17c78c 441 test-tool parse-options --list foo --list=bar --list=baz >output &&
c8ba1639
JK
442 test_cmp expect output
443'
444
445test_expect_success '--no-list resets list' '
2f17c78c 446 test-tool parse-options --list=other --list=irrelevant --list=options \
c8ba1639
JK
447 --no-list --list=foo --list=bar --list=baz >output &&
448 test_cmp expect output
449'
450
7d177152 451test_expect_success 'multiple quiet levels' '
2f17c78c 452 test-tool parse-options --expect="quiet: 3" -q -q -q
7d177152
PB
453'
454
7d177152 455test_expect_success 'multiple verbose levels' '
2f17c78c 456 test-tool parse-options --expect="verbose: 3" -v -v -v
7d177152
PB
457'
458
7d177152 459test_expect_success '--no-quiet sets --quiet to 0' '
2f17c78c 460 test-tool parse-options --expect="quiet: 0" --no-quiet
7d177152
PB
461'
462
7d177152 463test_expect_success '--no-quiet resets multiple -q to 0' '
2f17c78c 464 test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet
7d177152
PB
465'
466
7d177152 467test_expect_success '--no-verbose sets verbose to 0' '
2f17c78c 468 test-tool parse-options --expect="verbose: 0" --no-verbose
7d177152
PB
469'
470
7d177152 471test_expect_success '--no-verbose resets multiple verbose to 0' '
2f17c78c 472 test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose
7d177152
PB
473'
474
b02e7d5d
JS
475test_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
480'
481
51b4594b
JK
482test_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
487'
488
c1b117d3
SG
489test_expect_success 'KEEP_DASHDASH works' '
490 test-tool parse-options-flags --keep-dashdash cmd --opt=1 -- --opt=2 --unknown >actual &&
491 cat >expect <<-\EOF &&
492 opt: 1
493 arg 00: --
494 arg 01: --opt=2
495 arg 02: --unknown
496 EOF
497 test_cmp expect actual
498'
499
500test_expect_success 'KEEP_ARGV0 works' '
501 test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual &&
502 cat >expect <<-\EOF &&
503 opt: 3
504 arg 00: cmd
505 arg 01: arg0
506 EOF
507 test_cmp expect actual
508'
509
510test_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 &&
513 opt: 4
514 arg 00: arg0
515 arg 01: --opt=5
516 arg 02: --unknown
517 EOF
518 test_cmp expect actual
519'
520
99d86d60
SG
521test_expect_success 'KEEP_UNKNOWN_OPT works' '
522 test-tool parse-options-flags --keep-unknown-opt cmd --unknown=1 --opt=6 -u2 >actual &&
c1b117d3
SG
523 cat >expect <<-\EOF &&
524 opt: 6
525 arg 00: --unknown=1
526 arg 01: -u2
527 EOF
528 test_cmp expect actual
529'
530
531test_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 &&
c1b117d3
SG
533 grep "^error: unknown switch \`h$SQ" err &&
534 grep "^usage: " err
535'
536
537for help_opt in help help-all
538do
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 &&
c1b117d3
SG
541 grep '^error: unknown option \`'$help_opt\' err &&
542 grep '^usage: ' err
543 "
544done
545
99d86d60
SG
546test_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 &&
c1b117d3
SG
548 cat >expect <<-\EOF &&
549 opt: 0
550 arg 00: -h
551 arg 01: --help
552 arg 02: --help-all
553 EOF
554 test_cmp expect actual
555'
556
fa83cc83
SG
557test_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 &&
560 grep ^usage: err
561'
562
563test_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 &&
566 grep ^usage: err
567'
568
569test_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 &&
572 grep ^usage: err
573'
574
575test_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 &&
578 grep ^usage: err
579'
580
581test_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 &&
584 grep ^usage: err
585'
586
587test_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 &&
590 grep ^usage: err
591'
592
593test_expect_success 'subcommand - simple' '
594 test-tool parse-subcommand cmd subcmd-two >actual &&
595 cat >expect <<-\EOF &&
596 opt: 0
597 fn: subcmd_two
598 arg 00: subcmd-two
599 EOF
600 test_cmp expect actual
601'
602
603test_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 &&
606 opt: 1
607 fn: subcmd_two
608 arg 00: subcmd-two
609 arg 01: subcmd-one
610 arg 02: --opt=2
611 EOF
612 test_cmp expect actual
613'
614
615test_expect_success 'subcommand - KEEP_ARGV0' '
616 test-tool parse-subcommand --keep-argv0 cmd subcmd-two >actual &&
617 cat >expect <<-\EOF &&
618 opt: 0
619 fn: subcmd_two
620 arg 00: cmd
621 arg 01: subcmd-two
622 EOF
623 test_cmp expect actual
624'
625
626test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given' '
627 test-tool parse-subcommand --subcommand-optional cmd >actual &&
628 cat >expect <<-\EOF &&
629 opt: 0
630 fn: subcmd_one
631 EOF
632 test_cmp expect actual
633'
634
635test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + given subcommand' '
636 test-tool parse-subcommand --subcommand-optional cmd subcmd-two branch file >actual &&
637 cat >expect <<-\EOF &&
638 opt: 0
639 fn: subcmd_two
640 arg 00: subcmd-two
641 arg 01: branch
642 arg 02: file
643 EOF
644 test_cmp expect actual
645'
646
647test_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 &&
650 opt: 0
651 fn: subcmd_one
652 arg 00: branch
653 arg 01: file
654 EOF
655 test_cmp expect actual
656'
657
658test_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 &&
661 grep ^usage: err
662'
663
664test_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 &&
667 opt: 0
668 fn: subcmd_one
669 arg 00: --subcommand-opt
670 EOF
671 test_cmp expect actual
672'
673
674test_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 &&
677 opt: 0
678 fn: subcmd_one
679 arg 00: --subcommand-opt
680 arg 01: subcmd-two
681 EOF
682 test_cmp expect actual
683'
684
685test_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 &&
688 opt: 0
689 fn: subcmd_one
690 arg 00: --subcommand-opt
691 arg 01: branch
692 arg 02: --opt=1
693 EOF
694 test_cmp expect actual
695'
696
697test_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 &&
700 opt: 0
701 fn: subcmd_one
702 arg 00: cmd
703 arg 01: --subcommand-opt
704 arg 02: branch
705 EOF
706 test_cmp expect actual
707'
708
709test_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 &&
712 opt: 0
713 fn: subcmd_one
714 arg 00: --
715 arg 01: --subcommand-opt
716 arg 02: file
717 EOF
718 test_cmp expect actual
719'
720
721test_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
725'
726
727test_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 &&
729 grep ^BUG err
730'
731
732test_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 &&
734 grep ^BUG err
735'
736
737test_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 &&
739 grep ^BUG err
740'
741
84356ff7
PW
742test_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
746'
7595c0ec
PW
747
748test_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
752'
753
beb47437 754test_done