FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > ArchLinux > ArchLinux Pacman Development

 
 
LinkBack Thread Tools
 
Old 07-05-2011, 12:19 PM
Dave Reisner
 
Default parse_options: accept multiple arguments

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@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
")" "'$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
")" "'-${1:i:1}'" >&2
> ret=1
> --
> 1.7.6
>
>
 
Old 07-05-2011, 01:50 PM
Allan McRae
 
Default parse_options: accept multiple arguments

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@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.


No it doesn't... and while this currently does not matter, it will
matter if the --add option in pacman-key if files are passed that have
spaces. Will fix.


Allan
 
Old 07-05-2011, 02:27 PM
Allan McRae
 
Default parse_options: accept multiple arguments

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@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
 

Thread Tools




All times are GMT. The time now is 10:33 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org