[pacman-dev] [PATCH 3/3] parse_options: accept multiple arguments

Dave Reisner d at falconindy.com
Tue Jul 5 08:19:44 EDT 2011


On Tue, Jul 05, 2011 at 09:51:31PM +1000, Allan McRae wrote:
> 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 <allan at archlinux.org>
> ---
>  scripts/library/parse_options.sh |   21 ++++++++++++++++++---
>  1 files changed, 18 insertions(+), 3 deletions(-)
> 
> diff --git a/scripts/library/parse_options.sh b/scripts/library/parse_options.sh
> index 49cbb60..a2f9c1b 100644
> --- a/scripts/library/parse_options.sh
> +++ b/scripts/library/parse_options.sh
> @@ -28,7 +28,12 @@ parse_options() {
>  						if [[ -n $2 ]]; then
>  							printf ' %s' "$1"
>  							shift
> -							printf " '%s'" "$1"
> +							local arguments="$1"
> +							while [[ -n $2 && ${2:0:1} != "-" ]]; do
> +								shift
> +								arguments+=" $1"
> +							done
> +							printf " '%s'" "$arguments"

Does this ensure properly quoted multi word arguments are preserved?
Wouldn't it be easier to use an array and print with %q tokens? Same for
the next two instances.

>  						else
>  							printf "@SCRIPTNAME@: $(gettext "option %s requires an argument\n")" "'$1'" >&2
>  							ret=1
> @@ -56,12 +61,22 @@ parse_options() {
>  					else
>  						if [[ -n ${1:$i+1} ]]; then
>  							printf ' -%s' "${1:i:1}"
> -							printf " '%s'" "${1:$i+1}"
> +							local arguments="${1:$i+1}"
> +							while [[ -n $2 && ${2:0:1} != "-" ]]; do
> +								shift
> +								arguments+=" $1"
> +							done
> +							printf " '%s'" "$arguments"
>  						else
>  							if [[ -n $2 ]]; then
>  								printf ' -%s' "${1:i:1}"
>  								shift
> -								printf " '%s'" "${1}"
> +								local arguments="$1"
> +								while [[ -n $2 && ${2:0:1} != "-" ]]; do
> +									shift
> +									arguments+=" $1"
> +								done
> +								printf " '%s'" "$arguments"
>  							else
>  								printf "@SCRIPTNAME@: $(gettext "option %s requires an argument\n")" "'-${1:i:1}'" >&2
>  								ret=1
> -- 
> 1.7.6
> 
> 


More information about the pacman-dev mailing list