Article 5RAKZ Handling long options with getopts

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[@]}"latest?d=yIl2AUoC8zA latest?i=CLpm4GgFxjM:O_HpaxHXH4I:F7zBnMy latest?i=CLpm4GgFxjM:O_HpaxHXH4I:V_sGLiP latest?d=qj6IDK7rITs latest?i=CLpm4GgFxjM:O_HpaxHXH4I:gIN9vFwCLpm4GgFxjM
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments