]>
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 | ||
c0b80e05 | 8 | TEST_PASSES_SANITIZE_LEAK=true |
beb47437 JS |
9 | . ./test-lib.sh |
10 | ||
8425b7ea | 11 | cat >expect <<\EOF |
2f17c78c | 12 | usage: test-tool parse-options <options> |
beb47437 | 13 | |
c97ee171 BC |
14 | A helper function for the parse-options API. |
15 | ||
b9e63ddd RS |
16 | --yes get a boolean |
17 | -D, --no-doubt begins with 'no-' | |
18 | -B, --no-fear be brave | |
19 | -b, --boolean increment by one | |
010a2dac | 20 | -4, --or4 bitwise-or boolean with ...0100 |
2f4b97f9 | 21 | --neg-or4 same as --no-or4 |
010a2dac | 22 | |
beb47437 JS |
23 | -i, --integer <n> get a integer |
24 | -j <n> get a integer, too | |
2a514ed8 | 25 | -m, --magnitude <n> get a magnitude |
010a2dac | 26 | --set23 set integer to 23 |
62e7a6f7 PB |
27 | --mode1 set integer to 1 (cmdmode option) |
28 | --mode2 set integer to 2 (cmdmode option) | |
010a2dac | 29 | -L, --length <str> get length of <str> |
41dbcd45 | 30 | -F, --file <file> set file to <file> |
beb47437 | 31 | |
010a2dac | 32 | String options |
c512643e | 33 | -s, --string <string> get a string |
beb47437 | 34 | --string2 <str> get another string |
243e0614 | 35 | --st <st> get another string (pervert ordering) |
3a9f0f41 | 36 | -o <str> get another string |
448abbba JH |
37 | --longhelp help text of this entry |
38 | spans multiple lines | |
c8ba1639 | 39 | --list <str> add str to list |
beb47437 | 40 | |
010a2dac | 41 | Magic arguments |
e0319ff5 | 42 | -NUM set integer to NUM |
51a9949e | 43 | + same as -b |
6bbfd1fa AS |
44 | --ambiguous positive ambiguity |
45 | --no-ambiguous negative ambiguity | |
580d5bff | 46 | |
010a2dac | 47 | Standard options |
4279000d | 48 | --abbrev[=<n>] use <n> digits to display object names |
010a2dac SB |
49 | -v, --verbose be verbose |
50 | -n, --dry-run dry run | |
51 | -q, --quiet be quiet | |
ab6b28b0 | 52 | --expect <string> expected output in the variable dump |
010a2dac | 53 | |
5c387428 NTND |
54 | Alias |
55 | -A, --alias-source <string> | |
56 | get a string | |
57 | -Z, --alias-target <string> | |
7c280589 | 58 | alias of --alias-source |
5c387428 | 59 | |
beb47437 JS |
60 | EOF |
61 | ||
62 | test_expect_success 'test help' ' | |
2f17c78c | 63 | test_must_fail test-tool parse-options -h >output 2>output.err && |
ca8d148d | 64 | test_must_be_empty output.err && |
1108cea7 | 65 | test_cmp expect output |
beb47437 JS |
66 | ' |
67 | ||
9c7304e3 GS |
68 | mv expect expect.err |
69 | ||
8ca65aeb | 70 | check () { |
b9e63ddd RS |
71 | what="$1" && |
72 | shift && | |
73 | expect="$1" && | |
74 | shift && | |
2f17c78c | 75 | test-tool parse-options --expect="$what $expect" "$@" |
b9e63ddd RS |
76 | } |
77 | ||
9a001381 JX |
78 | check_unknown_i18n() { |
79 | case "$1" in | |
80 | --*) | |
81 | echo error: unknown option \`${1#--}\' >expect ;; | |
82 | -*) | |
83 | echo error: unknown switch \`${1#-}\' >expect ;; | |
84 | esac && | |
85 | cat expect.err >>expect && | |
2f17c78c | 86 | test_must_fail test-tool parse-options $* >output 2>output.err && |
ca8d148d | 87 | test_must_be_empty output && |
1108cea7 | 88 | test_cmp expect output.err |
9a001381 JX |
89 | } |
90 | ||
b9e63ddd RS |
91 | test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes' |
92 | test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt' | |
93 | test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D' | |
94 | test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear' | |
95 | test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B' | |
96 | ||
97 | test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes' | |
98 | test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB' | |
99 | ||
100 | test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes' | |
101 | test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt' | |
102 | ||
1a9bf1e1 BC |
103 | test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear' |
104 | test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear' | |
b9e63ddd | 105 | |
0f1930c5 | 106 | test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt' |
b9e63ddd | 107 | |
81a48cc0 CB |
108 | test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345' |
109 | ||
2a514ed8 CB |
110 | test_expect_success 'OPT_MAGNITUDE() simple' ' |
111 | check magnitude: 2345678 -m 2345678 | |
112 | ' | |
113 | ||
114 | test_expect_success 'OPT_MAGNITUDE() kilo' ' | |
115 | check magnitude: 239616 -m 234k | |
116 | ' | |
117 | ||
118 | test_expect_success 'OPT_MAGNITUDE() mega' ' | |
119 | check magnitude: 104857600 -m 100m | |
120 | ' | |
121 | ||
122 | test_expect_success 'OPT_MAGNITUDE() giga' ' | |
123 | check magnitude: 1073741824 -m 1g | |
124 | ' | |
125 | ||
126 | test_expect_success 'OPT_MAGNITUDE() 3giga' ' | |
127 | check magnitude: 3221225472 -m 3g | |
128 | ' | |
129 | ||
8425b7ea | 130 | cat >expect <<\EOF |
beb47437 JS |
131 | boolean: 2 |
132 | integer: 1729 | |
2a514ed8 | 133 | magnitude: 16384 |
c4aca9cc | 134 | timestamp: 0 |
beb47437 | 135 | string: 123 |
010a2dac SB |
136 | abbrev: 7 |
137 | verbose: 2 | |
36e6a5ba | 138 | quiet: 0 |
010a2dac | 139 | dry run: yes |
df217ed6 | 140 | file: prefix/my.file |
beb47437 JS |
141 | EOF |
142 | ||
143 | test_expect_success 'short options' ' | |
2f17c78c | 144 | test-tool parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \ |
2a514ed8 | 145 | >output 2>output.err && |
3af82863 | 146 | test_cmp expect output && |
ca8d148d | 147 | test_must_be_empty output.err |
beb47437 | 148 | ' |
010a2dac | 149 | |
8425b7ea | 150 | cat >expect <<\EOF |
beb47437 JS |
151 | boolean: 2 |
152 | integer: 1729 | |
2a514ed8 | 153 | magnitude: 16384 |
c4aca9cc | 154 | timestamp: 0 |
beb47437 | 155 | string: 321 |
010a2dac SB |
156 | abbrev: 10 |
157 | verbose: 2 | |
36e6a5ba | 158 | quiet: 0 |
010a2dac | 159 | dry run: no |
df217ed6 | 160 | file: prefix/fi.le |
beb47437 JS |
161 | EOF |
162 | ||
163 | test_expect_success 'long options' ' | |
2f17c78c | 164 | test-tool parse-options --boolean --integer 1729 --magnitude 16k \ |
2a514ed8 CB |
165 | --boolean --string2=321 --verbose --verbose --no-dry-run \ |
166 | --abbrev=10 --file fi.le --obsolete \ | |
167 | >output 2>output.err && | |
ca8d148d | 168 | test_must_be_empty output.err && |
3af82863 | 169 | test_cmp expect output |
beb47437 JS |
170 | ' |
171 | ||
d5d745f9 | 172 | test_expect_success 'missing required value' ' |
62f2ffc5 ÆAB |
173 | cat >expect <<-\EOF && |
174 | error: switch `s'\'' requires a value | |
175 | EOF | |
176 | test_expect_code 129 test-tool parse-options -s 2>actual && | |
177 | test_cmp expect actual && | |
178 | ||
179 | cat >expect <<-\EOF && | |
180 | error: option `string'\'' requires a value | |
181 | EOF | |
182 | test_expect_code 129 test-tool parse-options --string 2>actual && | |
183 | test_cmp expect actual && | |
184 | ||
185 | cat >expect <<-\EOF && | |
186 | error: option `file'\'' requires a value | |
187 | EOF | |
188 | test_expect_code 129 test-tool parse-options --file 2>actual && | |
189 | test_cmp expect actual | |
190 | ' | |
191 | ||
192 | test_expect_success 'superfluous value provided: boolean' ' | |
193 | cat >expect <<-\EOF && | |
194 | error: option `yes'\'' takes no value | |
195 | EOF | |
196 | test_expect_code 129 test-tool parse-options --yes=hi 2>actual && | |
197 | test_cmp expect actual && | |
198 | ||
199 | cat >expect <<-\EOF && | |
200 | error: option `no-yes'\'' takes no value | |
201 | EOF | |
202 | test_expect_code 129 test-tool parse-options --no-yes=hi 2>actual && | |
203 | test_cmp expect actual | |
204 | ' | |
205 | ||
206 | test_expect_success 'superfluous value provided: cmdmode' ' | |
207 | cat >expect <<-\EOF && | |
208 | error: option `mode1'\'' takes no value | |
209 | EOF | |
210 | test_expect_code 129 test-tool parse-options --mode1=hi 2>actual && | |
211 | test_cmp expect actual | |
d5d745f9 OM |
212 | ' |
213 | ||
8425b7ea | 214 | cat >expect <<\EOF |
beb47437 JS |
215 | boolean: 1 |
216 | integer: 13 | |
2a514ed8 | 217 | magnitude: 0 |
c4aca9cc | 218 | timestamp: 0 |
beb47437 | 219 | string: 123 |
010a2dac | 220 | abbrev: 7 |
e0070e8b | 221 | verbose: -1 |
36e6a5ba | 222 | quiet: 0 |
010a2dac | 223 | dry run: no |
df217ed6 | 224 | file: (not set) |
beb47437 JS |
225 | arg 00: a1 |
226 | arg 01: b1 | |
227 | arg 02: --boolean | |
228 | EOF | |
229 | ||
230 | test_expect_success 'intermingled arguments' ' | |
2f17c78c | 231 | test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \ |
8425b7ea | 232 | >output 2>output.err && |
ca8d148d | 233 | test_must_be_empty output.err && |
3af82863 | 234 | test_cmp expect output |
beb47437 JS |
235 | ' |
236 | ||
8425b7ea | 237 | cat >expect <<\EOF |
7f275b91 JS |
238 | boolean: 0 |
239 | integer: 2 | |
2a514ed8 | 240 | magnitude: 0 |
c4aca9cc | 241 | timestamp: 0 |
7f275b91 | 242 | string: (not set) |
010a2dac | 243 | abbrev: 7 |
e0070e8b | 244 | verbose: -1 |
36e6a5ba | 245 | quiet: 0 |
010a2dac | 246 | dry run: no |
df217ed6 | 247 | file: (not set) |
7f275b91 JS |
248 | EOF |
249 | ||
250 | test_expect_success 'unambiguously abbreviated option' ' | |
b02e7d5d | 251 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
2f17c78c | 252 | test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err && |
ca8d148d | 253 | test_must_be_empty output.err && |
3af82863 | 254 | test_cmp expect output |
7f275b91 JS |
255 | ' |
256 | ||
257 | test_expect_success 'unambiguously abbreviated option with "="' ' | |
b02e7d5d | 258 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
2f17c78c | 259 | test-tool parse-options --expect="integer: 2" --int=2 |
7f275b91 JS |
260 | ' |
261 | ||
41ac414e | 262 | test_expect_success 'ambiguously abbreviated option' ' |
b02e7d5d JS |
263 | test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
264 | test-tool parse-options --strin 123 | |
7f275b91 JS |
265 | ' |
266 | ||
243e0614 | 267 | test_expect_success 'non ambiguous option (after two options it abbreviates)' ' |
b02e7d5d | 268 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
2f17c78c | 269 | test-tool parse-options --expect="string: 123" --st 123 |
243e0614 JS |
270 | ' |
271 | ||
5c387428 NTND |
272 | test_expect_success 'Alias options do not contribute to abbreviation' ' |
273 | test-tool parse-options --alias-source 123 >output && | |
274 | grep "^string: 123" output && | |
275 | test-tool parse-options --alias-target 123 >output && | |
276 | grep "^string: 123" output && | |
277 | test_must_fail test-tool parse-options --alias && | |
278 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ | |
279 | test-tool parse-options --alias 123 >output && | |
280 | grep "^string: 123" output | |
281 | ' | |
282 | ||
8425b7ea | 283 | cat >typo.err <<\EOF |
395518cf | 284 | error: did you mean `--boolean` (with two dashes)? |
3a9f0f41 PH |
285 | EOF |
286 | ||
287 | test_expect_success 'detect possible typos' ' | |
2f17c78c | 288 | test_must_fail test-tool parse-options -boolean >output 2>output.err && |
ca8d148d | 289 | test_must_be_empty output && |
1108cea7 | 290 | test_cmp typo.err output.err |
3a9f0f41 PH |
291 | ' |
292 | ||
8425b7ea | 293 | cat >typo.err <<\EOF |
395518cf | 294 | error: did you mean `--ambiguous` (with two dashes)? |
38916c5b RS |
295 | EOF |
296 | ||
297 | test_expect_success 'detect possible typos' ' | |
2f17c78c | 298 | test_must_fail test-tool parse-options -ambiguous >output 2>output.err && |
ca8d148d | 299 | test_must_be_empty output && |
1108cea7 | 300 | test_cmp typo.err output.err |
38916c5b RS |
301 | ' |
302 | ||
8425b7ea | 303 | cat >expect <<\EOF |
010a2dac SB |
304 | Callback: "four", 0 |
305 | boolean: 5 | |
306 | integer: 4 | |
2a514ed8 | 307 | magnitude: 0 |
c4aca9cc | 308 | timestamp: 0 |
010a2dac SB |
309 | string: (not set) |
310 | abbrev: 7 | |
e0070e8b | 311 | verbose: -1 |
36e6a5ba | 312 | quiet: 0 |
010a2dac | 313 | dry run: no |
df217ed6 | 314 | file: (not set) |
010a2dac SB |
315 | EOF |
316 | ||
317 | test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' ' | |
2f17c78c | 318 | test-tool parse-options --length=four -b -4 >output 2>output.err && |
ca8d148d | 319 | test_must_be_empty output.err && |
010a2dac SB |
320 | test_cmp expect output |
321 | ' | |
322 | ||
1a9bf1e1 | 323 | test_expect_success 'OPT_CALLBACK() and callback errors work' ' |
2f17c78c | 324 | test_must_fail test-tool parse-options --no-length >output 2>output.err && |
1c5e94f4 | 325 | test_must_be_empty output && |
3bb0923f | 326 | test_must_be_empty output.err |
010a2dac SB |
327 | ' |
328 | ||
8425b7ea | 329 | cat >expect <<\EOF |
010a2dac SB |
330 | boolean: 1 |
331 | integer: 23 | |
2a514ed8 | 332 | magnitude: 0 |
c4aca9cc | 333 | timestamp: 0 |
010a2dac SB |
334 | string: (not set) |
335 | abbrev: 7 | |
e0070e8b | 336 | verbose: -1 |
36e6a5ba | 337 | quiet: 0 |
010a2dac | 338 | dry run: no |
df217ed6 | 339 | file: (not set) |
010a2dac SB |
340 | EOF |
341 | ||
342 | test_expect_success 'OPT_BIT() and OPT_SET_INT() work' ' | |
2f17c78c | 343 | test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err && |
ca8d148d | 344 | test_must_be_empty output.err && |
010a2dac SB |
345 | test_cmp expect output |
346 | ' | |
347 | ||
2f4b97f9 | 348 | test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' ' |
2f17c78c | 349 | test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err && |
ca8d148d | 350 | test_must_be_empty output.err && |
2f4b97f9 RS |
351 | test_cmp expect output |
352 | ' | |
353 | ||
2f4b97f9 | 354 | test_expect_success 'OPT_BIT() works' ' |
2f17c78c | 355 | test-tool parse-options --expect="boolean: 6" -bb --or4 |
2f4b97f9 RS |
356 | ' |
357 | ||
358 | test_expect_success 'OPT_NEGBIT() works' ' | |
2f17c78c | 359 | test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4 |
2f4b97f9 | 360 | ' |
010a2dac | 361 | |
62e7a6f7 PB |
362 | test_expect_success 'OPT_CMDMODE() works' ' |
363 | test-tool parse-options --expect="integer: 1" --mode1 | |
364 | ' | |
365 | ||
366 | test_expect_success 'OPT_CMDMODE() detects incompatibility' ' | |
367 | test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err && | |
368 | test_must_be_empty output && | |
369 | test_i18ngrep "incompatible with --mode" output.err | |
370 | ' | |
371 | ||
372 | test_expect_success 'OPT_CMDMODE() detects incompatibility with something else' ' | |
373 | test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err && | |
374 | test_must_be_empty output && | |
375 | test_i18ngrep "incompatible with something else" output.err | |
376 | ' | |
377 | ||
b9e63ddd | 378 | test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' ' |
2f17c78c | 379 | test-tool parse-options --expect="boolean: 6" + + + + + + |
51a9949e RS |
380 | ' |
381 | ||
e0319ff5 | 382 | test_expect_success 'OPT_NUMBER_CALLBACK() works' ' |
2f17c78c | 383 | test-tool parse-options --expect="integer: 12345" -12345 |
e0319ff5 RS |
384 | ' |
385 | ||
8425b7ea | 386 | cat >expect <<\EOF |
6bbfd1fa AS |
387 | boolean: 0 |
388 | integer: 0 | |
2a514ed8 | 389 | magnitude: 0 |
6bbfd1fa AS |
390 | timestamp: 0 |
391 | string: (not set) | |
392 | abbrev: 7 | |
e0070e8b | 393 | verbose: -1 |
36e6a5ba | 394 | quiet: 0 |
6bbfd1fa AS |
395 | dry run: no |
396 | file: (not set) | |
397 | EOF | |
398 | ||
399 | test_expect_success 'negation of OPT_NONEG flags is not ambiguous' ' | |
b02e7d5d | 400 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
2f17c78c | 401 | test-tool parse-options --no-ambig >output 2>output.err && |
ca8d148d | 402 | test_must_be_empty output.err && |
6bbfd1fa AS |
403 | test_cmp expect output |
404 | ' | |
405 | ||
8425b7ea | 406 | cat >>expect <<\EOF |
c8ba1639 JK |
407 | list: foo |
408 | list: bar | |
409 | list: baz | |
410 | EOF | |
411 | test_expect_success '--list keeps list of strings' ' | |
2f17c78c | 412 | test-tool parse-options --list foo --list=bar --list=baz >output && |
c8ba1639 JK |
413 | test_cmp expect output |
414 | ' | |
415 | ||
416 | test_expect_success '--no-list resets list' ' | |
2f17c78c | 417 | test-tool parse-options --list=other --list=irrelevant --list=options \ |
c8ba1639 JK |
418 | --no-list --list=foo --list=bar --list=baz >output && |
419 | test_cmp expect output | |
420 | ' | |
421 | ||
7d177152 | 422 | test_expect_success 'multiple quiet levels' ' |
2f17c78c | 423 | test-tool parse-options --expect="quiet: 3" -q -q -q |
7d177152 PB |
424 | ' |
425 | ||
7d177152 | 426 | test_expect_success 'multiple verbose levels' ' |
2f17c78c | 427 | test-tool parse-options --expect="verbose: 3" -v -v -v |
7d177152 PB |
428 | ' |
429 | ||
7d177152 | 430 | test_expect_success '--no-quiet sets --quiet to 0' ' |
2f17c78c | 431 | test-tool parse-options --expect="quiet: 0" --no-quiet |
7d177152 PB |
432 | ' |
433 | ||
7d177152 | 434 | test_expect_success '--no-quiet resets multiple -q to 0' ' |
2f17c78c | 435 | test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet |
7d177152 PB |
436 | ' |
437 | ||
7d177152 | 438 | test_expect_success '--no-verbose sets verbose to 0' ' |
2f17c78c | 439 | test-tool parse-options --expect="verbose: 0" --no-verbose |
7d177152 PB |
440 | ' |
441 | ||
7d177152 | 442 | test_expect_success '--no-verbose resets multiple verbose to 0' ' |
2f17c78c | 443 | test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose |
7d177152 PB |
444 | ' |
445 | ||
b02e7d5d JS |
446 | test_expect_success 'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' ' |
447 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ | |
448 | test-tool parse-options --ye && | |
449 | test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \ | |
450 | test-tool parse-options --ye | |
451 | ' | |
452 | ||
51b4594b JK |
453 | test_expect_success '--end-of-options treats remainder as args' ' |
454 | test-tool parse-options \ | |
455 | --expect="verbose: -1" \ | |
456 | --expect="arg 00: --verbose" \ | |
457 | --end-of-options --verbose | |
458 | ' | |
459 | ||
c1b117d3 SG |
460 | test_expect_success 'KEEP_DASHDASH works' ' |
461 | test-tool parse-options-flags --keep-dashdash cmd --opt=1 -- --opt=2 --unknown >actual && | |
462 | cat >expect <<-\EOF && | |
463 | opt: 1 | |
464 | arg 00: -- | |
465 | arg 01: --opt=2 | |
466 | arg 02: --unknown | |
467 | EOF | |
468 | test_cmp expect actual | |
469 | ' | |
470 | ||
471 | test_expect_success 'KEEP_ARGV0 works' ' | |
472 | test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual && | |
473 | cat >expect <<-\EOF && | |
474 | opt: 3 | |
475 | arg 00: cmd | |
476 | arg 01: arg0 | |
477 | EOF | |
478 | test_cmp expect actual | |
479 | ' | |
480 | ||
481 | test_expect_success 'STOP_AT_NON_OPTION works' ' | |
482 | test-tool parse-options-flags --stop-at-non-option cmd --opt=4 arg0 --opt=5 --unknown >actual && | |
483 | cat >expect <<-\EOF && | |
484 | opt: 4 | |
485 | arg 00: arg0 | |
486 | arg 01: --opt=5 | |
487 | arg 02: --unknown | |
488 | EOF | |
489 | test_cmp expect actual | |
490 | ' | |
491 | ||
99d86d60 SG |
492 | test_expect_success 'KEEP_UNKNOWN_OPT works' ' |
493 | test-tool parse-options-flags --keep-unknown-opt cmd --unknown=1 --opt=6 -u2 >actual && | |
c1b117d3 SG |
494 | cat >expect <<-\EOF && |
495 | opt: 6 | |
496 | arg 00: --unknown=1 | |
497 | arg 01: -u2 | |
498 | EOF | |
499 | test_cmp expect actual | |
500 | ' | |
501 | ||
502 | test_expect_success 'NO_INTERNAL_HELP works for -h' ' | |
503 | test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd -h 2>err && | |
c1b117d3 SG |
504 | grep "^error: unknown switch \`h$SQ" err && |
505 | grep "^usage: " err | |
506 | ' | |
507 | ||
508 | for help_opt in help help-all | |
509 | do | |
510 | test_expect_success "NO_INTERNAL_HELP works for --$help_opt" " | |
511 | test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd --$help_opt 2>err && | |
c1b117d3 SG |
512 | grep '^error: unknown option \`'$help_opt\' err && |
513 | grep '^usage: ' err | |
514 | " | |
515 | done | |
516 | ||
99d86d60 SG |
517 | test_expect_success 'KEEP_UNKNOWN_OPT | NO_INTERNAL_HELP works' ' |
518 | test-tool parse-options-flags --keep-unknown-opt --no-internal-help cmd -h --help --help-all >actual && | |
c1b117d3 SG |
519 | cat >expect <<-\EOF && |
520 | opt: 0 | |
521 | arg 00: -h | |
522 | arg 01: --help | |
523 | arg 02: --help-all | |
524 | EOF | |
525 | test_cmp expect actual | |
526 | ' | |
527 | ||
fa83cc83 SG |
528 | test_expect_success 'subcommand - no subcommand shows error and usage' ' |
529 | test_expect_code 129 test-tool parse-subcommand cmd 2>err && | |
530 | grep "^error: need a subcommand" err && | |
531 | grep ^usage: err | |
532 | ' | |
533 | ||
534 | test_expect_success 'subcommand - subcommand after -- shows error and usage' ' | |
535 | test_expect_code 129 test-tool parse-subcommand cmd -- subcmd-one 2>err && | |
536 | grep "^error: need a subcommand" err && | |
537 | grep ^usage: err | |
538 | ' | |
539 | ||
540 | test_expect_success 'subcommand - subcommand after --end-of-options shows error and usage' ' | |
541 | test_expect_code 129 test-tool parse-subcommand cmd --end-of-options subcmd-one 2>err && | |
542 | grep "^error: need a subcommand" err && | |
543 | grep ^usage: err | |
544 | ' | |
545 | ||
546 | test_expect_success 'subcommand - unknown subcommand shows error and usage' ' | |
547 | test_expect_code 129 test-tool parse-subcommand cmd nope 2>err && | |
548 | grep "^error: unknown subcommand: \`nope$SQ" err && | |
549 | grep ^usage: err | |
550 | ' | |
551 | ||
552 | test_expect_success 'subcommand - subcommands cannot be abbreviated' ' | |
553 | test_expect_code 129 test-tool parse-subcommand cmd subcmd-o 2>err && | |
554 | grep "^error: unknown subcommand: \`subcmd-o$SQ$" err && | |
555 | grep ^usage: err | |
556 | ' | |
557 | ||
558 | test_expect_success 'subcommand - no negated subcommands' ' | |
559 | test_expect_code 129 test-tool parse-subcommand cmd no-subcmd-one 2>err && | |
560 | grep "^error: unknown subcommand: \`no-subcmd-one$SQ" err && | |
561 | grep ^usage: err | |
562 | ' | |
563 | ||
564 | test_expect_success 'subcommand - simple' ' | |
565 | test-tool parse-subcommand cmd subcmd-two >actual && | |
566 | cat >expect <<-\EOF && | |
567 | opt: 0 | |
568 | fn: subcmd_two | |
569 | arg 00: subcmd-two | |
570 | EOF | |
571 | test_cmp expect actual | |
572 | ' | |
573 | ||
574 | test_expect_success 'subcommand - stop parsing at the first subcommand' ' | |
575 | test-tool parse-subcommand cmd --opt=1 subcmd-two subcmd-one --opt=2 >actual && | |
576 | cat >expect <<-\EOF && | |
577 | opt: 1 | |
578 | fn: subcmd_two | |
579 | arg 00: subcmd-two | |
580 | arg 01: subcmd-one | |
581 | arg 02: --opt=2 | |
582 | EOF | |
583 | test_cmp expect actual | |
584 | ' | |
585 | ||
586 | test_expect_success 'subcommand - KEEP_ARGV0' ' | |
587 | test-tool parse-subcommand --keep-argv0 cmd subcmd-two >actual && | |
588 | cat >expect <<-\EOF && | |
589 | opt: 0 | |
590 | fn: subcmd_two | |
591 | arg 00: cmd | |
592 | arg 01: subcmd-two | |
593 | EOF | |
594 | test_cmp expect actual | |
595 | ' | |
596 | ||
597 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given' ' | |
598 | test-tool parse-subcommand --subcommand-optional cmd >actual && | |
599 | cat >expect <<-\EOF && | |
600 | opt: 0 | |
601 | fn: subcmd_one | |
602 | EOF | |
603 | test_cmp expect actual | |
604 | ' | |
605 | ||
606 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + given subcommand' ' | |
607 | test-tool parse-subcommand --subcommand-optional cmd subcmd-two branch file >actual && | |
608 | cat >expect <<-\EOF && | |
609 | opt: 0 | |
610 | fn: subcmd_two | |
611 | arg 00: subcmd-two | |
612 | arg 01: branch | |
613 | arg 02: file | |
614 | EOF | |
615 | test_cmp expect actual | |
616 | ' | |
617 | ||
618 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown dashless args' ' | |
619 | test-tool parse-subcommand --subcommand-optional cmd branch file >actual && | |
620 | cat >expect <<-\EOF && | |
621 | opt: 0 | |
622 | fn: subcmd_one | |
623 | arg 00: branch | |
624 | arg 01: file | |
625 | EOF | |
626 | test_cmp expect actual | |
627 | ' | |
628 | ||
629 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown option' ' | |
630 | test_expect_code 129 test-tool parse-subcommand --subcommand-optional cmd --subcommand-opt 2>err && | |
631 | grep "^error: unknown option" err && | |
632 | grep ^usage: err | |
633 | ' | |
634 | ||
635 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand not given + unknown option' ' | |
636 | test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt >actual && | |
637 | cat >expect <<-\EOF && | |
638 | opt: 0 | |
639 | fn: subcmd_one | |
640 | arg 00: --subcommand-opt | |
641 | EOF | |
642 | test_cmp expect actual | |
643 | ' | |
644 | ||
645 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand ignored after unknown option' ' | |
646 | test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt subcmd-two >actual && | |
647 | cat >expect <<-\EOF && | |
648 | opt: 0 | |
649 | fn: subcmd_one | |
650 | arg 00: --subcommand-opt | |
651 | arg 01: subcmd-two | |
652 | EOF | |
653 | test_cmp expect actual | |
654 | ' | |
655 | ||
656 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + command and subcommand options cannot be mixed' ' | |
657 | test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt branch --opt=1 >actual && | |
658 | cat >expect <<-\EOF && | |
659 | opt: 0 | |
660 | fn: subcmd_one | |
661 | arg 00: --subcommand-opt | |
662 | arg 01: branch | |
663 | arg 02: --opt=1 | |
664 | EOF | |
665 | test_cmp expect actual | |
666 | ' | |
667 | ||
668 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_ARGV0' ' | |
669 | test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-argv0 cmd --subcommand-opt branch >actual && | |
670 | cat >expect <<-\EOF && | |
671 | opt: 0 | |
672 | fn: subcmd_one | |
673 | arg 00: cmd | |
674 | arg 01: --subcommand-opt | |
675 | arg 02: branch | |
676 | EOF | |
677 | test_cmp expect actual | |
678 | ' | |
679 | ||
680 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_DASHDASH' ' | |
681 | test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-dashdash cmd -- --subcommand-opt file >actual && | |
682 | cat >expect <<-\EOF && | |
683 | opt: 0 | |
684 | fn: subcmd_one | |
685 | arg 00: -- | |
686 | arg 01: --subcommand-opt | |
687 | arg 02: file | |
688 | EOF | |
689 | test_cmp expect actual | |
690 | ' | |
691 | ||
692 | test_expect_success 'subcommand - completion helper' ' | |
693 | test-tool parse-subcommand cmd --git-completion-helper >actual && | |
694 | echo "subcmd-one subcmd-two --opt= --no-opt" >expect && | |
695 | test_cmp expect actual | |
696 | ' | |
697 | ||
698 | test_expect_success 'subcommands are incompatible with STOP_AT_NON_OPTION' ' | |
699 | test_must_fail test-tool parse-subcommand --stop-at-non-option cmd subcmd-one 2>err && | |
700 | grep ^BUG err | |
701 | ' | |
702 | ||
703 | test_expect_success 'subcommands are incompatible with KEEP_UNKNOWN_OPT unless in combination with SUBCOMMAND_OPTIONAL' ' | |
704 | test_must_fail test-tool parse-subcommand --keep-unknown-opt cmd subcmd-two 2>err && | |
705 | grep ^BUG err | |
706 | ' | |
707 | ||
708 | test_expect_success 'subcommands are incompatible with KEEP_DASHDASH unless in combination with SUBCOMMAND_OPTIONAL' ' | |
709 | test_must_fail test-tool parse-subcommand --keep-dashdash cmd subcmd-two 2>err && | |
710 | grep ^BUG err | |
711 | ' | |
712 | ||
84356ff7 PW |
713 | test_expect_success 'negative magnitude' ' |
714 | test_must_fail test-tool parse-options --magnitude -1 >out 2>err && | |
715 | grep "non-negative integer" err && | |
716 | test_must_be_empty out | |
717 | ' | |
7595c0ec PW |
718 | |
719 | test_expect_success 'magnitude with units but no numbers' ' | |
720 | test_must_fail test-tool parse-options --magnitude m >out 2>err && | |
721 | grep "non-negative integer" err && | |
722 | test_must_be_empty out | |
723 | ' | |
724 | ||
beb47437 | 725 | test_done |