2011-06-08 09:36:46 -04:00
|
|
|
# getopt like parser
|
|
|
|
parse_options() {
|
|
|
|
local short_options=$1; shift;
|
|
|
|
local long_options=$1; shift;
|
|
|
|
local ret=0;
|
|
|
|
local unused_options=""
|
|
|
|
local i
|
|
|
|
|
|
|
|
while [[ -n $1 ]]; do
|
|
|
|
if [[ ${1:0:2} = '--' ]]; then
|
|
|
|
if [[ -n ${1:2} ]]; then
|
|
|
|
local match=""
|
|
|
|
for i in ${long_options//,/ }; do
|
|
|
|
if [[ ${1:2} = ${i//:} ]]; then
|
|
|
|
match=$i
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [[ -n $match ]]; then
|
2011-07-05 05:01:22 -04:00
|
|
|
local needsargument=0
|
|
|
|
|
|
|
|
[[ ${match} = ${1:2}: ]] && needsargument=1
|
2011-07-05 06:14:20 -04:00
|
|
|
[[ ${match} = ${1:2}:: && -n $2 && ${2:0:1} != "-" ]] && needsargument=1
|
2011-07-05 05:01:22 -04:00
|
|
|
|
|
|
|
if (( ! needsargument )); then
|
2012-02-15 22:50:51 -05:00
|
|
|
OPTRET+=("$1")
|
2011-06-08 09:36:46 -04:00
|
|
|
else
|
|
|
|
if [[ -n $2 ]]; then
|
2012-02-15 22:50:51 -05:00
|
|
|
OPTRET+=("$1" "$2")
|
2011-06-08 09:36:46 -04:00
|
|
|
shift
|
2011-07-05 06:14:20 -04:00
|
|
|
while [[ -n $2 && ${2:0:1} != "-" ]]; do
|
|
|
|
shift
|
2012-02-15 22:50:51 -05:00
|
|
|
OPTRET+=("$1")
|
2011-07-05 06:14:20 -04:00
|
|
|
done
|
2011-06-08 09:36:46 -04:00
|
|
|
else
|
2011-07-05 03:34:03 -04:00
|
|
|
printf "@SCRIPTNAME@: $(gettext "option %s requires an argument\n")" "'$1'" >&2
|
2011-06-08 09:36:46 -04:00
|
|
|
ret=1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "@SCRIPTNAME@: $(gettext "unrecognized option") '$1'" >&2
|
|
|
|
ret=1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
elif [[ ${1:0:1} = '-' ]]; then
|
|
|
|
for ((i=1; i<${#1}; i++)); do
|
|
|
|
if [[ $short_options =~ ${1:i:1} ]]; then
|
2011-07-05 05:01:22 -04:00
|
|
|
local needsargument=0
|
|
|
|
|
|
|
|
[[ $short_options =~ ${1:i:1}: && ! $short_options =~ ${1:i:1}:: ]] && needsargument=1
|
|
|
|
[[ $short_options =~ ${1:i:1}:: && \
|
|
|
|
( -n ${1:$i+1} || ( -n $2 && ${2:0:1} != "-" ) ) ]] && needsargument=1
|
|
|
|
|
|
|
|
if (( ! needsargument )); then
|
2012-02-15 22:50:51 -05:00
|
|
|
OPTRET+=("-${1:i:1}")
|
2011-07-05 05:01:22 -04:00
|
|
|
else
|
2011-06-08 09:36:46 -04:00
|
|
|
if [[ -n ${1:$i+1} ]]; then
|
2012-02-15 22:50:51 -05:00
|
|
|
OPTRET+=("-${1:i:1}" "${1:i+1}")
|
2011-07-05 06:14:20 -04:00
|
|
|
while [[ -n $2 && ${2:0:1} != "-" ]]; do
|
|
|
|
shift
|
2012-02-15 22:50:51 -05:00
|
|
|
OPTRET+=("$1")
|
2011-07-05 06:14:20 -04:00
|
|
|
done
|
2011-06-08 09:36:46 -04:00
|
|
|
else
|
|
|
|
if [[ -n $2 ]]; then
|
2012-02-15 22:50:51 -05:00
|
|
|
OPTRET+=("-${1:i:1}" "$2")
|
2011-06-08 09:36:46 -04:00
|
|
|
shift
|
2011-07-05 06:14:20 -04:00
|
|
|
while [[ -n $2 && ${2:0:1} != "-" ]]; do
|
|
|
|
shift
|
2012-02-15 22:50:51 -05:00
|
|
|
OPTRET+=("$1")
|
2011-07-05 06:14:20 -04:00
|
|
|
done
|
|
|
|
|
2011-06-08 09:36:46 -04:00
|
|
|
else
|
2011-07-05 03:34:03 -04:00
|
|
|
printf "@SCRIPTNAME@: $(gettext "option %s requires an argument\n")" "'-${1:i:1}'" >&2
|
2011-06-08 09:36:46 -04:00
|
|
|
ret=1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
else
|
2011-06-13 08:36:41 -04:00
|
|
|
echo "@SCRIPTNAME@: $(gettext "unrecognized option") '-${1:i:1}'" >&2
|
2011-06-08 09:36:46 -04:00
|
|
|
ret=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
else
|
2012-02-15 22:50:51 -05:00
|
|
|
unused_options+=("$1")
|
2011-06-08 09:36:46 -04:00
|
|
|
fi
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
2012-02-15 22:50:51 -05:00
|
|
|
OPTRET+=('--' "${unused_options[@]}")
|
2011-06-08 09:36:46 -04:00
|
|
|
return $ret
|
2012-02-15 22:50:51 -05:00
|
|
|
}
|