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