blob: a1e4616febe6c67f67ab2b4380610abc92468ba5 [file] [log] [blame]
Johannes Schindelinbeb47432007-10-13 17:34:45 +01001#!/bin/sh
2#
3# Copyright (c) 2007 Johannes Schindelin
4#
5
6test_description='our own option parser'
7
8. ./test-lib.sh
9
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +020010cat > expect << EOF
Johannes Schindelinbeb47432007-10-13 17:34:45 +010011usage: test-parse-options <options>
12
13 -b, --boolean get a boolean
Stephan Beyer010a2da2008-06-22 17:04:26 +020014 -4, --or4 bitwise-or boolean with ...0100
René Scharfe2f4b97f2009-05-07 21:44:17 +020015 --neg-or4 same as --no-or4
Stephan Beyer010a2da2008-06-22 17:04:26 +020016
Johannes Schindelinbeb47432007-10-13 17:34:45 +010017 -i, --integer <n> get a integer
18 -j <n> get a integer, too
Stephan Beyer010a2da2008-06-22 17:04:26 +020019 --set23 set integer to 23
20 -t <time> get timestamp of <time>
21 -L, --length <str> get length of <str>
Michael J Gruber41dbcd42011-02-15 14:09:13 +010022 -F, --file <file> set file to <file>
Johannes Schindelinbeb47432007-10-13 17:34:45 +010023
Stephan Beyer010a2da2008-06-22 17:04:26 +020024String options
Johannes Schindelinbeb47432007-10-13 17:34:45 +010025 -s, --string <string>
26 get a string
27 --string2 <str> get another string
Johannes Schindelin243e0612007-11-05 13:15:21 +000028 --st <st> get another string (pervert ordering)
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +010029 -o <str> get another string
Stephan Beyer010a2da2008-06-22 17:04:26 +020030 --default-string set string to default
Jeff Kingc8ba1632011-06-09 11:55:23 -040031 --list <str> add str to list
Johannes Schindelinbeb47432007-10-13 17:34:45 +010032
Stephan Beyer010a2da2008-06-22 17:04:26 +020033Magic arguments
Pierre Habouzit580d5bf2008-03-02 11:35:56 +010034 --quux means --quux
René Scharfee0319ff2009-05-07 21:45:08 +020035 -NUM set integer to NUM
René Scharfe51a99492009-05-07 21:45:42 +020036 + same as -b
Andreas Schwab6bbfd1f2009-09-25 20:44:44 +020037 --ambiguous positive ambiguity
38 --no-ambiguous negative ambiguity
Pierre Habouzit580d5bf2008-03-02 11:35:56 +010039
Stephan Beyer010a2da2008-06-22 17:04:26 +020040Standard options
41 --abbrev[=<n>] use <n> digits to display SHA-1s
42 -v, --verbose be verbose
43 -n, --dry-run dry run
44 -q, --quiet be quiet
45
Johannes Schindelinbeb47432007-10-13 17:34:45 +010046EOF
47
48test_expect_success 'test help' '
Stephan Beyer010a2da2008-06-22 17:04:26 +020049 test_must_fail test-parse-options -h > output 2> output.err &&
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +020050 test ! -s output.err &&
51 test_cmp expect output
Johannes Schindelinbeb47432007-10-13 17:34:45 +010052'
53
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +020054mv expect expect.err
55
Johannes Schindelinbeb47432007-10-13 17:34:45 +010056cat > expect << EOF
57boolean: 2
58integer: 1729
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -070059timestamp: 0
Johannes Schindelinbeb47432007-10-13 17:34:45 +010060string: 123
Stephan Beyer010a2da2008-06-22 17:04:26 +020061abbrev: 7
62verbose: 2
63quiet: no
64dry run: yes
Stephen Boyddf217ed2009-05-23 11:53:13 -070065file: prefix/my.file
Johannes Schindelinbeb47432007-10-13 17:34:45 +010066EOF
67
68test_expect_success 'short options' '
Stephen Boyddf217ed2009-05-23 11:53:13 -070069 test-parse-options -s123 -b -i 1729 -b -vv -n -F my.file \
70 > output 2> output.err &&
Junio C Hamano3af82862008-05-23 22:28:56 -070071 test_cmp expect output &&
Johannes Schindelinbeb47432007-10-13 17:34:45 +010072 test ! -s output.err
73'
Stephan Beyer010a2da2008-06-22 17:04:26 +020074
Johannes Schindelinbeb47432007-10-13 17:34:45 +010075cat > expect << EOF
76boolean: 2
77integer: 1729
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -070078timestamp: 0
Johannes Schindelinbeb47432007-10-13 17:34:45 +010079string: 321
Stephan Beyer010a2da2008-06-22 17:04:26 +020080abbrev: 10
81verbose: 2
82quiet: no
83dry run: no
Stephen Boyddf217ed2009-05-23 11:53:13 -070084file: prefix/fi.le
Johannes Schindelinbeb47432007-10-13 17:34:45 +010085EOF
86
87test_expect_success 'long options' '
88 test-parse-options --boolean --integer 1729 --boolean --string2=321 \
Stephen Boyddf217ed2009-05-23 11:53:13 -070089 --verbose --verbose --no-dry-run --abbrev=10 --file fi.le\
René Scharfe6acec032011-09-28 19:44:30 +020090 --obsolete > output 2> output.err &&
Johannes Schindelinbeb47432007-10-13 17:34:45 +010091 test ! -s output.err &&
Junio C Hamano3af82862008-05-23 22:28:56 -070092 test_cmp expect output
Johannes Schindelinbeb47432007-10-13 17:34:45 +010093'
94
Olivier Marind5d745f2008-07-21 20:30:36 +020095test_expect_success 'missing required value' '
96 test-parse-options -s;
97 test $? = 129 &&
98 test-parse-options --string;
Stephen Boyddf217ed2009-05-23 11:53:13 -070099 test $? = 129 &&
100 test-parse-options --file;
Olivier Marind5d745f2008-07-21 20:30:36 +0200101 test $? = 129
102'
103
Johannes Schindelinbeb47432007-10-13 17:34:45 +0100104cat > expect << EOF
105boolean: 1
106integer: 13
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -0700107timestamp: 0
Johannes Schindelinbeb47432007-10-13 17:34:45 +0100108string: 123
Stephan Beyer010a2da2008-06-22 17:04:26 +0200109abbrev: 7
110verbose: 0
111quiet: no
112dry run: no
Stephen Boyddf217ed2009-05-23 11:53:13 -0700113file: (not set)
Johannes Schindelinbeb47432007-10-13 17:34:45 +0100114arg 00: a1
115arg 01: b1
116arg 02: --boolean
117EOF
118
119test_expect_success 'intermingled arguments' '
120 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
121 > output 2> output.err &&
122 test ! -s output.err &&
Junio C Hamano3af82862008-05-23 22:28:56 -0700123 test_cmp expect output
Johannes Schindelinbeb47432007-10-13 17:34:45 +0100124'
125
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100126cat > expect << EOF
127boolean: 0
128integer: 2
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -0700129timestamp: 0
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100130string: (not set)
Stephan Beyer010a2da2008-06-22 17:04:26 +0200131abbrev: 7
132verbose: 0
133quiet: no
134dry run: no
Stephen Boyddf217ed2009-05-23 11:53:13 -0700135file: (not set)
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100136EOF
137
138test_expect_success 'unambiguously abbreviated option' '
139 test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
140 test ! -s output.err &&
Junio C Hamano3af82862008-05-23 22:28:56 -0700141 test_cmp expect output
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100142'
143
144test_expect_success 'unambiguously abbreviated option with "="' '
145 test-parse-options --int=2 > output 2> output.err &&
146 test ! -s output.err &&
Junio C Hamano3af82862008-05-23 22:28:56 -0700147 test_cmp expect output
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100148'
149
Junio C Hamano41ac4142008-02-01 01:50:53 -0800150test_expect_success 'ambiguously abbreviated option' '
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100151 test-parse-options --strin 123;
Junio C Hamano41ac4142008-02-01 01:50:53 -0800152 test $? = 129
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100153'
154
Johannes Schindelin243e0612007-11-05 13:15:21 +0000155cat > expect << EOF
156boolean: 0
157integer: 0
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -0700158timestamp: 0
Johannes Schindelin243e0612007-11-05 13:15:21 +0000159string: 123
Stephan Beyer010a2da2008-06-22 17:04:26 +0200160abbrev: 7
161verbose: 0
162quiet: no
163dry run: no
Stephen Boyddf217ed2009-05-23 11:53:13 -0700164file: (not set)
Johannes Schindelin243e0612007-11-05 13:15:21 +0000165EOF
166
167test_expect_success 'non ambiguous option (after two options it abbreviates)' '
168 test-parse-options --st 123 > output 2> output.err &&
169 test ! -s output.err &&
Junio C Hamano3af82862008-05-23 22:28:56 -0700170 test_cmp expect output
Johannes Schindelin243e0612007-11-05 13:15:21 +0000171'
172
Stephan Beyer010a2da2008-06-22 17:04:26 +0200173cat > typo.err << EOF
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100174error: did you mean \`--boolean\` (with two dashes ?)
175EOF
176
177test_expect_success 'detect possible typos' '
Stephan Beyer010a2da2008-06-22 17:04:26 +0200178 test_must_fail test-parse-options -boolean > output 2> output.err &&
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100179 test ! -s output &&
Stephan Beyer010a2da2008-06-22 17:04:26 +0200180 test_cmp typo.err output.err
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100181'
182
Pierre Habouzit580d5bf2008-03-02 11:35:56 +0100183cat > expect <<EOF
184boolean: 0
185integer: 0
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -0700186timestamp: 0
Pierre Habouzit580d5bf2008-03-02 11:35:56 +0100187string: (not set)
Stephan Beyer010a2da2008-06-22 17:04:26 +0200188abbrev: 7
189verbose: 0
190quiet: no
191dry run: no
Stephen Boyddf217ed2009-05-23 11:53:13 -0700192file: (not set)
Pierre Habouzit580d5bf2008-03-02 11:35:56 +0100193arg 00: --quux
194EOF
195
196test_expect_success 'keep some options as arguments' '
197 test-parse-options --quux > output 2> output.err &&
198 test ! -s output.err &&
Junio C Hamano3af82862008-05-23 22:28:56 -0700199 test_cmp expect output
Pierre Habouzit580d5bf2008-03-02 11:35:56 +0100200'
201
Stephan Beyer010a2da2008-06-22 17:04:26 +0200202cat > expect <<EOF
203boolean: 0
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -0700204integer: 0
205timestamp: 1
Stephan Beyer010a2da2008-06-22 17:04:26 +0200206string: default
207abbrev: 7
208verbose: 0
209quiet: yes
210dry run: no
Stephen Boyddf217ed2009-05-23 11:53:13 -0700211file: (not set)
Stephan Beyer010a2da2008-06-22 17:04:26 +0200212arg 00: foo
213EOF
214
215test_expect_success 'OPT_DATE() and OPT_SET_PTR() work' '
216 test-parse-options -t "1970-01-01 00:00:01 +0000" --default-string \
217 foo -q > output 2> output.err &&
218 test ! -s output.err &&
219 test_cmp expect output
220'
221
222cat > expect <<EOF
223Callback: "four", 0
224boolean: 5
225integer: 4
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -0700226timestamp: 0
Stephan Beyer010a2da2008-06-22 17:04:26 +0200227string: (not set)
228abbrev: 7
229verbose: 0
230quiet: no
231dry run: no
Stephen Boyddf217ed2009-05-23 11:53:13 -0700232file: (not set)
Stephan Beyer010a2da2008-06-22 17:04:26 +0200233EOF
234
235test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
236 test-parse-options --length=four -b -4 > output 2> output.err &&
237 test ! -s output.err &&
238 test_cmp expect output
239'
240
241cat > expect <<EOF
242Callback: "not set", 1
243EOF
244
245test_expect_success 'OPT_CALLBACK() and callback errors work' '
246 test_must_fail test-parse-options --no-length > output 2> output.err &&
247 test_cmp expect output &&
248 test_cmp expect.err output.err
249'
250
251cat > expect <<EOF
252boolean: 1
253integer: 23
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -0700254timestamp: 0
Stephan Beyer010a2da2008-06-22 17:04:26 +0200255string: (not set)
256abbrev: 7
257verbose: 0
258quiet: no
259dry run: no
Stephen Boyddf217ed2009-05-23 11:53:13 -0700260file: (not set)
Stephan Beyer010a2da2008-06-22 17:04:26 +0200261EOF
262
263test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
264 test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err &&
265 test ! -s output.err &&
266 test_cmp expect output
267'
268
René Scharfe2f4b97f2009-05-07 21:44:17 +0200269test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
270 test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err &&
271 test ! -s output.err &&
272 test_cmp expect output
273'
274
275cat > expect <<EOF
276boolean: 6
277integer: 0
278timestamp: 0
279string: (not set)
280abbrev: 7
281verbose: 0
282quiet: no
283dry run: no
Stephen Boyddf217ed2009-05-23 11:53:13 -0700284file: (not set)
René Scharfe2f4b97f2009-05-07 21:44:17 +0200285EOF
286
287test_expect_success 'OPT_BIT() works' '
288 test-parse-options -bb --or4 > output 2> output.err &&
289 test ! -s output.err &&
290 test_cmp expect output
291'
292
293test_expect_success 'OPT_NEGBIT() works' '
294 test-parse-options -bb --no-neg-or4 > output 2> output.err &&
295 test ! -s output.err &&
296 test_cmp expect output
297'
Stephan Beyer010a2da2008-06-22 17:04:26 +0200298
René Scharfe51a99492009-05-07 21:45:42 +0200299test_expect_success 'OPT_BOOLEAN() with PARSE_OPT_NODASH works' '
300 test-parse-options + + + + + + > output 2> output.err &&
301 test ! -s output.err &&
302 test_cmp expect output
303'
304
René Scharfee0319ff2009-05-07 21:45:08 +0200305cat > expect <<EOF
306boolean: 0
307integer: 12345
308timestamp: 0
309string: (not set)
310abbrev: 7
311verbose: 0
312quiet: no
313dry run: no
Stephen Boyddf217ed2009-05-23 11:53:13 -0700314file: (not set)
René Scharfee0319ff2009-05-07 21:45:08 +0200315EOF
316
317test_expect_success 'OPT_NUMBER_CALLBACK() works' '
318 test-parse-options -12345 > output 2> output.err &&
319 test ! -s output.err &&
320 test_cmp expect output
321'
322
Andreas Schwab6bbfd1f2009-09-25 20:44:44 +0200323cat >expect <<EOF
324boolean: 0
325integer: 0
326timestamp: 0
327string: (not set)
328abbrev: 7
329verbose: 0
330quiet: no
331dry run: no
332file: (not set)
333EOF
334
335test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
336 test-parse-options --no-ambig >output 2>output.err &&
337 test ! -s output.err &&
338 test_cmp expect output
339'
340
Jeff Kingc8ba1632011-06-09 11:55:23 -0400341cat >>expect <<'EOF'
342list: foo
343list: bar
344list: baz
345EOF
346test_expect_success '--list keeps list of strings' '
347 test-parse-options --list foo --list=bar --list=baz >output &&
348 test_cmp expect output
349'
350
351test_expect_success '--no-list resets list' '
352 test-parse-options --list=other --list=irrelevant --list=options \
353 --no-list --list=foo --list=bar --list=baz >output &&
354 test_cmp expect output
355'
356
Johannes Schindelinbeb47432007-10-13 17:34:45 +0100357test_done