You're probably not going to like this answer: for providing an arg to an option that optionally takes a arg, you're not allowed to use a space:
$ getopt -o 'w::' -- -w -- arg
-w '' -- 'arg'
$ getopt -o 'w::' -- -w 1 -- arg
-w '' -- '1' 'arg'
$ getopt -o 'w::' -- -w1 -- arg
-w '1' -- 'arg'
Similarly, for option long argument, you must use =
$ getopt -o '' -l 'warning::' -- --warning -- arg
--warning '' -- 'arg'
$ getopt -o '' -l 'warning::' -- --warning 1 -- arg
--warning '' -- '1' 'arg'
$ getopt -o '' -l 'warning::' -- --warning=1 -- arg
--warning '1' -- 'arg'
From the man page:
A simple short option is a '-' followed by a short option character[...] If the option has an optional argument, it must be written
directly after the option character if present.
A long option normally begins with '--' [...] If the option has an optional
argument, it must be written directly after the long option name, separated
by '=', if present (if you add the '=' but nothing behind it, it is
interpreted as if no argument was present; this is a slight bug, see the
BUGS).
Just a code review comment about code layout: in an if-else-fi construct, put the short branch first. Here, by the time I get to else, I have to scan up to remember what the if condition was.
That also lets you write in an "assertion" style and out-dent the "main" code of the function:
region ()
{
# Process command line options
shortopts="hw::"
longopts="help,version,warning:"
opts=$(getopt -o "$shortopts" -l "$longopts" \
-n "$(basename $0)" -- "$@")
if [ $? -ne 0 ]; then
shorthelp=1 # getopt returned (and reported) an error.
return
fi
eval "set -- $opts"
# ...
Also, you can check the exit status directly and still assign to the variable (I'm also removing the basename call here)
if ! opts=$(getopt -o "$shortopts" -l "$longopts" -n "${0##*/}" -- "$@")
then
shorthelp=1 # getopt returned (and reported) an error.
return
fi
With -w I set level=1. Otherwise, for -wNUM I set level=NUM
local level=0
#...
case "$1" in
-w|--warning)
level=${2:-"1"}
;;
That uses the ${var:-default} form of parameter expansion.