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