Merge tag 'v2.8.6' into maint-2.9
[git] / t / t0040-parse-options.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Johannes Schindelin
4 #
5
6 test_description='our own option parser'
7
8 . ./test-lib.sh
9
10 cat >expect <<\EOF
11 usage: test-parse-options <options>
12
13     --yes                 get a boolean
14     -D, --no-doubt        begins with 'no-'
15     -B, --no-fear         be brave
16     -b, --boolean         increment by one
17     -4, --or4             bitwise-or boolean with ...0100
18     --neg-or4             same as --no-or4
19
20     -i, --integer <n>     get a integer
21     -j <n>                get a integer, too
22     -m, --magnitude <n>   get a magnitude
23     --set23               set integer to 23
24     -t <time>             get timestamp of <time>
25     -L, --length <str>    get length of <str>
26     -F, --file <file>     set file to <file>
27
28 String options
29     -s, --string <string>
30                           get a string
31     --string2 <str>       get another string
32     --st <st>             get another string (pervert ordering)
33     -o <str>              get another string
34     --list <str>          add str to list
35
36 Magic arguments
37     --quux                means --quux
38     -NUM                  set integer to NUM
39     +                     same as -b
40     --ambiguous           positive ambiguity
41     --no-ambiguous        negative ambiguity
42
43 Standard options
44     --abbrev[=<n>]        use <n> digits to display SHA-1s
45     -v, --verbose         be verbose
46     -n, --dry-run         dry run
47     -q, --quiet           be quiet
48     --expect <string>     expected output in the variable dump
49
50 EOF
51
52 test_expect_success 'test help' '
53         test_must_fail test-parse-options -h >output 2>output.err &&
54         test_must_be_empty output.err &&
55         test_i18ncmp expect output
56 '
57
58 mv expect expect.err
59
60 check () {
61         what="$1" &&
62         shift &&
63         expect="$1" &&
64         shift &&
65         test-parse-options --expect="$what $expect" "$@"
66 }
67
68 check_unknown_i18n() {
69         case "$1" in
70         --*)
71                 echo error: unknown option \`${1#--}\' >expect ;;
72         -*)
73                 echo error: unknown switch \`${1#-}\' >expect ;;
74         esac &&
75         cat expect.err >>expect &&
76         test_must_fail test-parse-options $* >output 2>output.err &&
77         test_must_be_empty output &&
78         test_i18ncmp expect output.err
79 }
80
81 test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
82 test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
83 test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
84 test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
85 test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
86
87 test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
88 test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
89
90 test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
91 test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
92
93 test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
94 test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
95
96 test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
97
98 test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
99
100 test_expect_success 'OPT_MAGNITUDE() simple' '
101         check magnitude: 2345678 -m 2345678
102 '
103
104 test_expect_success 'OPT_MAGNITUDE() kilo' '
105         check magnitude: 239616 -m 234k
106 '
107
108 test_expect_success 'OPT_MAGNITUDE() mega' '
109         check magnitude: 104857600 -m 100m
110 '
111
112 test_expect_success 'OPT_MAGNITUDE() giga' '
113         check magnitude: 1073741824 -m 1g
114 '
115
116 test_expect_success 'OPT_MAGNITUDE() 3giga' '
117         check magnitude: 3221225472 -m 3g
118 '
119
120 cat >expect <<\EOF
121 boolean: 2
122 integer: 1729
123 magnitude: 16384
124 timestamp: 0
125 string: 123
126 abbrev: 7
127 verbose: 2
128 quiet: 0
129 dry run: yes
130 file: prefix/my.file
131 EOF
132
133 test_expect_success 'short options' '
134         test-parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
135         >output 2>output.err &&
136         test_cmp expect output &&
137         test_must_be_empty output.err
138 '
139
140 cat >expect <<\EOF
141 boolean: 2
142 integer: 1729
143 magnitude: 16384
144 timestamp: 0
145 string: 321
146 abbrev: 10
147 verbose: 2
148 quiet: 0
149 dry run: no
150 file: prefix/fi.le
151 EOF
152
153 test_expect_success 'long options' '
154         test-parse-options --boolean --integer 1729 --magnitude 16k \
155                 --boolean --string2=321 --verbose --verbose --no-dry-run \
156                 --abbrev=10 --file fi.le --obsolete \
157                 >output 2>output.err &&
158         test_must_be_empty output.err &&
159         test_cmp expect output
160 '
161
162 test_expect_success 'missing required value' '
163         test_expect_code 129 test-parse-options -s &&
164         test_expect_code 129 test-parse-options --string &&
165         test_expect_code 129 test-parse-options --file
166 '
167
168 cat >expect <<\EOF
169 boolean: 1
170 integer: 13
171 magnitude: 0
172 timestamp: 0
173 string: 123
174 abbrev: 7
175 verbose: -1
176 quiet: 0
177 dry run: no
178 file: (not set)
179 arg 00: a1
180 arg 01: b1
181 arg 02: --boolean
182 EOF
183
184 test_expect_success 'intermingled arguments' '
185         test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
186                 >output 2>output.err &&
187         test_must_be_empty output.err &&
188         test_cmp expect output
189 '
190
191 cat >expect <<\EOF
192 boolean: 0
193 integer: 2
194 magnitude: 0
195 timestamp: 0
196 string: (not set)
197 abbrev: 7
198 verbose: -1
199 quiet: 0
200 dry run: no
201 file: (not set)
202 EOF
203
204 test_expect_success 'unambiguously abbreviated option' '
205         test-parse-options --int 2 --boolean --no-bo >output 2>output.err &&
206         test_must_be_empty output.err &&
207         test_cmp expect output
208 '
209
210 test_expect_success 'unambiguously abbreviated option with "="' '
211         test-parse-options --int=2 >output 2>output.err &&
212         test_must_be_empty output.err &&
213         test_cmp expect output
214 '
215
216 test_expect_success 'ambiguously abbreviated option' '
217         test_expect_code 129 test-parse-options --strin 123
218 '
219
220 cat >expect <<\EOF
221 boolean: 0
222 integer: 0
223 magnitude: 0
224 timestamp: 0
225 string: 123
226 abbrev: 7
227 verbose: -1
228 quiet: 0
229 dry run: no
230 file: (not set)
231 EOF
232
233 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
234         test-parse-options --st 123 >output 2>output.err &&
235         test_must_be_empty output.err &&
236         test_cmp expect output
237 '
238
239 cat >typo.err <<\EOF
240 error: did you mean `--boolean` (with two dashes ?)
241 EOF
242
243 test_expect_success 'detect possible typos' '
244         test_must_fail test-parse-options -boolean >output 2>output.err &&
245         test_must_be_empty output &&
246         test_cmp typo.err output.err
247 '
248
249 cat >typo.err <<\EOF
250 error: did you mean `--ambiguous` (with two dashes ?)
251 EOF
252
253 test_expect_success 'detect possible typos' '
254         test_must_fail test-parse-options -ambiguous >output 2>output.err &&
255         test_must_be_empty output &&
256         test_cmp typo.err output.err
257 '
258
259 cat >expect <<\EOF
260 boolean: 0
261 integer: 0
262 magnitude: 0
263 timestamp: 0
264 string: (not set)
265 abbrev: 7
266 verbose: -1
267 quiet: 0
268 dry run: no
269 file: (not set)
270 arg 00: --quux
271 EOF
272
273 test_expect_success 'keep some options as arguments' '
274         test-parse-options --quux >output 2>output.err &&
275         test_must_be_empty output.err &&
276         test_cmp expect output
277 '
278
279 cat >expect <<\EOF
280 boolean: 0
281 integer: 0
282 magnitude: 0
283 timestamp: 1
284 string: (not set)
285 abbrev: 7
286 verbose: -1
287 quiet: 1
288 dry run: no
289 file: (not set)
290 arg 00: foo
291 EOF
292
293 test_expect_success 'OPT_DATE() works' '
294         test-parse-options -t "1970-01-01 00:00:01 +0000" \
295                 foo -q >output 2>output.err &&
296         test_must_be_empty output.err &&
297         test_cmp expect output
298 '
299
300 cat >expect <<\EOF
301 Callback: "four", 0
302 boolean: 5
303 integer: 4
304 magnitude: 0
305 timestamp: 0
306 string: (not set)
307 abbrev: 7
308 verbose: -1
309 quiet: 0
310 dry run: no
311 file: (not set)
312 EOF
313
314 test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
315         test-parse-options --length=four -b -4 >output 2>output.err &&
316         test_must_be_empty output.err &&
317         test_cmp expect output
318 '
319
320 >expect
321
322 test_expect_success 'OPT_CALLBACK() and callback errors work' '
323         test_must_fail test-parse-options --no-length >output 2>output.err &&
324         test_i18ncmp expect output &&
325         test_i18ncmp expect.err output.err
326 '
327
328 cat >expect <<\EOF
329 boolean: 1
330 integer: 23
331 magnitude: 0
332 timestamp: 0
333 string: (not set)
334 abbrev: 7
335 verbose: -1
336 quiet: 0
337 dry run: no
338 file: (not set)
339 EOF
340
341 test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
342         test-parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
343         test_must_be_empty output.err &&
344         test_cmp expect output
345 '
346
347 test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
348         test-parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
349         test_must_be_empty output.err &&
350         test_cmp expect output
351 '
352
353 cat >expect <<\EOF
354 boolean: 6
355 integer: 0
356 magnitude: 0
357 timestamp: 0
358 string: (not set)
359 abbrev: 7
360 verbose: -1
361 quiet: 0
362 dry run: no
363 file: (not set)
364 EOF
365
366 test_expect_success 'OPT_BIT() works' '
367         test-parse-options -bb --or4 >output 2>output.err &&
368         test_must_be_empty output.err &&
369         test_cmp expect output
370 '
371
372 test_expect_success 'OPT_NEGBIT() works' '
373         test-parse-options -bb --no-neg-or4 >output 2>output.err &&
374         test_must_be_empty output.err &&
375         test_cmp expect output
376 '
377
378 test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
379         test-parse-options + + + + + + >output 2>output.err &&
380         test_must_be_empty output.err &&
381         test_cmp expect output
382 '
383
384 cat >expect <<\EOF
385 boolean: 0
386 integer: 12345
387 magnitude: 0
388 timestamp: 0
389 string: (not set)
390 abbrev: 7
391 verbose: -1
392 quiet: 0
393 dry run: no
394 file: (not set)
395 EOF
396
397 test_expect_success 'OPT_NUMBER_CALLBACK() works' '
398         test-parse-options -12345 >output 2>output.err &&
399         test_must_be_empty output.err &&
400         test_cmp expect output
401 '
402
403 cat >expect <<\EOF
404 boolean: 0
405 integer: 0
406 magnitude: 0
407 timestamp: 0
408 string: (not set)
409 abbrev: 7
410 verbose: -1
411 quiet: 0
412 dry run: no
413 file: (not set)
414 EOF
415
416 test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
417         test-parse-options --no-ambig >output 2>output.err &&
418         test_must_be_empty output.err &&
419         test_cmp expect output
420 '
421
422 cat >>expect <<\EOF
423 list: foo
424 list: bar
425 list: baz
426 EOF
427 test_expect_success '--list keeps list of strings' '
428         test-parse-options --list foo --list=bar --list=baz >output &&
429         test_cmp expect output
430 '
431
432 test_expect_success '--no-list resets list' '
433         test-parse-options --list=other --list=irrelevant --list=options \
434                 --no-list --list=foo --list=bar --list=baz >output &&
435         test_cmp expect output
436 '
437
438 cat >expect <<\EOF
439 boolean: 0
440 integer: 0
441 magnitude: 0
442 timestamp: 0
443 string: (not set)
444 abbrev: 7
445 verbose: -1
446 quiet: 3
447 dry run: no
448 file: (not set)
449 EOF
450
451 test_expect_success 'multiple quiet levels' '
452         test-parse-options -q -q -q >output 2>output.err &&
453         test_must_be_empty output.err &&
454         test_cmp expect output
455 '
456
457 cat >expect <<\EOF
458 boolean: 0
459 integer: 0
460 magnitude: 0
461 timestamp: 0
462 string: (not set)
463 abbrev: 7
464 verbose: 3
465 quiet: 0
466 dry run: no
467 file: (not set)
468 EOF
469
470 test_expect_success 'multiple verbose levels' '
471         test-parse-options -v -v -v >output 2>output.err &&
472         test_must_be_empty output.err &&
473         test_cmp expect output
474 '
475
476 cat >expect <<\EOF
477 boolean: 0
478 integer: 0
479 magnitude: 0
480 timestamp: 0
481 string: (not set)
482 abbrev: 7
483 verbose: -1
484 quiet: 0
485 dry run: no
486 file: (not set)
487 EOF
488
489 test_expect_success '--no-quiet sets --quiet to 0' '
490         test-parse-options --no-quiet >output 2>output.err &&
491         test_must_be_empty output.err &&
492         test_cmp expect output
493 '
494
495 cat >expect <<\EOF
496 boolean: 0
497 integer: 0
498 magnitude: 0
499 timestamp: 0
500 string: (not set)
501 abbrev: 7
502 verbose: -1
503 quiet: 0
504 dry run: no
505 file: (not set)
506 EOF
507
508 test_expect_success '--no-quiet resets multiple -q to 0' '
509         test-parse-options -q -q -q --no-quiet >output 2>output.err &&
510         test_must_be_empty output.err &&
511         test_cmp expect output
512 '
513
514 cat >expect <<\EOF
515 boolean: 0
516 integer: 0
517 magnitude: 0
518 timestamp: 0
519 string: (not set)
520 abbrev: 7
521 verbose: 0
522 quiet: 0
523 dry run: no
524 file: (not set)
525 EOF
526
527 test_expect_success '--no-verbose sets verbose to 0' '
528         test-parse-options --no-verbose >output 2>output.err &&
529         test_must_be_empty output.err &&
530         test_cmp expect output
531 '
532
533 cat >expect <<\EOF
534 boolean: 0
535 integer: 0
536 magnitude: 0
537 timestamp: 0
538 string: (not set)
539 abbrev: 7
540 verbose: 0
541 quiet: 0
542 dry run: no
543 file: (not set)
544 EOF
545
546 test_expect_success '--no-verbose resets multiple verbose to 0' '
547         test-parse-options -v -v -v --no-verbose >output 2>output.err &&
548         test_must_be_empty output.err &&
549         test_cmp expect output
550 '
551
552 test_done