]>
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 | |
010a2dac SB |
22 | --set23 set integer to 23 |
23 | -t <time> get timestamp of <time> | |
24 | -L, --length <str> get length of <str> | |
41dbcd45 | 25 | -F, --file <file> set file to <file> |
beb47437 | 26 | |
010a2dac | 27 | String options |
beb47437 JS |
28 | -s, --string <string> |
29 | get a string | |
30 | --string2 <str> get another string | |
243e0614 | 31 | --st <st> get another string (pervert ordering) |
3a9f0f41 | 32 | -o <str> get another string |
c8ba1639 | 33 | --list <str> add str to list |
beb47437 | 34 | |
010a2dac | 35 | Magic arguments |
580d5bff | 36 | --quux means --quux |
e0319ff5 | 37 | -NUM set integer to NUM |
51a9949e | 38 | + same as -b |
6bbfd1fa AS |
39 | --ambiguous positive ambiguity |
40 | --no-ambiguous negative ambiguity | |
580d5bff | 41 | |
010a2dac SB |
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 | ||
beb47437 JS |
48 | EOF |
49 | ||
50 | test_expect_success 'test help' ' | |
010a2dac | 51 | test_must_fail test-parse-options -h > output 2> output.err && |
ca8d148d | 52 | test_must_be_empty output.err && |
9a001381 | 53 | test_i18ncmp expect output |
beb47437 JS |
54 | ' |
55 | ||
9c7304e3 GS |
56 | mv expect expect.err |
57 | ||
b9e63ddd RS |
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 && | |
ca8d148d | 77 | test_must_be_empty output.err && |
b9e63ddd RS |
78 | test_cmp expect output |
79 | } | |
80 | ||
9a001381 JX |
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 && | |
ca8d148d | 88 | test_must_be_empty output.err && |
9a001381 JX |
89 | test_i18ncmp expect output |
90 | } | |
91 | ||
b9e63ddd RS |
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 && | |
ca8d148d | 101 | test_must_be_empty output && |
b9e63ddd RS |
102 | test_cmp expect output.err |
103 | } | |
104 | ||
9a001381 JX |
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 && | |
ca8d148d | 114 | test_must_be_empty output && |
9a001381 JX |
115 | test_i18ncmp expect output.err |
116 | } | |
117 | ||
b9e63ddd RS |
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 | ||
9a001381 JX |
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' | |
b9e63ddd | 132 | |
0f1930c5 | 133 | test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt' |
b9e63ddd | 134 | |
beb47437 JS |
135 | cat > expect << EOF |
136 | boolean: 2 | |
137 | integer: 1729 | |
c4aca9cc | 138 | timestamp: 0 |
beb47437 | 139 | string: 123 |
010a2dac SB |
140 | abbrev: 7 |
141 | verbose: 2 | |
142 | quiet: no | |
143 | dry run: yes | |
df217ed6 | 144 | file: prefix/my.file |
beb47437 JS |
145 | EOF |
146 | ||
147 | test_expect_success 'short options' ' | |
df217ed6 SB |
148 | test-parse-options -s123 -b -i 1729 -b -vv -n -F my.file \ |
149 | > output 2> output.err && | |
3af82863 | 150 | test_cmp expect output && |
ca8d148d | 151 | test_must_be_empty output.err |
beb47437 | 152 | ' |
010a2dac | 153 | |
beb47437 JS |
154 | cat > expect << EOF |
155 | boolean: 2 | |
156 | integer: 1729 | |
c4aca9cc | 157 | timestamp: 0 |
beb47437 | 158 | string: 321 |
010a2dac SB |
159 | abbrev: 10 |
160 | verbose: 2 | |
161 | quiet: no | |
162 | dry run: no | |
df217ed6 | 163 | file: prefix/fi.le |
beb47437 JS |
164 | EOF |
165 | ||
166 | test_expect_success 'long options' ' | |
167 | test-parse-options --boolean --integer 1729 --boolean --string2=321 \ | |
df217ed6 | 168 | --verbose --verbose --no-dry-run --abbrev=10 --file fi.le\ |
6acec038 | 169 | --obsolete > output 2> output.err && |
ca8d148d | 170 | test_must_be_empty output.err && |
3af82863 | 171 | test_cmp expect output |
beb47437 JS |
172 | ' |
173 | ||
d5d745f9 | 174 | test_expect_success 'missing required value' ' |
c21fc9d0 JK |
175 | test_expect_code 129 test-parse-options -s && |
176 | test_expect_code 129 test-parse-options --string && | |
177 | test_expect_code 129 test-parse-options --file | |
d5d745f9 OM |
178 | ' |
179 | ||
beb47437 JS |
180 | cat > expect << EOF |
181 | boolean: 1 | |
182 | integer: 13 | |
c4aca9cc | 183 | timestamp: 0 |
beb47437 | 184 | string: 123 |
010a2dac SB |
185 | abbrev: 7 |
186 | verbose: 0 | |
187 | quiet: no | |
188 | dry run: no | |
df217ed6 | 189 | file: (not set) |
beb47437 JS |
190 | arg 00: a1 |
191 | arg 01: b1 | |
192 | arg 02: --boolean | |
193 | EOF | |
194 | ||
195 | test_expect_success 'intermingled arguments' ' | |
196 | test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \ | |
197 | > output 2> output.err && | |
ca8d148d | 198 | test_must_be_empty output.err && |
3af82863 | 199 | test_cmp expect output |
beb47437 JS |
200 | ' |
201 | ||
7f275b91 JS |
202 | cat > expect << EOF |
203 | boolean: 0 | |
204 | integer: 2 | |
c4aca9cc | 205 | timestamp: 0 |
7f275b91 | 206 | string: (not set) |
010a2dac SB |
207 | abbrev: 7 |
208 | verbose: 0 | |
209 | quiet: no | |
210 | dry run: no | |
df217ed6 | 211 | file: (not set) |
7f275b91 JS |
212 | EOF |
213 | ||
214 | test_expect_success 'unambiguously abbreviated option' ' | |
215 | test-parse-options --int 2 --boolean --no-bo > output 2> output.err && | |
ca8d148d | 216 | test_must_be_empty output.err && |
3af82863 | 217 | test_cmp expect output |
7f275b91 JS |
218 | ' |
219 | ||
220 | test_expect_success 'unambiguously abbreviated option with "="' ' | |
221 | test-parse-options --int=2 > output 2> output.err && | |
ca8d148d | 222 | test_must_be_empty output.err && |
3af82863 | 223 | test_cmp expect output |
7f275b91 JS |
224 | ' |
225 | ||
41ac414e | 226 | test_expect_success 'ambiguously abbreviated option' ' |
c21fc9d0 | 227 | test_expect_code 129 test-parse-options --strin 123 |
7f275b91 JS |
228 | ' |
229 | ||
243e0614 JS |
230 | cat > expect << EOF |
231 | boolean: 0 | |
232 | integer: 0 | |
c4aca9cc | 233 | timestamp: 0 |
243e0614 | 234 | string: 123 |
010a2dac SB |
235 | abbrev: 7 |
236 | verbose: 0 | |
237 | quiet: no | |
238 | dry run: no | |
df217ed6 | 239 | file: (not set) |
243e0614 JS |
240 | EOF |
241 | ||
242 | test_expect_success 'non ambiguous option (after two options it abbreviates)' ' | |
243 | test-parse-options --st 123 > output 2> output.err && | |
ca8d148d | 244 | test_must_be_empty output.err && |
3af82863 | 245 | test_cmp expect output |
243e0614 JS |
246 | ' |
247 | ||
010a2dac | 248 | cat > typo.err << EOF |
3a9f0f41 PH |
249 | error: did you mean \`--boolean\` (with two dashes ?) |
250 | EOF | |
251 | ||
252 | test_expect_success 'detect possible typos' ' | |
010a2dac | 253 | test_must_fail test-parse-options -boolean > output 2> output.err && |
ca8d148d | 254 | test_must_be_empty output && |
010a2dac | 255 | test_cmp typo.err output.err |
3a9f0f41 PH |
256 | ' |
257 | ||
38916c5b RS |
258 | cat > typo.err << EOF |
259 | error: did you mean \`--ambiguous\` (with two dashes ?) | |
260 | EOF | |
261 | ||
262 | test_expect_success 'detect possible typos' ' | |
263 | test_must_fail test-parse-options -ambiguous > output 2> output.err && | |
ca8d148d | 264 | test_must_be_empty output && |
38916c5b RS |
265 | test_cmp typo.err output.err |
266 | ' | |
267 | ||
580d5bff PH |
268 | cat > expect <<EOF |
269 | boolean: 0 | |
270 | integer: 0 | |
c4aca9cc | 271 | timestamp: 0 |
580d5bff | 272 | string: (not set) |
010a2dac SB |
273 | abbrev: 7 |
274 | verbose: 0 | |
275 | quiet: no | |
276 | dry run: no | |
df217ed6 | 277 | file: (not set) |
580d5bff PH |
278 | arg 00: --quux |
279 | EOF | |
280 | ||
281 | test_expect_success 'keep some options as arguments' ' | |
282 | test-parse-options --quux > output 2> output.err && | |
ca8d148d | 283 | test_must_be_empty output.err && |
3af82863 | 284 | test_cmp expect output |
580d5bff PH |
285 | ' |
286 | ||
010a2dac SB |
287 | cat > expect <<EOF |
288 | boolean: 0 | |
c4aca9cc JH |
289 | integer: 0 |
290 | timestamp: 1 | |
20d1c652 | 291 | string: (not set) |
010a2dac SB |
292 | abbrev: 7 |
293 | verbose: 0 | |
294 | quiet: yes | |
295 | dry run: no | |
df217ed6 | 296 | file: (not set) |
010a2dac SB |
297 | arg 00: foo |
298 | EOF | |
299 | ||
20d1c652 MR |
300 | test_expect_success 'OPT_DATE() works' ' |
301 | test-parse-options -t "1970-01-01 00:00:01 +0000" \ | |
010a2dac | 302 | foo -q > output 2> output.err && |
ca8d148d | 303 | test_must_be_empty output.err && |
010a2dac SB |
304 | test_cmp expect output |
305 | ' | |
306 | ||
307 | cat > expect <<EOF | |
308 | Callback: "four", 0 | |
309 | boolean: 5 | |
310 | integer: 4 | |
c4aca9cc | 311 | timestamp: 0 |
010a2dac SB |
312 | string: (not set) |
313 | abbrev: 7 | |
314 | verbose: 0 | |
315 | quiet: no | |
316 | dry run: no | |
df217ed6 | 317 | file: (not set) |
010a2dac SB |
318 | EOF |
319 | ||
320 | test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' ' | |
321 | test-parse-options --length=four -b -4 > output 2> output.err && | |
ca8d148d | 322 | test_must_be_empty output.err && |
010a2dac SB |
323 | test_cmp expect output |
324 | ' | |
325 | ||
326 | cat > expect <<EOF | |
327 | Callback: "not set", 1 | |
328 | EOF | |
329 | ||
330 | test_expect_success 'OPT_CALLBACK() and callback errors work' ' | |
331 | test_must_fail test-parse-options --no-length > output 2> output.err && | |
9a001381 JX |
332 | test_i18ncmp expect output && |
333 | test_i18ncmp expect.err output.err | |
010a2dac SB |
334 | ' |
335 | ||
336 | cat > expect <<EOF | |
337 | boolean: 1 | |
338 | integer: 23 | |
c4aca9cc | 339 | timestamp: 0 |
010a2dac SB |
340 | string: (not set) |
341 | abbrev: 7 | |
342 | verbose: 0 | |
343 | quiet: no | |
344 | dry run: no | |
df217ed6 | 345 | file: (not set) |
010a2dac SB |
346 | EOF |
347 | ||
348 | test_expect_success 'OPT_BIT() and OPT_SET_INT() work' ' | |
349 | test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err && | |
ca8d148d | 350 | test_must_be_empty output.err && |
010a2dac SB |
351 | test_cmp expect output |
352 | ' | |
353 | ||
2f4b97f9 RS |
354 | test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' ' |
355 | test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err && | |
ca8d148d | 356 | test_must_be_empty output.err && |
2f4b97f9 RS |
357 | test_cmp expect output |
358 | ' | |
359 | ||
360 | cat > expect <<EOF | |
361 | boolean: 6 | |
362 | integer: 0 | |
363 | timestamp: 0 | |
364 | string: (not set) | |
365 | abbrev: 7 | |
366 | verbose: 0 | |
367 | quiet: no | |
368 | dry run: no | |
df217ed6 | 369 | file: (not set) |
2f4b97f9 RS |
370 | EOF |
371 | ||
372 | test_expect_success 'OPT_BIT() works' ' | |
373 | test-parse-options -bb --or4 > output 2> output.err && | |
ca8d148d | 374 | test_must_be_empty output.err && |
2f4b97f9 RS |
375 | test_cmp expect output |
376 | ' | |
377 | ||
378 | test_expect_success 'OPT_NEGBIT() works' ' | |
379 | test-parse-options -bb --no-neg-or4 > output 2> output.err && | |
ca8d148d | 380 | test_must_be_empty output.err && |
2f4b97f9 RS |
381 | test_cmp expect output |
382 | ' | |
010a2dac | 383 | |
b9e63ddd | 384 | test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' ' |
51a9949e | 385 | test-parse-options + + + + + + > output 2> output.err && |
ca8d148d | 386 | test_must_be_empty output.err && |
51a9949e RS |
387 | test_cmp expect output |
388 | ' | |
389 | ||
e0319ff5 RS |
390 | cat > expect <<EOF |
391 | boolean: 0 | |
392 | integer: 12345 | |
393 | timestamp: 0 | |
394 | string: (not set) | |
395 | abbrev: 7 | |
396 | verbose: 0 | |
397 | quiet: no | |
398 | dry run: no | |
df217ed6 | 399 | file: (not set) |
e0319ff5 RS |
400 | EOF |
401 | ||
402 | test_expect_success 'OPT_NUMBER_CALLBACK() works' ' | |
403 | test-parse-options -12345 > output 2> output.err && | |
ca8d148d | 404 | test_must_be_empty output.err && |
e0319ff5 RS |
405 | test_cmp expect output |
406 | ' | |
407 | ||
6bbfd1fa AS |
408 | cat >expect <<EOF |
409 | boolean: 0 | |
410 | integer: 0 | |
411 | timestamp: 0 | |
412 | string: (not set) | |
413 | abbrev: 7 | |
414 | verbose: 0 | |
415 | quiet: no | |
416 | dry run: no | |
417 | file: (not set) | |
418 | EOF | |
419 | ||
420 | test_expect_success 'negation of OPT_NONEG flags is not ambiguous' ' | |
421 | test-parse-options --no-ambig >output 2>output.err && | |
ca8d148d | 422 | test_must_be_empty output.err && |
6bbfd1fa AS |
423 | test_cmp expect output |
424 | ' | |
425 | ||
c8ba1639 JK |
426 | cat >>expect <<'EOF' |
427 | list: foo | |
428 | list: bar | |
429 | list: baz | |
430 | EOF | |
431 | test_expect_success '--list keeps list of strings' ' | |
432 | test-parse-options --list foo --list=bar --list=baz >output && | |
433 | test_cmp expect output | |
434 | ' | |
435 | ||
436 | test_expect_success '--no-list resets list' ' | |
437 | test-parse-options --list=other --list=irrelevant --list=options \ | |
438 | --no-list --list=foo --list=bar --list=baz >output && | |
439 | test_cmp expect output | |
440 | ' | |
441 | ||
beb47437 | 442 | test_done |