]>
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 | ||
13 | -b, --boolean get a boolean | |
010a2dac | 14 | -4, --or4 bitwise-or boolean with ...0100 |
2f4b97f9 | 15 | --neg-or4 same as --no-or4 |
010a2dac | 16 | |
beb47437 JS |
17 | -i, --integer <n> get a integer |
18 | -j <n> get a integer, too | |
010a2dac SB |
19 | --set23 set integer to 23 |
20 | -t <time> get timestamp of <time> | |
21 | -L, --length <str> get length of <str> | |
41dbcd45 | 22 | -F, --file <file> set file to <file> |
beb47437 | 23 | |
010a2dac | 24 | String options |
beb47437 JS |
25 | -s, --string <string> |
26 | get a string | |
27 | --string2 <str> get another string | |
243e0614 | 28 | --st <st> get another string (pervert ordering) |
3a9f0f41 | 29 | -o <str> get another string |
010a2dac | 30 | --default-string set string to default |
beb47437 | 31 | |
010a2dac | 32 | Magic arguments |
580d5bff | 33 | --quux means --quux |
e0319ff5 | 34 | -NUM set integer to NUM |
51a9949e | 35 | + same as -b |
6bbfd1fa AS |
36 | --ambiguous positive ambiguity |
37 | --no-ambiguous negative ambiguity | |
580d5bff | 38 | |
010a2dac SB |
39 | Standard options |
40 | --abbrev[=<n>] use <n> digits to display SHA-1s | |
41 | -v, --verbose be verbose | |
42 | -n, --dry-run dry run | |
43 | -q, --quiet be quiet | |
44 | ||
beb47437 JS |
45 | EOF |
46 | ||
47 | test_expect_success 'test help' ' | |
010a2dac | 48 | test_must_fail test-parse-options -h > output 2> output.err && |
9c7304e3 GS |
49 | test ! -s output.err && |
50 | test_cmp expect output | |
beb47437 JS |
51 | ' |
52 | ||
9c7304e3 GS |
53 | mv expect expect.err |
54 | ||
beb47437 JS |
55 | cat > expect << EOF |
56 | boolean: 2 | |
57 | integer: 1729 | |
c4aca9cc | 58 | timestamp: 0 |
beb47437 | 59 | string: 123 |
010a2dac SB |
60 | abbrev: 7 |
61 | verbose: 2 | |
62 | quiet: no | |
63 | dry run: yes | |
df217ed6 | 64 | file: prefix/my.file |
beb47437 JS |
65 | EOF |
66 | ||
67 | test_expect_success 'short options' ' | |
df217ed6 SB |
68 | test-parse-options -s123 -b -i 1729 -b -vv -n -F my.file \ |
69 | > output 2> output.err && | |
3af82863 | 70 | test_cmp expect output && |
beb47437 JS |
71 | test ! -s output.err |
72 | ' | |
010a2dac | 73 | |
beb47437 JS |
74 | cat > expect << EOF |
75 | boolean: 2 | |
76 | integer: 1729 | |
c4aca9cc | 77 | timestamp: 0 |
beb47437 | 78 | string: 321 |
010a2dac SB |
79 | abbrev: 10 |
80 | verbose: 2 | |
81 | quiet: no | |
82 | dry run: no | |
df217ed6 | 83 | file: prefix/fi.le |
beb47437 JS |
84 | EOF |
85 | ||
86 | test_expect_success 'long options' ' | |
87 | test-parse-options --boolean --integer 1729 --boolean --string2=321 \ | |
df217ed6 | 88 | --verbose --verbose --no-dry-run --abbrev=10 --file fi.le\ |
beb47437 JS |
89 | > output 2> output.err && |
90 | test ! -s output.err && | |
3af82863 | 91 | test_cmp expect output |
beb47437 JS |
92 | ' |
93 | ||
d5d745f9 OM |
94 | test_expect_success 'missing required value' ' |
95 | test-parse-options -s; | |
96 | test $? = 129 && | |
97 | test-parse-options --string; | |
df217ed6 SB |
98 | test $? = 129 && |
99 | test-parse-options --file; | |
d5d745f9 OM |
100 | test $? = 129 |
101 | ' | |
102 | ||
beb47437 JS |
103 | cat > expect << EOF |
104 | boolean: 1 | |
105 | integer: 13 | |
c4aca9cc | 106 | timestamp: 0 |
beb47437 | 107 | string: 123 |
010a2dac SB |
108 | abbrev: 7 |
109 | verbose: 0 | |
110 | quiet: no | |
111 | dry run: no | |
df217ed6 | 112 | file: (not set) |
beb47437 JS |
113 | arg 00: a1 |
114 | arg 01: b1 | |
115 | arg 02: --boolean | |
116 | EOF | |
117 | ||
118 | test_expect_success 'intermingled arguments' ' | |
119 | test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \ | |
120 | > output 2> output.err && | |
121 | test ! -s output.err && | |
3af82863 | 122 | test_cmp expect output |
beb47437 JS |
123 | ' |
124 | ||
7f275b91 JS |
125 | cat > expect << EOF |
126 | boolean: 0 | |
127 | integer: 2 | |
c4aca9cc | 128 | timestamp: 0 |
7f275b91 | 129 | string: (not set) |
010a2dac SB |
130 | abbrev: 7 |
131 | verbose: 0 | |
132 | quiet: no | |
133 | dry run: no | |
df217ed6 | 134 | file: (not set) |
7f275b91 JS |
135 | EOF |
136 | ||
137 | test_expect_success 'unambiguously abbreviated option' ' | |
138 | test-parse-options --int 2 --boolean --no-bo > output 2> output.err && | |
139 | test ! -s output.err && | |
3af82863 | 140 | test_cmp expect output |
7f275b91 JS |
141 | ' |
142 | ||
143 | test_expect_success 'unambiguously abbreviated option with "="' ' | |
144 | test-parse-options --int=2 > output 2> output.err && | |
145 | test ! -s output.err && | |
3af82863 | 146 | test_cmp expect output |
7f275b91 JS |
147 | ' |
148 | ||
41ac414e | 149 | test_expect_success 'ambiguously abbreviated option' ' |
7f275b91 | 150 | test-parse-options --strin 123; |
41ac414e | 151 | test $? = 129 |
7f275b91 JS |
152 | ' |
153 | ||
243e0614 JS |
154 | cat > expect << EOF |
155 | boolean: 0 | |
156 | integer: 0 | |
c4aca9cc | 157 | timestamp: 0 |
243e0614 | 158 | string: 123 |
010a2dac SB |
159 | abbrev: 7 |
160 | verbose: 0 | |
161 | quiet: no | |
162 | dry run: no | |
df217ed6 | 163 | file: (not set) |
243e0614 JS |
164 | EOF |
165 | ||
166 | test_expect_success 'non ambiguous option (after two options it abbreviates)' ' | |
167 | test-parse-options --st 123 > output 2> output.err && | |
168 | test ! -s output.err && | |
3af82863 | 169 | test_cmp expect output |
243e0614 JS |
170 | ' |
171 | ||
010a2dac | 172 | cat > typo.err << EOF |
3a9f0f41 PH |
173 | error: did you mean \`--boolean\` (with two dashes ?) |
174 | EOF | |
175 | ||
176 | test_expect_success 'detect possible typos' ' | |
010a2dac | 177 | test_must_fail test-parse-options -boolean > output 2> output.err && |
3a9f0f41 | 178 | test ! -s output && |
010a2dac | 179 | test_cmp typo.err output.err |
3a9f0f41 PH |
180 | ' |
181 | ||
580d5bff PH |
182 | cat > expect <<EOF |
183 | boolean: 0 | |
184 | integer: 0 | |
c4aca9cc | 185 | timestamp: 0 |
580d5bff | 186 | string: (not set) |
010a2dac SB |
187 | abbrev: 7 |
188 | verbose: 0 | |
189 | quiet: no | |
190 | dry run: no | |
df217ed6 | 191 | file: (not set) |
580d5bff PH |
192 | arg 00: --quux |
193 | EOF | |
194 | ||
195 | test_expect_success 'keep some options as arguments' ' | |
196 | test-parse-options --quux > output 2> output.err && | |
197 | test ! -s output.err && | |
3af82863 | 198 | test_cmp expect output |
580d5bff PH |
199 | ' |
200 | ||
010a2dac SB |
201 | cat > expect <<EOF |
202 | boolean: 0 | |
c4aca9cc JH |
203 | integer: 0 |
204 | timestamp: 1 | |
010a2dac SB |
205 | string: default |
206 | abbrev: 7 | |
207 | verbose: 0 | |
208 | quiet: yes | |
209 | dry run: no | |
df217ed6 | 210 | file: (not set) |
010a2dac SB |
211 | arg 00: foo |
212 | EOF | |
213 | ||
214 | test_expect_success 'OPT_DATE() and OPT_SET_PTR() work' ' | |
215 | test-parse-options -t "1970-01-01 00:00:01 +0000" --default-string \ | |
216 | foo -q > output 2> output.err && | |
217 | test ! -s output.err && | |
218 | test_cmp expect output | |
219 | ' | |
220 | ||
221 | cat > expect <<EOF | |
222 | Callback: "four", 0 | |
223 | boolean: 5 | |
224 | integer: 4 | |
c4aca9cc | 225 | timestamp: 0 |
010a2dac SB |
226 | string: (not set) |
227 | abbrev: 7 | |
228 | verbose: 0 | |
229 | quiet: no | |
230 | dry run: no | |
df217ed6 | 231 | file: (not set) |
010a2dac SB |
232 | EOF |
233 | ||
234 | test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' ' | |
235 | test-parse-options --length=four -b -4 > output 2> output.err && | |
236 | test ! -s output.err && | |
237 | test_cmp expect output | |
238 | ' | |
239 | ||
240 | cat > expect <<EOF | |
241 | Callback: "not set", 1 | |
242 | EOF | |
243 | ||
244 | test_expect_success 'OPT_CALLBACK() and callback errors work' ' | |
245 | test_must_fail test-parse-options --no-length > output 2> output.err && | |
246 | test_cmp expect output && | |
247 | test_cmp expect.err output.err | |
248 | ' | |
249 | ||
250 | cat > expect <<EOF | |
251 | boolean: 1 | |
252 | integer: 23 | |
c4aca9cc | 253 | timestamp: 0 |
010a2dac SB |
254 | string: (not set) |
255 | abbrev: 7 | |
256 | verbose: 0 | |
257 | quiet: no | |
258 | dry run: no | |
df217ed6 | 259 | file: (not set) |
010a2dac SB |
260 | EOF |
261 | ||
262 | test_expect_success 'OPT_BIT() and OPT_SET_INT() work' ' | |
263 | test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err && | |
264 | test ! -s output.err && | |
265 | test_cmp expect output | |
266 | ' | |
267 | ||
2f4b97f9 RS |
268 | test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' ' |
269 | test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err && | |
270 | test ! -s output.err && | |
271 | test_cmp expect output | |
272 | ' | |
273 | ||
274 | cat > expect <<EOF | |
275 | boolean: 6 | |
276 | integer: 0 | |
277 | timestamp: 0 | |
278 | string: (not set) | |
279 | abbrev: 7 | |
280 | verbose: 0 | |
281 | quiet: no | |
282 | dry run: no | |
df217ed6 | 283 | file: (not set) |
2f4b97f9 RS |
284 | EOF |
285 | ||
286 | test_expect_success 'OPT_BIT() works' ' | |
287 | test-parse-options -bb --or4 > output 2> output.err && | |
288 | test ! -s output.err && | |
289 | test_cmp expect output | |
290 | ' | |
291 | ||
292 | test_expect_success 'OPT_NEGBIT() works' ' | |
293 | test-parse-options -bb --no-neg-or4 > output 2> output.err && | |
294 | test ! -s output.err && | |
295 | test_cmp expect output | |
296 | ' | |
010a2dac | 297 | |
51a9949e RS |
298 | test_expect_success 'OPT_BOOLEAN() with PARSE_OPT_NODASH works' ' |
299 | test-parse-options + + + + + + > output 2> output.err && | |
300 | test ! -s output.err && | |
301 | test_cmp expect output | |
302 | ' | |
303 | ||
e0319ff5 RS |
304 | cat > expect <<EOF |
305 | boolean: 0 | |
306 | integer: 12345 | |
307 | timestamp: 0 | |
308 | string: (not set) | |
309 | abbrev: 7 | |
310 | verbose: 0 | |
311 | quiet: no | |
312 | dry run: no | |
df217ed6 | 313 | file: (not set) |
e0319ff5 RS |
314 | EOF |
315 | ||
316 | test_expect_success 'OPT_NUMBER_CALLBACK() works' ' | |
317 | test-parse-options -12345 > output 2> output.err && | |
318 | test ! -s output.err && | |
319 | test_cmp expect output | |
320 | ' | |
321 | ||
6bbfd1fa AS |
322 | cat >expect <<EOF |
323 | boolean: 0 | |
324 | integer: 0 | |
325 | timestamp: 0 | |
326 | string: (not set) | |
327 | abbrev: 7 | |
328 | verbose: 0 | |
329 | quiet: no | |
330 | dry run: no | |
331 | file: (not set) | |
332 | EOF | |
333 | ||
334 | test_expect_success 'negation of OPT_NONEG flags is not ambiguous' ' | |
335 | test-parse-options --no-ambig >output 2>output.err && | |
336 | test ! -s output.err && | |
337 | test_cmp expect output | |
338 | ' | |
339 | ||
beb47437 | 340 | test_done |