From bfd6d22be20d939dfd77a75f21c790b55548ab4a Mon Sep 17 00:00:00 2001 From: Allan McRae Date: Tue, 5 Jul 2011 20:14:20 +1000 Subject: parse_options: accept multiple arguments Allow command-line options to accept multiple arguments without additional quoting by taking the list of arguments until one starting with a "-" is reached. The only current use of this is the --pkg option in makepkg. This allows (e.g.) makepkg --pkg foo bar and packages "foo" and "bar" will be built. Signed-off-by: Allan McRae Signed-off-by: Dan McGee --- scripts/library/parse_options.sh | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'scripts/library') diff --git a/scripts/library/parse_options.sh b/scripts/library/parse_options.sh index 49cbb60f..48fd42cd 100644 --- a/scripts/library/parse_options.sh +++ b/scripts/library/parse_options.sh @@ -20,15 +20,20 @@ parse_options() { local needsargument=0 [[ ${match} = ${1:2}: ]] && needsargument=1 - [[ ${match} = ${1:2}:: && -n $2 && ${2:0:1} != "-" ]] && needsargument=1 + [[ ${match} = ${1:2}:: && -n $2 && ${2:0:1} != "-" ]] && needsargument=1 if (( ! needsargument )); then printf ' %s' "$1" else if [[ -n $2 ]]; then - printf ' %s' "$1" + printf ' %s ' "$1" shift - printf " '%s'" "$1" + printf "'%q" "$1" + while [[ -n $2 && ${2:0:1} != "-" ]]; do + shift + printf " %q" "$1" + done + printf "'" else printf "@SCRIPTNAME@: $(gettext "option %s requires an argument\n")" "'$1'" >&2 ret=1 @@ -55,13 +60,24 @@ parse_options() { printf ' -%s' "${1:i:1}" else if [[ -n ${1:$i+1} ]]; then - printf ' -%s' "${1:i:1}" - printf " '%s'" "${1:$i+1}" + printf ' -%s ' "${1:i:1}" + printf "'%q" "${1:$i+1}" + while [[ -n $2 && ${2:0:1} != "-" ]]; do + shift + printf " %q" "$1" + done + printf "'" else if [[ -n $2 ]]; then - printf ' -%s' "${1:i:1}" + printf ' -%s ' "${1:i:1}" shift - printf " '%s'" "${1}" + printf "'%q" "$1" + while [[ -n $2 && ${2:0:1} != "-" ]]; do + shift + printf " %q" "$1" + done + printf "'" + else printf "@SCRIPTNAME@: $(gettext "option %s requires an argument\n")" "'-${1:i:1}'" >&2 ret=1 -- cgit v1.2.3-24-g4f1b