INSTALL: Reorder dependencies, split shell and Perl
[alt-git.git] / t / t0040-parse-options.sh
blobbbc821ef97de76ef532ec1d1d704da15e8973500
1 #!/bin/sh
3 # Copyright (c) 2007 Johannes Schindelin
6 test_description='our own option parser'
8 . ./test-lib.sh
10 cat > expect.err << EOF
11 usage: test-parse-options <options>
13 -b, --boolean get a boolean
14 -4, --or4 bitwise-or boolean with ...0100
15 --neg-or4 same as --no-or4
17 -i, --integer <n> get a integer
18 -j <n> get a integer, too
19 --set23 set integer to 23
20 -t <time> get timestamp of <time>
21 -L, --length <str> get length of <str>
22 -F, --file <FILE> set file to <FILE>
24 String options
25 -s, --string <string>
26 get a string
27 --string2 <str> get another string
28 --st <st> get another string (pervert ordering)
29 -o <str> get another string
30 --default-string set string to default
32 Magic arguments
33 --quux means --quux
34 -NUM set integer to NUM
35 + same as -b
37 Standard options
38 --abbrev[=<n>] use <n> digits to display SHA-1s
39 -v, --verbose be verbose
40 -n, --dry-run dry run
41 -q, --quiet be quiet
43 EOF
45 test_expect_success 'test help' '
46 test_must_fail test-parse-options -h > output 2> output.err &&
47 test ! -s output &&
48 test_cmp expect.err output.err
51 cat > expect << EOF
52 boolean: 2
53 integer: 1729
54 timestamp: 0
55 string: 123
56 abbrev: 7
57 verbose: 2
58 quiet: no
59 dry run: yes
60 file: prefix/my.file
61 EOF
63 test_expect_success 'short options' '
64 test-parse-options -s123 -b -i 1729 -b -vv -n -F my.file \
65 > output 2> output.err &&
66 test_cmp expect output &&
67 test ! -s output.err
70 cat > expect << EOF
71 boolean: 2
72 integer: 1729
73 timestamp: 0
74 string: 321
75 abbrev: 10
76 verbose: 2
77 quiet: no
78 dry run: no
79 file: prefix/fi.le
80 EOF
82 test_expect_success 'long options' '
83 test-parse-options --boolean --integer 1729 --boolean --string2=321 \
84 --verbose --verbose --no-dry-run --abbrev=10 --file fi.le\
85 > output 2> output.err &&
86 test ! -s output.err &&
87 test_cmp expect output
90 test_expect_success 'missing required value' '
91 test-parse-options -s;
92 test $? = 129 &&
93 test-parse-options --string;
94 test $? = 129 &&
95 test-parse-options --file;
96 test $? = 129
99 cat > expect << EOF
100 boolean: 1
101 integer: 13
102 timestamp: 0
103 string: 123
104 abbrev: 7
105 verbose: 0
106 quiet: no
107 dry run: no
108 file: (not set)
109 arg 00: a1
110 arg 01: b1
111 arg 02: --boolean
114 test_expect_success 'intermingled arguments' '
115 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
116 > output 2> output.err &&
117 test ! -s output.err &&
118 test_cmp expect output
121 cat > expect << EOF
122 boolean: 0
123 integer: 2
124 timestamp: 0
125 string: (not set)
126 abbrev: 7
127 verbose: 0
128 quiet: no
129 dry run: no
130 file: (not set)
133 test_expect_success 'unambiguously abbreviated option' '
134 test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
135 test ! -s output.err &&
136 test_cmp expect output
139 test_expect_success 'unambiguously abbreviated option with "="' '
140 test-parse-options --int=2 > output 2> output.err &&
141 test ! -s output.err &&
142 test_cmp expect output
145 test_expect_success 'ambiguously abbreviated option' '
146 test-parse-options --strin 123;
147 test $? = 129
150 cat > expect << EOF
151 boolean: 0
152 integer: 0
153 timestamp: 0
154 string: 123
155 abbrev: 7
156 verbose: 0
157 quiet: no
158 dry run: no
159 file: (not set)
162 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
163 test-parse-options --st 123 > output 2> output.err &&
164 test ! -s output.err &&
165 test_cmp expect output
168 cat > typo.err << EOF
169 error: did you mean \`--boolean\` (with two dashes ?)
172 test_expect_success 'detect possible typos' '
173 test_must_fail test-parse-options -boolean > output 2> output.err &&
174 test ! -s output &&
175 test_cmp typo.err output.err
178 cat > expect <<EOF
179 boolean: 0
180 integer: 0
181 timestamp: 0
182 string: (not set)
183 abbrev: 7
184 verbose: 0
185 quiet: no
186 dry run: no
187 file: (not set)
188 arg 00: --quux
191 test_expect_success 'keep some options as arguments' '
192 test-parse-options --quux > output 2> output.err &&
193 test ! -s output.err &&
194 test_cmp expect output
197 cat > expect <<EOF
198 boolean: 0
199 integer: 0
200 timestamp: 1
201 string: default
202 abbrev: 7
203 verbose: 0
204 quiet: yes
205 dry run: no
206 file: (not set)
207 arg 00: foo
210 test_expect_success 'OPT_DATE() and OPT_SET_PTR() work' '
211 test-parse-options -t "1970-01-01 00:00:01 +0000" --default-string \
212 foo -q > output 2> output.err &&
213 test ! -s output.err &&
214 test_cmp expect output
217 cat > expect <<EOF
218 Callback: "four", 0
219 boolean: 5
220 integer: 4
221 timestamp: 0
222 string: (not set)
223 abbrev: 7
224 verbose: 0
225 quiet: no
226 dry run: no
227 file: (not set)
230 test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
231 test-parse-options --length=four -b -4 > output 2> output.err &&
232 test ! -s output.err &&
233 test_cmp expect output
236 cat > expect <<EOF
237 Callback: "not set", 1
240 test_expect_success 'OPT_CALLBACK() and callback errors work' '
241 test_must_fail test-parse-options --no-length > output 2> output.err &&
242 test_cmp expect output &&
243 test_cmp expect.err output.err
246 cat > expect <<EOF
247 boolean: 1
248 integer: 23
249 timestamp: 0
250 string: (not set)
251 abbrev: 7
252 verbose: 0
253 quiet: no
254 dry run: no
255 file: (not set)
258 test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
259 test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err &&
260 test ! -s output.err &&
261 test_cmp expect output
264 test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
265 test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err &&
266 test ! -s output.err &&
267 test_cmp expect output
270 cat > expect <<EOF
271 boolean: 6
272 integer: 0
273 timestamp: 0
274 string: (not set)
275 abbrev: 7
276 verbose: 0
277 quiet: no
278 dry run: no
279 file: (not set)
282 test_expect_success 'OPT_BIT() works' '
283 test-parse-options -bb --or4 > output 2> output.err &&
284 test ! -s output.err &&
285 test_cmp expect output
288 test_expect_success 'OPT_NEGBIT() works' '
289 test-parse-options -bb --no-neg-or4 > output 2> output.err &&
290 test ! -s output.err &&
291 test_cmp expect output
294 test_expect_success 'OPT_BOOLEAN() with PARSE_OPT_NODASH works' '
295 test-parse-options + + + + + + > output 2> output.err &&
296 test ! -s output.err &&
297 test_cmp expect output
300 cat > expect <<EOF
301 boolean: 0
302 integer: 12345
303 timestamp: 0
304 string: (not set)
305 abbrev: 7
306 verbose: 0
307 quiet: no
308 dry run: no
309 file: (not set)
312 test_expect_success 'OPT_NUMBER_CALLBACK() works' '
313 test-parse-options -12345 > output 2> output.err &&
314 test ! -s output.err &&
315 test_cmp expect output
318 test_done