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