[pacman-dev] [PATCH 0/3] parse_options: implement features useful for pacman-key
The implementation of optional arguments and accepting multiple arguments are features that are required for using the option parser in pacman-key. e.g. pacman-key --add [<file(s)>] This can take no arguments or an arbitary number of arguments. Allan McRae (3): parse_options: add missing newlines parse_options: implement optional arguments parse_options: accept multiple arguments scripts/library/parse_options.sh | 44 ++++++++++++++++++++++++++++++------- 1 files changed, 35 insertions(+), 9 deletions(-) -- 1.7.6
Signed-off-by: Allan McRae <allan@archlinux.org> --- scripts/library/parse_options.sh | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/library/parse_options.sh b/scripts/library/parse_options.sh index 5ced260..9622230 100644 --- a/scripts/library/parse_options.sh +++ b/scripts/library/parse_options.sh @@ -25,7 +25,7 @@ parse_options() { shift printf " '%s'" "$1" else - printf "@SCRIPTNAME@: $(gettext "option %s requires an argument")" "'$1'" >&2 + printf "@SCRIPTNAME@: $(gettext "option %s requires an argument\n")" "'$1'" >&2 ret=1 fi fi @@ -50,7 +50,7 @@ parse_options() { shift printf " '%s'" "${1}" else - printf "@SCRIPTNAME@: $(gettext "option %s requires an argument")" "'-${1:i:1}'" >&2 + printf "@SCRIPTNAME@: $(gettext "option %s requires an argument\n")" "'-${1:i:1}'" >&2 ret=1 fi fi -- 1.7.6
On Tue, Jul 5, 2011 at 6:51 AM, Allan McRae <allan@archlinux.org> wrote:
Signed-off-by: Allan McRae <allan@archlinux.org>
I'll signoff, with a catch- looks like this file never got added to the relevant POTFILES.in so these strings will be lost if the translations are updated.
--- scripts/library/parse_options.sh | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/scripts/library/parse_options.sh b/scripts/library/parse_options.sh index 5ced260..9622230 100644 --- a/scripts/library/parse_options.sh +++ b/scripts/library/parse_options.sh @@ -25,7 +25,7 @@ parse_options() { shift printf " '%s'" "$1" else - printf "@SCRIPTNAME@: $(gettext "option %s requires an argument")" "'$1'" >&2 + printf "@SCRIPTNAME@: $(gettext "option %s requires an argument\n")" "'$1'" >&2 ret=1 fi fi @@ -50,7 +50,7 @@ parse_options() { shift printf " '%s'" "${1}" else - printf "@SCRIPTNAME@: $(gettext "option %s requires an argument")" "'-${1:i:1}'" >&2 + printf "@SCRIPTNAME@: $(gettext "option %s requires an argument\n")" "'-${1:i:1}'" >&2 ret=1 fi fi -- 1.7.6
On 05/07/11 23:37, Dan McGee wrote:
On Tue, Jul 5, 2011 at 6:51 AM, Allan McRae<allan@archlinux.org> wrote:
Signed-off-by: Allan McRae<allan@archlinux.org>
I'll signoff, with a catch- looks like this file never got added to the relevant POTFILES.in so these strings will be lost if the translations are updated.
Stuck a patch adding this file and the output_format.sh one to the relevant POTFILES.in. Is that all that needs done there? Allan
On Tue, Jul 5, 2011 at 8:51 AM, Allan McRae <allan@archlinux.org> wrote:
On 05/07/11 23:37, Dan McGee wrote:
On Tue, Jul 5, 2011 at 6:51 AM, Allan McRae<allan@archlinux.org> wrote:
Signed-off-by: Allan McRae<allan@archlinux.org>
I'll signoff, with a catch- looks like this file never got added to the relevant POTFILES.in so these strings will be lost if the translations are updated.
Stuck a patch adding this file and the output_format.sh one to the relevant POTFILES.in. Is that all that needs done there?
Yeah that should be it. Easiest way to check things out is $ make -C scripts/po pacman-scripts.pot-update And look at the resulting translation catalog. -Dan
This allows options specified with a trailing "::" to optionally take arguments. Signed-off-by: Allan McRae <allan@archlinux.org> --- scripts/library/parse_options.sh | 19 +++++++++++++++---- 1 files changed, 15 insertions(+), 4 deletions(-) diff --git a/scripts/library/parse_options.sh b/scripts/library/parse_options.sh index 9622230..49cbb60 100644 --- a/scripts/library/parse_options.sh +++ b/scripts/library/parse_options.sh @@ -17,7 +17,12 @@ parse_options() { fi done if [[ -n $match ]]; then - if [[ ${1:2} = $match ]]; then + local needsargument=0 + + [[ ${match} = ${1:2}: ]] && needsargument=1 + [[ ${match} = ${1:2}:: && -n $2 && ${2:0:1} != "-" ]] && needsargument=1 + + if (( ! needsargument )); then printf ' %s' "$1" else if [[ -n $2 ]]; then @@ -40,7 +45,15 @@ parse_options() { elif [[ ${1:0:1} = '-' ]]; then for ((i=1; i<${#1}; i++)); do if [[ $short_options =~ ${1:i:1} ]]; then - if [[ $short_options =~ ${1:i:1}: ]]; then + 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 + printf ' -%s' "${1:i:1}" + else if [[ -n ${1:$i+1} ]]; then printf ' -%s' "${1:i:1}" printf " '%s'" "${1:$i+1}" @@ -55,8 +68,6 @@ parse_options() { fi fi break - else - printf ' -%s' "${1:i:1}" fi else echo "@SCRIPTNAME@: $(gettext "unrecognized option") '-${1:i:1}'" >&2 -- 1.7.6
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" 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
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\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
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
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
participants (3)
-
Allan McRae
-
Dan McGee
-
Dave Reisner