]>
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 OM |
174 | test_expect_success 'missing required value' ' |
175 | test-parse-options -s; | |
176 | test $? = 129 && | |
177 | test-parse-options --string; | |
df217ed6 SB |
178 | test $? = 129 && |
179 | test-parse-options --file; | |
d5d745f9 OM |
180 | test $? = 129 |
181 | ' | |
182 | ||
beb47437 JS |
183 | cat > expect << EOF |
184 | boolean: 1 | |
185 | integer: 13 | |
c4aca9cc | 186 | timestamp: 0 |
beb47437 | 187 | string: 123 |
010a2dac SB |
188 | abbrev: 7 |
189 | verbose: 0 | |
190 | quiet: no | |
191 | dry run: no | |
df217ed6 | 192 | file: (not set) |
beb47437 JS |
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 && | |
ca8d148d | 201 | test_must_be_empty output.err && |
3af82863 | 202 | test_cmp expect output |
beb47437 JS |
203 | ' |
204 | ||
7f275b91 JS |
205 | cat > expect << EOF |
206 | boolean: 0 | |
207 | integer: 2 | |
c4aca9cc | 208 | timestamp: 0 |
7f275b91 | 209 | string: (not set) |
010a2dac SB |
210 | abbrev: 7 |
211 | verbose: 0 | |
212 | quiet: no | |
213 | dry run: no | |
df217ed6 | 214 | file: (not set) |
7f275b91 JS |
215 | EOF |
216 | ||
217 | test_expect_success 'unambiguously abbreviated option' ' | |
218 | test-parse-options --int 2 --boolean --no-bo > output 2> output.err && | |
ca8d148d | 219 | test_must_be_empty output.err && |
3af82863 | 220 | test_cmp expect output |
7f275b91 JS |
221 | ' |
222 | ||
223 | test_expect_success 'unambiguously abbreviated option with "="' ' | |
224 | test-parse-options --int=2 > output 2> output.err && | |
ca8d148d | 225 | test_must_be_empty output.err && |
3af82863 | 226 | test_cmp expect output |
7f275b91 JS |
227 | ' |
228 | ||
41ac414e | 229 | test_expect_success 'ambiguously abbreviated option' ' |
7f275b91 | 230 | test-parse-options --strin 123; |
41ac414e | 231 | test $? = 129 |
7f275b91 JS |
232 | ' |
233 | ||
243e0614 JS |
234 | cat > expect << EOF |
235 | boolean: 0 | |
236 | integer: 0 | |
c4aca9cc | 237 | timestamp: 0 |
243e0614 | 238 | string: 123 |
010a2dac SB |
239 | abbrev: 7 |
240 | verbose: 0 | |
241 | quiet: no | |
242 | dry run: no | |
df217ed6 | 243 | file: (not set) |
243e0614 JS |
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 && | |
ca8d148d | 248 | test_must_be_empty output.err && |
3af82863 | 249 | test_cmp expect output |
243e0614 JS |
250 | ' |
251 | ||
010a2dac | 252 | cat > typo.err << EOF |
3a9f0f41 PH |
253 | error: did you mean \`--boolean\` (with two dashes ?) |
254 | EOF | |
255 | ||
256 | test_expect_success 'detect possible typos' ' | |
010a2dac | 257 | test_must_fail test-parse-options -boolean > output 2> output.err && |
ca8d148d | 258 | test_must_be_empty output && |
010a2dac | 259 | test_cmp typo.err output.err |
3a9f0f41 PH |
260 | ' |
261 | ||
38916c5b RS |
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 && | |
ca8d148d | 268 | test_must_be_empty output && |
38916c5b RS |
269 | test_cmp typo.err output.err |
270 | ' | |
271 | ||
580d5bff PH |
272 | cat > expect <<EOF |
273 | boolean: 0 | |
274 | integer: 0 | |
c4aca9cc | 275 | timestamp: 0 |
580d5bff | 276 | string: (not set) |
010a2dac SB |
277 | abbrev: 7 |
278 | verbose: 0 | |
279 | quiet: no | |
280 | dry run: no | |
df217ed6 | 281 | file: (not set) |
580d5bff PH |
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 && | |
ca8d148d | 287 | test_must_be_empty output.err && |
3af82863 | 288 | test_cmp expect output |
580d5bff PH |
289 | ' |
290 | ||
010a2dac SB |
291 | cat > expect <<EOF |
292 | boolean: 0 | |
c4aca9cc JH |
293 | integer: 0 |
294 | timestamp: 1 | |
20d1c652 | 295 | string: (not set) |
010a2dac SB |
296 | abbrev: 7 |
297 | verbose: 0 | |
298 | quiet: yes | |
299 | dry run: no | |
df217ed6 | 300 | file: (not set) |
010a2dac SB |
301 | arg 00: foo |
302 | EOF | |
303 | ||
20d1c652 MR |
304 | test_expect_success 'OPT_DATE() works' ' |
305 | test-parse-options -t "1970-01-01 00:00:01 +0000" \ | |
010a2dac | 306 | foo -q > output 2> output.err && |
ca8d148d | 307 | test_must_be_empty output.err && |
010a2dac SB |
308 | test_cmp expect output |
309 | ' | |
310 | ||
311 | cat > expect <<EOF | |
312 | Callback: "four", 0 | |
313 | boolean: 5 | |
314 | integer: 4 | |
c4aca9cc | 315 | timestamp: 0 |
010a2dac SB |
316 | string: (not set) |
317 | abbrev: 7 | |
318 | verbose: 0 | |
319 | quiet: no | |
320 | dry run: no | |
df217ed6 | 321 | file: (not set) |
010a2dac SB |
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 && | |
ca8d148d | 326 | test_must_be_empty output.err && |
010a2dac SB |
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 && | |
9a001381 JX |
336 | test_i18ncmp expect output && |
337 | test_i18ncmp expect.err output.err | |
010a2dac SB |
338 | ' |
339 | ||
340 | cat > expect <<EOF | |
341 | boolean: 1 | |
342 | integer: 23 | |
c4aca9cc | 343 | timestamp: 0 |
010a2dac SB |
344 | string: (not set) |
345 | abbrev: 7 | |
346 | verbose: 0 | |
347 | quiet: no | |
348 | dry run: no | |
df217ed6 | 349 | file: (not set) |
010a2dac SB |
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 && | |
ca8d148d | 354 | test_must_be_empty output.err && |
010a2dac SB |
355 | test_cmp expect output |
356 | ' | |
357 | ||
2f4b97f9 RS |
358 | test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' ' |
359 | test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err && | |
ca8d148d | 360 | test_must_be_empty output.err && |
2f4b97f9 RS |
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 | |
df217ed6 | 373 | file: (not set) |
2f4b97f9 RS |
374 | EOF |
375 | ||
376 | test_expect_success 'OPT_BIT() works' ' | |
377 | test-parse-options -bb --or4 > output 2> output.err && | |
ca8d148d | 378 | test_must_be_empty output.err && |
2f4b97f9 RS |
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 && | |
ca8d148d | 384 | test_must_be_empty output.err && |
2f4b97f9 RS |
385 | test_cmp expect output |
386 | ' | |
010a2dac | 387 | |
b9e63ddd | 388 | test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' ' |
51a9949e | 389 | test-parse-options + + + + + + > output 2> output.err && |
ca8d148d | 390 | test_must_be_empty output.err && |
51a9949e RS |
391 | test_cmp expect output |
392 | ' | |
393 | ||
e0319ff5 RS |
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 | |
df217ed6 | 403 | file: (not set) |
e0319ff5 RS |
404 | EOF |
405 | ||
406 | test_expect_success 'OPT_NUMBER_CALLBACK() works' ' | |
407 | test-parse-options -12345 > output 2> output.err && | |
ca8d148d | 408 | test_must_be_empty output.err && |
e0319ff5 RS |
409 | test_cmp expect output |
410 | ' | |
411 | ||
6bbfd1fa AS |
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 && | |
ca8d148d | 426 | test_must_be_empty output.err && |
6bbfd1fa AS |
427 | test_cmp expect output |
428 | ' | |
429 | ||
c8ba1639 JK |
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 | ||
beb47437 | 446 | test_done |