OILS / spec / builtin-getopts.test.sh View on Github | oilshell.org

347 lines, 223 significant
1## compare_shells: dash bash mksh ash
2
3#### getopts empty
4set --
5getopts 'a:' opt
6echo "status=$? opt=$opt OPTARG=$OPTARG"
7## stdout: status=1 opt=? OPTARG=
8
9#### getopts sees unknown arg
10set -- -Z
11getopts 'a:' opt
12echo "status=$? opt=$opt OPTARG=$OPTARG"
13## stdout: status=0 opt=? OPTARG=
14
15#### getopts three invocations
16set -- -h -c foo
17getopts 'hc:' opt
18echo status=$? opt=$opt
19getopts 'hc:' opt
20echo status=$? opt=$opt
21getopts 'hc:' opt
22echo status=$? opt=$opt
23## STDOUT:
24status=0 opt=h
25status=0 opt=c
26status=1 opt=?
27## END
28
29#### getopts resets OPTARG
30set -- -c foo -h
31getopts 'hc:' opt
32echo status=$? opt=$opt OPTARG=$OPTARG
33getopts 'hc:' opt
34echo status=$? opt=$opt OPTARG=$OPTARG
35## STDOUT:
36status=0 opt=c OPTARG=foo
37status=0 opt=h OPTARG=
38## END
39
40#### Basic getopts invocation
41set -- -h -c foo x y z
42FLAG_h=0
43FLAG_c=''
44while getopts "hc:" opt; do
45 case $opt in
46 h) FLAG_h=1 ;;
47 c) FLAG_c="$OPTARG" ;;
48 esac
49done
50shift $(( OPTIND - 1 ))
51echo h=$FLAG_h c=$FLAG_c optind=$OPTIND argv=$@
52## stdout: h=1 c=foo optind=4 argv=x y z
53
54#### getopts with invalid variable name
55set -- -c foo -h
56getopts 'hc:' opt-
57echo status=$? opt=$opt OPTARG=$OPTARG OPTIND=$OPTIND
58## stdout: status=2 opt= OPTARG=foo OPTIND=3
59## OK bash stdout: status=1 opt= OPTARG=foo OPTIND=3
60## OK mksh stdout: status=1 opt= OPTARG= OPTIND=1
61
62#### getopts with invalid flag
63set -- -h -x
64while getopts "hc:" opt; do
65 case $opt in
66 h) FLAG_h=1 ;;
67 c) FLAG_c="$OPTARG" ;;
68 '?') echo ERROR $OPTIND; exit 2; ;;
69 esac
70done
71echo status=$?
72## stdout: ERROR 3
73## status: 2
74
75#### getopts with with -
76set -- -h -
77echo "$@"
78while getopts "hc:" opt; do
79 case $opt in
80 h) FLAG_h=1 ;;
81 c) FLAG_c="$OPTARG" ;;
82 '?') echo ERROR $OPTIND; exit 2; ;;
83 esac
84done
85echo status=$?
86## STDOUT:
87-h -
88status=0
89## END
90
91#### getopts missing required argument
92set -- -h -c
93while getopts "hc:" opt; do
94 case $opt in
95 h) FLAG_h=1 ;;
96 c) FLAG_c="$OPTARG" ;;
97 '?') echo ERROR $OPTIND; exit 2; ;;
98 esac
99done
100echo status=$?
101## stdout: ERROR 3
102## status: 2
103
104#### getopts doesn't look for flags after args
105set -- x -h -c y
106FLAG_h=0
107FLAG_c=''
108while getopts "hc:" opt; do
109 case $opt in
110 h) FLAG_h=1 ;;
111 c) FLAG_c="$OPTARG" ;;
112 esac
113done
114shift $(( OPTIND - 1 ))
115echo h=$FLAG_h c=$FLAG_c optind=$OPTIND argv=$@
116## stdout: h=0 c= optind=1 argv=x -h -c y
117
118#### getopts with explicit args
119# NOTE: Alpine doesn't appear to use this, but bash-completion does.
120FLAG_h=0
121FLAG_c=''
122arg=''
123set -- A B C
124while getopts "hc:" opt -h -c foo x y z; do
125 case $opt in
126 h) FLAG_h=1 ;;
127 c) FLAG_c="$OPTARG" ;;
128 esac
129done
130echo h=$FLAG_h c=$FLAG_c optind=$OPTIND argv=$@
131## STDOUT:
132h=1 c=foo optind=4 argv=A B C
133## END
134
135#### OPTIND
136echo $OPTIND
137## stdout: 1
138
139#### OPTIND after multiple getopts with same spec
140while getopts "hc:" opt; do
141 echo '-'
142done
143echo $OPTIND
144
145set -- -h -c foo x y z
146while getopts "hc:" opt; do
147 echo '-'
148done
149echo $OPTIND
150
151set --
152while getopts "hc:" opt; do
153 echo '-'
154done
155echo $OPTIND
156## stdout-json: "1\n-\n-\n4\n1\n"
157## BUG mksh/osh stdout-json: "1\n-\n-\n4\n4\n"
158
159#### OPTIND after multiple getopts with different spec
160# Wow this is poorly specified! A fundamental design problem with the global
161# variable OPTIND.
162set -- -a
163while getopts "ab:" opt; do
164 echo '.'
165done
166echo $OPTIND
167
168set -- -c -d -e foo
169while getopts "cde:" opt; do
170 echo '-'
171done
172echo $OPTIND
173
174set -- -f
175while getopts "f:" opt; do
176 echo '_'
177done
178echo $OPTIND
179## stdout-json: ".\n2\n-\n-\n5\n2\n"
180## BUG ash/dash stdout-json: ".\n2\n-\n-\n-\n5\n_\n2\n"
181## BUG mksh/osh stdout-json: ".\n2\n-\n-\n5\n5\n"
182
183#### OPTIND narrowed down
184FLAG_a=
185FLAG_b=
186FLAG_c=
187FLAG_d=
188FLAG_e=
189set -- -a
190while getopts "ab:" opt; do
191 case $opt in
192 a) FLAG_a=1 ;;
193 b) FLAG_b="$OPTARG" ;;
194 esac
195done
196# Bash doesn't reset optind! It skips over c! mksh at least warns about this!
197# You have to reset OPTIND yourself.
198
199set -- -c -d -e E
200while getopts "cde:" opt; do
201 case $opt in
202 c) FLAG_c=1 ;;
203 d) FLAG_d=1 ;;
204 e) FLAG_e="$OPTARG" ;;
205 esac
206done
207
208echo a=$FLAG_a b=$FLAG_b c=$FLAG_c d=$FLAG_d e=$FLAG_e
209## stdout: a=1 b= c=1 d=1 e=E
210## BUG bash/mksh/osh stdout: a=1 b= c= d=1 e=E
211
212
213#### Getopts parses the function's arguments
214FLAG_h=0
215FLAG_c=''
216myfunc() {
217 while getopts "hc:" opt; do
218 case $opt in
219 h) FLAG_h=1 ;;
220 c) FLAG_c="$OPTARG" ;;
221 esac
222 done
223}
224set -- -h -c foo x y z
225myfunc -c bar
226echo h=$FLAG_h c=$FLAG_c opt=$opt optind=$OPTIND argv=$@
227## stdout: h=0 c=bar opt=? optind=3 argv=-h -c foo x y z
228
229#### Local OPTIND
230# minimal test case extracted from bash-completion
231min() {
232 local OPTIND=1
233
234 while getopts "n:e:o:i:s" flag "$@"; do
235 echo "loop $OPTIND";
236 done
237}
238min -s
239## stdout: loop 2
240
241#### two flags: -ab
242getopts "ab" opt -ab
243echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
244getopts "ab" opt -ab
245echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
246## STDOUT:
247OPTIND=1 opt=a OPTARG=
248OPTIND=2 opt=b OPTARG=
249## END
250## OK dash/mksh/ash STDOUT:
251OPTIND=2 opt=a OPTARG=
252OPTIND=2 opt=b OPTARG=
253## END
254
255#### flag and arg: -c10
256getopts "c:" opt -c10
257echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
258getopts "c:" opt -c10
259echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
260## STDOUT:
261OPTIND=2 opt=c OPTARG=10
262OPTIND=2 opt=? OPTARG=
263## END
264## BUG dash STDOUT:
265OPTIND=2 opt=c OPTARG=10
266OPTIND=2 opt=? OPTARG=10
267## END
268
269#### More Smooshing 1
270getopts "ab:c:" opt -ab hi -c hello
271echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
272getopts "ab:c:" opt -ab hi -c hello
273echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
274getopts "ab:c:" opt -ab hi -c hello
275echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
276## STDOUT:
277OPTIND=1 opt=a OPTARG=
278OPTIND=3 opt=b OPTARG=hi
279OPTIND=5 opt=c OPTARG=hello
280## END
281## OK dash/mksh/ash STDOUT:
282OPTIND=2 opt=a OPTARG=
283OPTIND=3 opt=b OPTARG=hi
284OPTIND=5 opt=c OPTARG=hello
285## END
286
287#### More Smooshing 2
288getopts "abc:" opt -abc10
289echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
290getopts "abc:" opt -abc10
291echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
292getopts "abc:" opt -abc10
293echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
294## STDOUT:
295OPTIND=1 opt=a OPTARG=
296OPTIND=1 opt=b OPTARG=
297OPTIND=2 opt=c OPTARG=10
298## END
299## OK dash/mksh/ash STDOUT:
300OPTIND=2 opt=a OPTARG=
301OPTIND=2 opt=b OPTARG=
302OPTIND=2 opt=c OPTARG=10
303## END
304
305#### OPTIND should be >= 1 (regression)
306OPTIND=-1
307getopts a: foo
308echo status=$?
309
310OPTIND=0
311getopts a: foo
312echo status=$?
313## STDOUT:
314status=1
315status=1
316## END
317## OK dash status: 2
318## OK dash stdout-json: ""
319
320
321#### getopts bug #1523
322
323$SH $REPO_ROOT/spec/testdata/getopts-1523.sh -abcdef -abcde
324
325## status: 1
326## STDOUT:
327opt:a
328opt:b
329opt:c arg:def
330opt:a
331opt:b
332opt:c arg:de
333## END
334
335#### More regression for #1523
336
337$SH $REPO_ROOT/spec/testdata/getopts-1523.sh -abcdef -xyz
338
339## status: 1
340## STDOUT:
341opt:a
342opt:b
343opt:c arg:def
344err:?
345err:?
346err:?
347## END