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