OILS / spec / builtin-getopts.test.sh View on Github | oils.pub

490 lines, 311 significant
1## compare_shells: dash bash mksh ash
2## oils_failures_allowed: 3
3
4#### getopts empty
5set --
6getopts 'a:' opt
7echo "status=$? opt=$opt OPTARG=$OPTARG"
8## stdout: status=1 opt=? OPTARG=
9
10#### getopts sees unknown arg
11set -- -Z
12getopts 'a:' opt
13echo "status=$? opt=$opt OPTARG=$OPTARG"
14## STDOUT:
15status=0 opt=? OPTARG=
16## END
17
18#### getopts three invocations
19set -- -h -c foo
20getopts 'hc:' opt
21echo status=$? opt=$opt
22getopts 'hc:' opt
23echo status=$? opt=$opt
24getopts 'hc:' opt
25echo status=$? opt=$opt
26## STDOUT:
27status=0 opt=h
28status=0 opt=c
29status=1 opt=?
30## END
31
32#### getopts resets OPTARG
33set -- -c foo -h
34getopts 'hc:' opt
35echo status=$? opt=$opt OPTARG=$OPTARG
36getopts 'hc:' opt
37echo status=$? opt=$opt OPTARG=$OPTARG
38## STDOUT:
39status=0 opt=c OPTARG=foo
40status=0 opt=h OPTARG=
41## END
42
43#### OPTARG is empty (not unset) after parsing a flag doesn't take an arg
44
45set -u
46getopts 'ab' name '-a'
47echo name=$name
48echo OPTARG=$OPTARG
49
50## STDOUT:
51name=a
52OPTARG=
53## END
54
55## BUG bash/mksh status: 1
56## BUG bash/mksh STDOUT:
57name=a
58## END
59
60#### Basic getopts invocation
61set -- -h -c foo x y z
62FLAG_h=0
63FLAG_c=''
64while getopts "hc:" opt; do
65 case $opt in
66 h) FLAG_h=1 ;;
67 c) FLAG_c="$OPTARG" ;;
68 esac
69done
70shift $(( OPTIND - 1 ))
71echo h=$FLAG_h c=$FLAG_c optind=$OPTIND argv=$@
72## stdout: h=1 c=foo optind=4 argv=x y z
73
74#### getopts with invalid variable name
75set -- -c foo -h
76getopts 'hc:' opt-
77echo status=$? opt=$opt OPTARG=$OPTARG OPTIND=$OPTIND
78## stdout: status=2 opt= OPTARG=foo OPTIND=3
79## OK bash stdout: status=1 opt= OPTARG=foo OPTIND=3
80## OK mksh stdout: status=1 opt= OPTARG= OPTIND=1
81
82#### getopts with invalid flag
83set -- -h -x
84while getopts "hc:" opt; do
85 case $opt in
86 h) FLAG_h=1 ;;
87 c) FLAG_c="$OPTARG" ;;
88 '?') echo ERROR $OPTIND; exit 2; ;;
89 esac
90done
91echo status=$?
92## stdout: ERROR 3
93## status: 2
94
95#### getopts with with -
96set -- -h -
97echo "$@"
98while getopts "hc:" opt; do
99 case $opt in
100 h) FLAG_h=1 ;;
101 c) FLAG_c="$OPTARG" ;;
102 '?') echo ERROR $OPTIND; exit 2; ;;
103 esac
104done
105echo status=$?
106## STDOUT:
107-h -
108status=0
109## END
110
111#### getopts missing required argument
112set -- -h -c
113while getopts "hc:" opt; do
114 case $opt in
115 h) FLAG_h=1 ;;
116 c) FLAG_c="$OPTARG" ;;
117 '?') echo ERROR $OPTIND; exit 2; ;;
118 esac
119done
120echo status=$?
121## stdout: ERROR 3
122## status: 2
123
124#### getopts doesn't look for flags after args
125set -- x -h -c y
126FLAG_h=0
127FLAG_c=''
128while getopts "hc:" opt; do
129 case $opt in
130 h) FLAG_h=1 ;;
131 c) FLAG_c="$OPTARG" ;;
132 esac
133done
134shift $(( OPTIND - 1 ))
135echo h=$FLAG_h c=$FLAG_c optind=$OPTIND argv=$@
136## stdout: h=0 c= optind=1 argv=x -h -c y
137
138#### getopts with explicit args
139# NOTE: Alpine doesn't appear to use this, but bash-completion does.
140FLAG_h=0
141FLAG_c=''
142arg=''
143set -- A B C
144while getopts "hc:" opt -h -c foo x y z; do
145 case $opt in
146 h) FLAG_h=1 ;;
147 c) FLAG_c="$OPTARG" ;;
148 esac
149done
150echo h=$FLAG_h c=$FLAG_c optind=$OPTIND argv=$@
151## STDOUT:
152h=1 c=foo optind=4 argv=A B C
153## END
154
155#### OPTIND
156echo $OPTIND
157## stdout: 1
158
159#### OPTIND after multiple getopts with same spec
160while getopts "hc:" opt; do
161 echo '-'
162done
163echo OPTIND=$OPTIND
164
165set -- -h -c foo x y z
166while getopts "hc:" opt; do
167 echo '-'
168done
169echo OPTIND=$OPTIND
170
171set --
172while getopts "hc:" opt; do
173 echo '-'
174done
175echo OPTIND=$OPTIND
176
177## STDOUT:
178OPTIND=1
179-
180-
181OPTIND=4
182OPTIND=1
183## END
184## BUG mksh STDOUT:
185OPTIND=1
186-
187-
188OPTIND=4
189OPTIND=4
190## END
191
192#### OPTIND after multiple getopts with different spec
193# Wow this is poorly specified! A fundamental design problem with the global
194# variable OPTIND.
195set -- -a
196while getopts "ab:" opt; do
197 echo '.'
198done
199echo OPTIND=$OPTIND
200
201set -- -c -d -e foo
202while getopts "cde:" opt; do
203 echo '-'
204done
205echo OPTIND=$OPTIND
206
207set -- -f
208while getopts "f:" opt; do
209 echo '_'
210done
211echo OPTIND=$OPTIND
212
213## STDOUT:
214.
215OPTIND=2
216-
217-
218OPTIND=5
219OPTIND=2
220## END
221## BUG ash/dash STDOUT:
222.
223OPTIND=2
224-
225-
226-
227OPTIND=5
228_
229OPTIND=2
230## END
231## BUG mksh STDOUT:
232.
233OPTIND=2
234-
235-
236OPTIND=5
237OPTIND=5
238## END
239
240#### OPTIND narrowed down
241FLAG_a=
242FLAG_b=
243FLAG_c=
244FLAG_d=
245FLAG_e=
246set -- -a
247while getopts "ab:" opt; do
248 case $opt in
249 a) FLAG_a=1 ;;
250 b) FLAG_b="$OPTARG" ;;
251 esac
252done
253# Bash doesn't reset OPTIND! It skips over c! mksh at least warns about this!
254# You have to reset OPTIND yourself.
255
256set -- -c -d -e E
257while getopts "cde:" opt; do
258 case $opt in
259 c) FLAG_c=1 ;;
260 d) FLAG_d=1 ;;
261 e) FLAG_e="$OPTARG" ;;
262 esac
263done
264
265echo a=$FLAG_a b=$FLAG_b c=$FLAG_c d=$FLAG_d e=$FLAG_e
266
267## STDOUT:
268a=1 b= c=1 d=1 e=E
269## END
270
271## BUG bash/mksh STDOUT:
272a=1 b= c= d=1 e=E
273## END
274
275
276#### Getopts parses the function's arguments
277FLAG_h=0
278FLAG_c=''
279myfunc() {
280 while getopts "hc:" opt; do
281 case $opt in
282 h) FLAG_h=1 ;;
283 c) FLAG_c="$OPTARG" ;;
284 esac
285 done
286}
287set -- -h -c foo x y z
288myfunc -c bar
289echo h=$FLAG_h c=$FLAG_c opt=$opt optind=$OPTIND argv=$@
290## stdout: h=0 c=bar opt=? optind=3 argv=-h -c foo x y z
291
292#### Local OPTIND
293# minimal test case extracted from bash-completion
294min() {
295 local OPTIND=1
296
297 while getopts "n:e:o:i:s" flag "$@"; do
298 echo "loop $OPTIND";
299 done
300}
301min -s
302## stdout: loop 2
303
304#### two flags: -ab
305getopts "ab" opt -ab
306echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
307getopts "ab" opt -ab
308echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
309## STDOUT:
310OPTIND=1 opt=a OPTARG=
311OPTIND=2 opt=b OPTARG=
312## END
313## OK dash/mksh/ash STDOUT:
314OPTIND=2 opt=a OPTARG=
315OPTIND=2 opt=b OPTARG=
316## END
317
318#### flag and arg: -c10
319getopts "c:" opt -c10
320echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
321getopts "c:" opt -c10
322echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
323## STDOUT:
324OPTIND=2 opt=c OPTARG=10
325OPTIND=2 opt=? OPTARG=
326## END
327## BUG dash STDOUT:
328OPTIND=2 opt=c OPTARG=10
329OPTIND=2 opt=? OPTARG=10
330## END
331
332#### More Smooshing 1
333getopts "ab:c:" opt -ab hi -c hello
334echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
335getopts "ab:c:" opt -ab hi -c hello
336echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
337getopts "ab:c:" opt -ab hi -c hello
338echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
339## STDOUT:
340OPTIND=1 opt=a OPTARG=
341OPTIND=3 opt=b OPTARG=hi
342OPTIND=5 opt=c OPTARG=hello
343## END
344## OK dash/mksh/ash STDOUT:
345OPTIND=2 opt=a OPTARG=
346OPTIND=3 opt=b OPTARG=hi
347OPTIND=5 opt=c OPTARG=hello
348## END
349
350#### More Smooshing 2
351getopts "abc:" opt -abc10
352echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
353getopts "abc:" opt -abc10
354echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
355getopts "abc:" opt -abc10
356echo OPTIND=$OPTIND opt=$opt OPTARG=$OPTARG
357## STDOUT:
358OPTIND=1 opt=a OPTARG=
359OPTIND=1 opt=b OPTARG=
360OPTIND=2 opt=c OPTARG=10
361## END
362## OK dash/mksh/ash STDOUT:
363OPTIND=2 opt=a OPTARG=
364OPTIND=2 opt=b OPTARG=
365OPTIND=2 opt=c OPTARG=10
366## END
367
368#### OPTIND should be >= 1 (regression)
369OPTIND=-1
370getopts a: foo
371echo status=$?
372
373OPTIND=0
374getopts a: foo
375echo status=$?
376## STDOUT:
377status=1
378status=1
379## END
380## OK dash status: 2
381## OK dash stdout-json: ""
382
383
384#### getopts bug #1523
385
386$SH $REPO_ROOT/spec/testdata/getopts-1523.sh -abcdef -abcde
387
388## status: 1
389## STDOUT:
390opt:a
391opt:b
392opt:c arg:def
393opt:a
394opt:b
395opt:c arg:de
396## END
397
398#### More regression for #1523
399
400$SH $REPO_ROOT/spec/testdata/getopts-1523.sh -abcdef -xyz
401
402## status: 1
403## STDOUT:
404opt:a
405opt:b
406opt:c arg:def
407err:?
408err:?
409err:?
410## END
411
412#### getopts silent error reporting - invalid option
413# Leading : in optspec enables silent mode: OPTARG=option char, no error msg
414set -- -Z
415getopts ':a:' opt 2>&1
416echo "status=$? opt=$opt OPTARG=$OPTARG"
417## status: 0
418## stdout: status=0 opt=? OPTARG=Z
419## STDERR:
420## END
421
422#### getopts silent error reporting - missing required argument
423# Silent mode returns ':' and sets OPTARG to option char
424set -- -a
425getopts ':a:' opt 2>&1
426echo "status=$? opt=$opt OPTARG=$OPTARG"
427## status: 0
428## stdout: status=0 opt=: OPTARG=a
429## STDERR:
430## END
431
432#### getopts normal mode - invalid option (compare with silent)
433# Normal mode: OPTARG is empty, prints error message
434set -- -Z
435getopts 'a:' opt 2>/dev/null
436echo "status=$? opt=$opt OPTARG=$OPTARG"
437## status: 0
438## stdout: status=0 opt=? OPTARG=
439
440#### getopts normal mode - missing required argument (compare with silent)
441# Normal mode returns '?', OPTARG is empty
442set -- -a
443getopts 'a:' opt 2>/dev/null
444echo "status=$? opt=$opt OPTARG=$OPTARG"
445## status: 0
446## stdout: status=0 opt=? OPTARG=
447
448#### getopts handles '--' #2579
449set -- "-a" "--"
450while getopts "a" name; do
451 case "$name" in
452 a)
453 echo "a"
454 ;;
455 ?)
456 echo "?"
457 ;;
458 esac
459done
460echo "name=$name"
461echo "$OPTIND"
462## STDOUT:
463a
464name=?
4653
466## END
467
468#### getopts leaves all args after '--' as operands #2579
469set -- "-a" "--" "-c" "operand"
470while getopts "a" name; do
471 case "$name" in
472 a)
473 echo "a"
474 ;;
475 c)
476 echo "c"
477 ;;
478 ?)
479 echo "?"
480 ;;
481 esac
482done
483shift $((OPTIND - 1))
484echo "$#"
485echo "$@"
486## STDOUT:
487a
4882
489-c operand
490## END