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