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