Handling long options with getopts
by Faki from LinuxQuestions.org on (#5RAKZ)
I am parsing options with `getopts` but would like to handle long
options as well.
Code:mygetopts ()
{
aggr=()
for arg in "$@"; do
shift 1
case $arg in
("--context") aggr+=( "-C" ) ;;
(*) aggr+=( "$arg" ) ;;
esac
done
set -- "${aggr[@]}"
local OPTIND OPTARG
local shortopts="C:"
while getopts "$shortopts" arg; do
echo "--> arg: $arg"
case $arg in
("C")
context="$OPTARG"
;;
(*)
break
;;
esac
done
}But I wonder whether the use of `set -- "${aggr[@]}"` is correct.
Or is the following (using `eval`) more appropriate?
Code:eval set -- "${aggr[@]}"or
Code:eval "set -- ${aggr[@]}"
options as well.
Code:mygetopts ()
{
aggr=()
for arg in "$@"; do
shift 1
case $arg in
("--context") aggr+=( "-C" ) ;;
(*) aggr+=( "$arg" ) ;;
esac
done
set -- "${aggr[@]}"
local OPTIND OPTARG
local shortopts="C:"
while getopts "$shortopts" arg; do
echo "--> arg: $arg"
case $arg in
("C")
context="$OPTARG"
;;
(*)
break
;;
esac
done
}But I wonder whether the use of `set -- "${aggr[@]}"` is correct.
Or is the following (using `eval`) more appropriate?
Code:eval set -- "${aggr[@]}"or
Code:eval "set -- ${aggr[@]}"