I'm trying to understand why getopts
seems to ignore all arguments if an "unnnamed" argument precedes any named arguments.
Using an example from http://wiki.bash-hackers.org/howto/getopts_tutorial,
#!/bin/bash
while getopts ":a" opt; do
case $opt in
a)
echo "-a was triggered!" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
And observing the outcome:
$ ./opt_test
$ ./opt_test -a
-a was triggered!
$ ./opt_test -a -f
-a was triggered!
Invalid option: -f
$ ./opt_test a -a -f
$ ./opt_test a -a
$ ./opt_test a -f
$ ./opt_test lala -f
$
So prepending an unnamed argument (an argument without a dash) seems to make getopts
ignore all arguments.
Why is this and how can I work around it? I'd like my program to be able to catch such things and print a usage screen.