1#!/bin/sh
2#
3# Copyright (c) 2007 Johannes Schindelin
4#
5
6test_description='our own option parser'
7
8. ./test-lib.sh
9
10cat > expect.err << EOF
11usage: test-parse-options <options>
12
13 -b, --boolean get a boolean
14 -4, --or4 bitwise-or boolean with ...0100
15 --neg-or4 same as --no-or4
16
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>
23
24String 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
31
32Magic arguments
33 --quux means --quux
34 -NUM set integer to NUM
35 + same as -b
36
37Standard 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
42
43EOF
44
45test_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
49'
50
51cat > expect << EOF
52boolean: 2
53integer: 1729
54timestamp: 0
55string: 123
56abbrev: 7
57verbose: 2
58quiet: no
59dry run: yes
60file: prefix/my.file
61EOF
62
63test_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
68'
69
70cat > expect << EOF
71boolean: 2
72integer: 1729
73timestamp: 0
74string: 321
75abbrev: 10
76verbose: 2
77quiet: no
78dry run: no
79file: prefix/fi.le
80EOF
81
82test_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
88'
89
90test_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
97'
98
99cat > expect << EOF
100boolean: 1
101integer: 13
102timestamp: 0
103string: 123
104abbrev: 7
105verbose: 0
106quiet: no
107dry run: no
108file: (not set)
109arg 00: a1
110arg 01: b1
111arg 02: --boolean
112EOF
113
114test_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
119'
120
121cat > expect << EOF
122boolean: 0
123integer: 2
124timestamp: 0
125string: (not set)
126abbrev: 7
127verbose: 0
128quiet: no
129dry run: no
130file: (not set)
131EOF
132
133test_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
137'
138
139test_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
143'
144
145test_expect_success 'ambiguously abbreviated option' '
146 test-parse-options --strin 123;
147 test $? = 129
148'
149
150cat > expect << EOF
151boolean: 0
152integer: 0
153timestamp: 0
154string: 123
155abbrev: 7
156verbose: 0
157quiet: no
158dry run: no
159file: (not set)
160EOF
161
162test_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
166'
167
168cat > typo.err << EOF
169error: did you mean \`--boolean\` (with two dashes ?)
170EOF
171
172test_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
176'
177
178cat > expect <<EOF
179boolean: 0
180integer: 0
181timestamp: 0
182string: (not set)
183abbrev: 7
184verbose: 0
185quiet: no
186dry run: no
187file: (not set)
188arg 00: --quux
189EOF
190
191test_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
195'
196
197cat > expect <<EOF
198boolean: 0
199integer: 0
200timestamp: 1
201string: default
202abbrev: 7
203verbose: 0
204quiet: yes
205dry run: no
206file: (not set)
207arg 00: foo
208EOF
209
210test_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
215'
216
217cat > expect <<EOF
218Callback: "four", 0
219boolean: 5
220integer: 4
221timestamp: 0
222string: (not set)
223abbrev: 7
224verbose: 0
225quiet: no
226dry run: no
227file: (not set)
228EOF
229
230test_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
234'
235
236cat > expect <<EOF
237Callback: "not set", 1
238EOF
239
240test_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
244'
245
246cat > expect <<EOF
247boolean: 1
248integer: 23
249timestamp: 0
250string: (not set)
251abbrev: 7
252verbose: 0
253quiet: no
254dry run: no
255file: (not set)
256EOF
257
258test_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
262'
263
264test_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
268'
269
270cat > expect <<EOF
271boolean: 6
272integer: 0
273timestamp: 0
274string: (not set)
275abbrev: 7
276verbose: 0
277quiet: no
278dry run: no
279file: (not set)
280EOF
281
282test_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
286'
287
288test_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
292'
293
294test_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
298'
299
300cat > expect <<EOF
301boolean: 0
302integer: 12345
303timestamp: 0
304string: (not set)
305abbrev: 7
306verbose: 0
307quiet: no
308dry run: no
309file: (not set)
310EOF
311
312test_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
316'
317
318test_done