Open
Description
Feeding back a more radical change than the prior suggestions in PR #8 and issue #9.
What I now have in my template.sh
:
From memory, I iterated from this blog source and this gist.
function parse_params() {
# -allow a command to fail with !’s side effect on errexit
# -use return value from ${PIPESTATUS[0]}, because ! hosed $?
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
script_exit 'I’m sorry, `getopt --test` failed in this environment.' 2
fi
OPTIONS=chno:v
LONGOPTS=cron,help,no-color,output:,verbose
# -regarding ! and PIPESTATUS see above
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out “--options”)
# -pass arguments only via -- "$@" to separate them correctly
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# e.g. return value is 1
# then getopt has complained about wrong arguments to stdout
script_exit "Argument error" 2
fi
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
local param
...