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

Allan McRae allan at archlinux.org
Tue Jul 5 10:27:32 EDT 2011


On 05/07/11 22:19, Dave Reisner wrote:
> 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.

Changed the above segment to:

if [[ -n $2 ]]; then
	printf ' %s ' "$1"
	shift
	printf "'%q" "$1"
	while [[ -n $2 && ${2:0:1} != "-" ]]; do
		shift
		printf " %q" "$1"
	done
	printf "'"

which outputs exactly the same thing but with the correct escapes.  e.g.

 > ./test.sh --add foo\ bar.asc baz.asc
  --add 'foo\ bar.asc baz.asc' --

which is escaped enough for the only current intended usage where spaces 
matter  (filenames in "pacman-key --add") so that is good enough for me.

Allan


More information about the pacman-dev mailing list