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