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