I want to use getopt to parsing my input, such as --count=123 --range=456 --err
Here is my test code:
argv=$(getopt --name `basename $0` --options '' --longoptions err::,count:,range: -- "$@") 2>&1 || show_usage
eval "set -- ${argv}"
echo "debug> [$argv]"
while true; do
case "$1" in
"--count")
echo "HitCount=$2"
echo "debug> [$1/$2/$3/$4/$5/$6/$7/$8]"
shift 2
echo "debug> [$1/$2/$3/$4/$5/$6/$7/$8]"
;;
"--range")
echo "ScanRange=$2"
echo "debug> [$1/$2/$3/$4/$5/$6/$7/$8]"
shift 2
echo "debug> [$1/$2/$3/$4/$5/$6/$7/$8]"
break
;;
"--")
echo "debug> [$1/$2/$3/$4/$5/$6/$7/$8]"
shift
echo "debug> [$1/$2/$3/$4/$5/$6/$7/$8]"
break
;;
*)
show_usage
break
;;
esac
done
test1: it's good
./getopt.sh --count=1234 --range=5678
debug> [ --count '1234' --range '5678' --]
HitCount=1234
debug> [--count/1234/--range/5678/--///]
debug> [--range/5678/--/////]
ScanRange=5678
debug> [--range/5678/--/////]
debug> [--///////]
test2: I only change the order in command line, but it cannot parsed "--count". I don't know why?
./getopt.sh --range=5678 --count=1234
debug> [ --range '5678' --count '1234' --]
ScanRange=5678
debug> [--range/5678/--count/1234/--///]
debug> [--count/1234/--/////]
CodePudding user response:
When passing parameters to getopt, does the order must same as the parsing order?
No.
it cannot parsed "--count". I don't know why?
You have break
before "--"
so it breaks the loop.