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