Merge branch 'bc/asciidoc-pretty-formats-fix'
[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     --set23               set integer to 23
23     -t <time>             get timestamp of <time>
24     -L, --length <str>    get length of <str>
25     -F, --file <file>     set file to <file>
26
27 String options
28     -s, --string <string>
29                           get a string
30     --string2 <str>       get another string
31     --st <st>             get another string (pervert ordering)
32     -o <str>              get another string
33     --list <str>          add str to list
34
35 Magic arguments
36     --quux                means --quux
37     -NUM                  set integer to NUM
38     +                     same as -b
39     --ambiguous           positive ambiguity
40     --no-ambiguous        negative ambiguity
41
42 Standard options
43     --abbrev[=<n>]        use <n> digits to display SHA-1s
44     -v, --verbose         be verbose
45     -n, --dry-run         dry run
46     -q, --quiet           be quiet
47
48 EOF
49
50 test_expect_success 'test help' '
51         test_must_fail test-parse-options -h > output 2> output.err &&
52         test_must_be_empty output.err &&
53         test_i18ncmp expect output
54 '
55
56 mv expect expect.err
57
58 cat >expect.template <<EOF
59 boolean: 0
60 integer: 0
61 timestamp: 0
62 string: (not set)
63 abbrev: 7
64 verbose: 0
65 quiet: no
66 dry run: no
67 file: (not set)
68 EOF
69
70 check() {
71         what="$1" &&
72         shift &&
73         expect="$1" &&
74         shift &&
75         sed "s/^$what .*/$what $expect/" <expect.template >expect &&
76         test-parse-options $* >output 2>output.err &&
77         test_must_be_empty output.err &&
78         test_cmp expect output
79 }
80
81 check_i18n() {
82         what="$1" &&
83         shift &&
84         expect="$1" &&
85         shift &&
86         sed "s/^$what .*/$what $expect/" <expect.template >expect &&
87         test-parse-options $* >output 2>output.err &&
88         test_must_be_empty output.err &&
89         test_i18ncmp expect output
90 }
91
92 check_unknown() {
93         case "$1" in
94         --*)
95                 echo error: unknown option \`${1#--}\' >expect ;;
96         -*)
97                 echo error: unknown switch \`${1#-}\' >expect ;;
98         esac &&
99         cat expect.err >>expect &&
100         test_must_fail test-parse-options $* >output 2>output.err &&
101         test_must_be_empty output &&
102         test_cmp expect output.err
103 }
104
105 check_unknown_i18n() {
106         case "$1" in
107         --*)
108                 echo error: unknown option \`${1#--}\' >expect ;;
109         -*)
110                 echo error: unknown switch \`${1#-}\' >expect ;;
111         esac &&
112         cat expect.err >>expect &&
113         test_must_fail test-parse-options $* >output 2>output.err &&
114         test_must_be_empty output &&
115         test_i18ncmp expect output.err
116 }
117
118 test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
119 test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
120 test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
121 test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
122 test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
123
124 test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
125 test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
126
127 test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
128 test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
129
130 test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
131 test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
132
133 test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
134
135 cat > expect << EOF
136 boolean: 2
137 integer: 1729
138 timestamp: 0
139 string: 123
140 abbrev: 7
141 verbose: 2
142 quiet: no
143 dry run: yes
144 file: prefix/my.file
145 EOF
146
147 test_expect_success 'short options' '
148         test-parse-options -s123 -b -i 1729 -b -vv -n -F my.file \
149         > output 2> output.err &&
150         test_cmp expect output &&
151         test_must_be_empty output.err
152 '
153
154 cat > expect << EOF
155 boolean: 2
156 integer: 1729
157 timestamp: 0
158 string: 321
159 abbrev: 10
160 verbose: 2
161 quiet: no
162 dry run: no
163 file: prefix/fi.le
164 EOF
165
166 test_expect_success 'long options' '
167         test-parse-options --boolean --integer 1729 --boolean --string2=321 \
168                 --verbose --verbose --no-dry-run --abbrev=10 --file fi.le\
169                 --obsolete > output 2> output.err &&
170         test_must_be_empty output.err &&
171         test_cmp expect output
172 '
173
174 test_expect_success 'missing required value' '
175         test-parse-options -s;
176         test $? = 129 &&
177         test-parse-options --string;
178         test $? = 129 &&
179         test-parse-options --file;
180         test $? = 129
181 '
182
183 cat > expect << EOF
184 boolean: 1
185 integer: 13
186 timestamp: 0
187 string: 123
188 abbrev: 7
189 verbose: 0
190 quiet: no
191 dry run: no
192 file: (not set)
193 arg 00: a1
194 arg 01: b1
195 arg 02: --boolean
196 EOF
197
198 test_expect_success 'intermingled arguments' '
199         test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
200                 > output 2> output.err &&
201         test_must_be_empty output.err &&
202         test_cmp expect output
203 '
204
205 cat > expect << EOF
206 boolean: 0
207 integer: 2
208 timestamp: 0
209 string: (not set)
210 abbrev: 7
211 verbose: 0
212 quiet: no
213 dry run: no
214 file: (not set)
215 EOF
216
217 test_expect_success 'unambiguously abbreviated option' '
218         test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
219         test_must_be_empty output.err &&
220         test_cmp expect output
221 '
222
223 test_expect_success 'unambiguously abbreviated option with "="' '
224         test-parse-options --int=2 > output 2> output.err &&
225         test_must_be_empty output.err &&
226         test_cmp expect output
227 '
228
229 test_expect_success 'ambiguously abbreviated option' '
230         test-parse-options --strin 123;
231         test $? = 129
232 '
233
234 cat > expect << EOF
235 boolean: 0
236 integer: 0
237 timestamp: 0
238 string: 123
239 abbrev: 7
240 verbose: 0
241 quiet: no
242 dry run: no
243 file: (not set)
244 EOF
245
246 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
247         test-parse-options --st 123 > output 2> output.err &&
248         test_must_be_empty output.err &&
249         test_cmp expect output
250 '
251
252 cat > typo.err << EOF
253 error: did you mean \`--boolean\` (with two dashes ?)
254 EOF
255
256 test_expect_success 'detect possible typos' '
257         test_must_fail test-parse-options -boolean > output 2> output.err &&
258         test_must_be_empty output &&
259         test_cmp typo.err output.err
260 '
261
262 cat > typo.err << EOF
263 error: did you mean \`--ambiguous\` (with two dashes ?)
264 EOF
265
266 test_expect_success 'detect possible typos' '
267         test_must_fail test-parse-options -ambiguous > output 2> output.err &&
268         test_must_be_empty output &&
269         test_cmp typo.err output.err
270 '
271
272 cat > expect <<EOF
273 boolean: 0
274 integer: 0
275 timestamp: 0
276 string: (not set)
277 abbrev: 7
278 verbose: 0
279 quiet: no
280 dry run: no
281 file: (not set)
282 arg 00: --quux
283 EOF
284
285 test_expect_success 'keep some options as arguments' '
286         test-parse-options --quux > output 2> output.err &&
287         test_must_be_empty output.err &&
288         test_cmp expect output
289 '
290
291 cat > expect <<EOF
292 boolean: 0
293 integer: 0
294 timestamp: 1
295 string: (not set)
296 abbrev: 7
297 verbose: 0
298 quiet: yes
299 dry run: no
300 file: (not set)
301 arg 00: foo
302 EOF
303
304 test_expect_success 'OPT_DATE() works' '
305         test-parse-options -t "1970-01-01 00:00:01 +0000" \
306                 foo -q > output 2> output.err &&
307         test_must_be_empty output.err &&
308         test_cmp expect output
309 '
310
311 cat > expect <<EOF
312 Callback: "four", 0
313 boolean: 5
314 integer: 4
315 timestamp: 0
316 string: (not set)
317 abbrev: 7
318 verbose: 0
319 quiet: no
320 dry run: no
321 file: (not set)
322 EOF
323
324 test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
325         test-parse-options --length=four -b -4 > output 2> output.err &&
326         test_must_be_empty output.err &&
327         test_cmp expect output
328 '
329
330 cat > expect <<EOF
331 Callback: "not set", 1
332 EOF
333
334 test_expect_success 'OPT_CALLBACK() and callback errors work' '
335         test_must_fail test-parse-options --no-length > output 2> output.err &&
336         test_i18ncmp expect output &&
337         test_i18ncmp expect.err output.err
338 '
339
340 cat > expect <<EOF
341 boolean: 1
342 integer: 23
343 timestamp: 0
344 string: (not set)
345 abbrev: 7
346 verbose: 0
347 quiet: no
348 dry run: no
349 file: (not set)
350 EOF
351
352 test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
353         test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err &&
354         test_must_be_empty output.err &&
355         test_cmp expect output
356 '
357
358 test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
359         test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err &&
360         test_must_be_empty output.err &&
361         test_cmp expect output
362 '
363
364 cat > expect <<EOF
365 boolean: 6
366 integer: 0
367 timestamp: 0
368 string: (not set)
369 abbrev: 7
370 verbose: 0
371 quiet: no
372 dry run: no
373 file: (not set)
374 EOF
375
376 test_expect_success 'OPT_BIT() works' '
377         test-parse-options -bb --or4 > output 2> output.err &&
378         test_must_be_empty output.err &&
379         test_cmp expect output
380 '
381
382 test_expect_success 'OPT_NEGBIT() works' '
383         test-parse-options -bb --no-neg-or4 > output 2> output.err &&
384         test_must_be_empty output.err &&
385         test_cmp expect output
386 '
387
388 test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
389         test-parse-options + + + + + + > output 2> output.err &&
390         test_must_be_empty output.err &&
391         test_cmp expect output
392 '
393
394 cat > expect <<EOF
395 boolean: 0
396 integer: 12345
397 timestamp: 0
398 string: (not set)
399 abbrev: 7
400 verbose: 0
401 quiet: no
402 dry run: no
403 file: (not set)
404 EOF
405
406 test_expect_success 'OPT_NUMBER_CALLBACK() works' '
407         test-parse-options -12345 > output 2> output.err &&
408         test_must_be_empty output.err &&
409         test_cmp expect output
410 '
411
412 cat >expect <<EOF
413 boolean: 0
414 integer: 0
415 timestamp: 0
416 string: (not set)
417 abbrev: 7
418 verbose: 0
419 quiet: no
420 dry run: no
421 file: (not set)
422 EOF
423
424 test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
425         test-parse-options --no-ambig >output 2>output.err &&
426         test_must_be_empty output.err &&
427         test_cmp expect output
428 '
429
430 cat >>expect <<'EOF'
431 list: foo
432 list: bar
433 list: baz
434 EOF
435 test_expect_success '--list keeps list of strings' '
436         test-parse-options --list foo --list=bar --list=baz >output &&
437         test_cmp expect output
438 '
439
440 test_expect_success '--no-list resets list' '
441         test-parse-options --list=other --list=irrelevant --list=options \
442                 --no-list --list=foo --list=bar --list=baz >output &&
443         test_cmp expect output
444 '
445
446 test_done