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