I am using getopts
and have the intention of using an option -v
which can take an optional argument.
When using getopt
isnstead of getopts
, people customarily use the shift
command to go to the next argument. But I do not see people use shift
when using getopts
.
local vb=1 sort=0
local OPTIND OPTARG
local shortopts="Vuhvs"
while getopts $shortopts arg; do
case $arg in
("V") printf '%s\n' "Version" ; return ;;
("u") printf '%s\n' "usage" ; return ;;
("h") printf '%s\n' "help" ; return ;;
("v")
# Allows argument to be optional.
# Defines option with no arguments in shortopts.
nextarg=${!OPTIND}
if [[ (-n "$nextarg") && ("$nextarg" != -*) ]] ; then
OPTIND=$((OPTIND + 1))
vb="$nextarg"
fi
;;
#.............................
("s") sort=1 ; shift ;; # shifts arg by 1
#.............................
(?)
pfm "Invalid option: -${OPTARG}."
pfm "Invoke \`linge-pfa -h\` for details."
break
;;
esac
done