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