[pacman-dev] [PATCH] makepkg: move option parsing code to separte file
This move the getopt replacement function parse_options out of makepkg.sh.in and into a separate file. The code is inserted into the relevant place in makepkg using m4. This will allow the reuse of the option parsing code in other scripts (i.e. pacman-key) while avoiding code duplication. Signed-off-by: Allan McRae <allan@archlinux.org> --- scripts/Makefile.am | 10 +++- scripts/library/parse_options.sh | 87 +++++++++++++++++++++++++++++++++++++ scripts/makepkg.sh.in | 88 +------------------------------------- 3 files changed, 95 insertions(+), 90 deletions(-) create mode 100644 scripts/library/parse_options.sh diff --git a/scripts/Makefile.am b/scripts/Makefile.am index daa5b2a..74ee180 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -23,6 +23,9 @@ EXTRA_DIST = \ rankmirrors.sh.in \ repo-add.sh.in +LIBRARY = \ + library/parse_options.sh + # Files that should be removed, but which Automake does not know. MOSTLYCLEANFILES = $(bin_SCRIPTS) *.tmp @@ -47,6 +50,7 @@ edit = sed \ -e 's|@SIZECMD[@]|$(SIZECMD)|g' \ -e 's|@SEDINPLACE[@]|$(SEDINPLACE)|g' \ -e 's|@DUPATH[@]|$(DUPATH)|g' \ + -e 's|@SCRIPTNAME[@]|$@|g' \ -e 's|@configure_input[@]|Generated from $@.sh.in; do not edit by hand.|g' ## All the scripts depend on Makefile so that they are rebuilt when the @@ -54,11 +58,11 @@ edit = sed \ ## wrong file by accident. # two 'test' lines- make sure we can handle both sh and py type scripts # third 'test' line- make sure one of the two checks succeeded -$(OURSCRIPTS): Makefile +$(OURSCRIPTS): Makefile $(LIBRARY) @echo ' ' GEN $@; @rm -f $@ $@.tmp - @test -f $(srcdir)/$@.sh.in && $(edit) $(srcdir)/$@.sh.in >$@.tmp || true - @test -f $(srcdir)/$@.py.in && $(edit) $(srcdir)/$@.py.in >$@.tmp || true + @test -f $(srcdir)/$@.sh.in && m4 -P $(srcdir)/$@.sh.in | $(edit) >$@.tmp || true + @test -f $(srcdir)/$@.py.in && m4 -P $(srcdir)/$@.py.in | $(edit) >$@.tmp || true @test -f $@.tmp || false @chmod +x $@.tmp @chmod a-w $@.tmp diff --git a/scripts/library/parse_options.sh b/scripts/library/parse_options.sh new file mode 100644 index 0000000..32aa0be --- /dev/null +++ b/scripts/library/parse_options.sh @@ -0,0 +1,87 @@ +# getopt like parser +parse_options() { + local short_options=$1; shift; + local long_options=$1; shift; + local ret=0; + local unused_options="" + local i + + while [[ -n $1 ]]; do + if [[ ${1:0:2} = '--' ]]; then + if [[ -n ${1:2} ]]; then + local match="" + for i in ${long_options//,/ }; do + if [[ ${1:2} = ${i//:} ]]; then + match=$i + break + fi + done + if [[ -n $match ]]; then + if [[ ${1:2} = $match ]]; then + printf ' %s' "$1" + else + if [[ -n $2 ]]; then + printf ' %s' "$1" + shift + printf " '%s'" "$1" + else + echo "@SCRIPTNAME@: option '$1' $(gettext "requires an argument")" >&2 + ret=1 + fi + fi + else + echo "@SCRIPTNAME@: $(gettext "unrecognized option") '$1'" >&2 + ret=1 + fi + else + shift + break + fi + 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 + if [[ -n ${1:$i+1} ]]; then + printf ' -%s' "${1:i:1}" + printf " '%s'" "${1:$i+1}" + else + if [[ -n $2 ]]; then + printf ' -%s' "${1:i:1}" + shift + printf " '%s'" "${1}" + else + echo "@SCRIPTNAME@: option $(gettext "requires an argument") -- '${1:i:1}'" >&2 + ret=1 + fi + fi + break + else + printf ' -%s' "${1:i:1}" + fi + else + echo "@SCRIPTNAME@: $(gettext "invalid option") -- '${1:i:1}'" >&2 + ret=1 + fi + done + else + unused_options="${unused_options} '$1'" + fi + shift + done + + printf " --" + if [[ -n $unused_options ]]; then + for i in ${unused_options[@]}; do + printf ' %s' "$i" + done + fi + if [[ -n $1 ]]; then + while [[ -n $1 ]]; do + printf " '%s'" "${1}" + shift + done + fi + printf "\n" + + return $ret +} \ No newline at end of file diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index b3081fc..0639fbd 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -1506,93 +1506,7 @@ canonicalize_path() { fi } -# getopt like parser -parse_options() { - local short_options=$1; shift; - local long_options=$1; shift; - local ret=0; - local unused_options="" - local i - - while [[ -n $1 ]]; do - if [[ ${1:0:2} = '--' ]]; then - if [[ -n ${1:2} ]]; then - local match="" - for i in ${long_options//,/ }; do - if [[ ${1:2} = ${i//:} ]]; then - match=$i - break - fi - done - if [[ -n $match ]]; then - if [[ ${1:2} = $match ]]; then - printf ' %s' "$1" - else - if [[ -n $2 ]]; then - printf ' %s' "$1" - shift - printf " '%s'" "$1" - else - echo "makepkg: option '$1' $(gettext "requires an argument")" >&2 - ret=1 - fi - fi - else - echo "makepkg: $(gettext "unrecognized option") '$1'" >&2 - ret=1 - fi - else - shift - break - fi - 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 - if [[ -n ${1:$i+1} ]]; then - printf ' -%s' "${1:i:1}" - printf " '%s'" "${1:$i+1}" - else - if [[ -n $2 ]]; then - printf ' -%s' "${1:i:1}" - shift - printf " '%s'" "${1}" - else - echo "makepkg: option $(gettext "requires an argument") -- '${1:i:1}'" >&2 - ret=1 - fi - fi - break - else - printf ' -%s' "${1:i:1}" - fi - else - echo "makepkg: $(gettext "invalid option") -- '${1:i:1}'" >&2 - ret=1 - fi - done - else - unused_options="${unused_options} '$1'" - fi - shift - done - - printf " --" - if [[ -n $unused_options ]]; then - for i in ${unused_options[@]}; do - printf ' %s' "$i" - done - fi - if [[ -n $1 ]]; then - while [[ -n $1 ]]; do - printf " '%s'" "${1}" - shift - done - fi - printf "\n" - - return $ret -} +m4_include(library/parse_options.sh) usage() { printf "makepkg (pacman) %s\n" "$myver" -- 1.7.5.4
Move the common output formatting functions into a separate library file and import that into each script. makepkg is excluded dues to its additional color formatting. Signed-off-by: Allan McRae <allan@archlinux.org> --- scripts/Makefile.am | 1 + scripts/library/output_format.sh | 21 +++++++++++++++++++++ scripts/pacman-db-upgrade.sh.in | 9 +-------- scripts/pacman-key.sh.in | 21 +-------------------- scripts/pkgdelta.sh.in | 16 +--------------- scripts/repo-add.sh.in | 22 +--------------------- 6 files changed, 26 insertions(+), 64 deletions(-) create mode 100644 scripts/library/output_format.sh diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 74ee180..303731c 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -24,6 +24,7 @@ EXTRA_DIST = \ repo-add.sh.in LIBRARY = \ + library/output_format.sh \ library/parse_options.sh # Files that should be removed, but which Automake does not know. diff --git a/scripts/library/output_format.sh b/scripts/library/output_format.sh new file mode 100644 index 0000000..9e890e7 --- /dev/null +++ b/scripts/library/output_format.sh @@ -0,0 +1,21 @@ +msg() { + (( QUIET )) && return + local mesg=$1; shift + printf "==> ${mesg}\n" "$@" >&1 +} + +msg2() { + (( QUIET )) && return + local mesg=$1; shift + printf " -> ${mesg}\n" "$@" >&1 +} + +warning() { + local mesg=$1; shift + printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2 +} + +error() { + local mesg=$1; shift + printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2 +} \ No newline at end of file diff --git a/scripts/pacman-db-upgrade.sh.in b/scripts/pacman-db-upgrade.sh.in index 17d8beb..eaaa934 100644 --- a/scripts/pacman-db-upgrade.sh.in +++ b/scripts/pacman-db-upgrade.sh.in @@ -28,15 +28,8 @@ myver='@PACKAGE_VERSION@' eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf) dbroot="${DBPath:-@localstatedir@/lib/pacman/}" -msg() { - local mesg=$1; shift - printf "==> ${mesg}\n" "$@" >&2 -} +m4_include(library/output_format.sh) -error () { - local mesg=$1; shift - printf "==> ERROR: ${mesg}\n" "$@" >&2 -} usage() { printf "pacman-db-upgrade (pacman) %s\n\n" "$myver" printf "$(gettext "Usage: %s [pacman_db_root]")\n\n" "$0" diff --git a/scripts/pacman-key.sh.in b/scripts/pacman-key.sh.in index 20ec20f..a757c9b 100644 --- a/scripts/pacman-key.sh.in +++ b/scripts/pacman-key.sh.in @@ -26,26 +26,7 @@ export TEXTDOMAINDIR='@localedir@' myver="@PACKAGE_VERSION@" -msg() { - local mesg=$1; shift - printf "==> ${mesg}\n" "$@" >&1 -} - -msg2() { - (( QUIET )) && return - local mesg=$1; shift - printf " -> ${mesg}\n" "$@" >&1 -} - -warning() { - local mesg=$1; shift - printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2 -} - -error() { - local mesg=$1; shift - printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2 -} +m4_include(library/output_format.sh) usage() { printf "pacman-key (pacman) %s\n" ${myver} diff --git a/scripts/pkgdelta.sh.in b/scripts/pkgdelta.sh.in index d17b41d..c5a4882 100644 --- a/scripts/pkgdelta.sh.in +++ b/scripts/pkgdelta.sh.in @@ -34,21 +34,7 @@ QUIET=0 # ensure we have a sane umask set umask 0022 -msg() { - (( QUIET )) && return - local mesg=$1; shift - printf "==> ${mesg}\n" "$@" >&1 -} - -warning() { - local mesg=$1; shift - printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2 -} - -error() { - local mesg=$1; shift - printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2 -} +m4_include(library/output_format.sh) # print usage instructions usage() { diff --git a/scripts/repo-add.sh.in b/scripts/repo-add.sh.in index 0ffc0df..6ccefd7 100644 --- a/scripts/repo-add.sh.in +++ b/scripts/repo-add.sh.in @@ -38,27 +38,7 @@ CLEAN_LOCK=0 # ensure we have a sane umask set umask 0022 -msg() { - (( QUIET )) && return - local mesg=$1; shift - printf "==> ${mesg}\n" "$@" >&1 -} - -msg2() { - (( QUIET )) && return - local mesg=$1; shift - printf " -> ${mesg}\n" "$@" >&1 -} - -warning() { - local mesg=$1; shift - printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2 -} - -error() { - local mesg=$1; shift - printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2 -} +m4_include(library/output_format.sh) # print usage instructions usage() { -- 1.7.5.4
On Fri, Jun 10, 2011 at 4:36 AM, Allan McRae <allan@archlinux.org> wrote:
Move the common output formatting functions into a separate library file and import that into each script. makepkg is excluded dues to its additional color formatting. *due to
Otherwise, looks good. Depending on what you decide with the Makefile dependencies stuff I mentioned in reply to the other library func email, you may need to adjust things here too (and add it to the 4 lines requiring it).
Signed-off-by: Allan McRae <allan@archlinux.org> --- scripts/Makefile.am | 1 + scripts/library/output_format.sh | 21 +++++++++++++++++++++ scripts/pacman-db-upgrade.sh.in | 9 +-------- scripts/pacman-key.sh.in | 21 +-------------------- scripts/pkgdelta.sh.in | 16 +--------------- scripts/repo-add.sh.in | 22 +--------------------- 6 files changed, 26 insertions(+), 64 deletions(-) create mode 100644 scripts/library/output_format.sh
diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 74ee180..303731c 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -24,6 +24,7 @@ EXTRA_DIST = \ repo-add.sh.in
LIBRARY = \ + library/output_format.sh \ library/parse_options.sh
You'll want to add deps accordingly in the Makefile as well, so if someone updates the library file the right scripts get rebuilt.
# Files that should be removed, but which Automake does not know. diff --git a/scripts/library/output_format.sh b/scripts/library/output_format.sh new file mode 100644 index 0000000..9e890e7 --- /dev/null +++ b/scripts/library/output_format.sh @@ -0,0 +1,21 @@ +msg() { + (( QUIET )) && return + local mesg=$1; shift + printf "==> ${mesg}\n" "$@" >&1 +} + +msg2() { + (( QUIET )) && return + local mesg=$1; shift + printf " -> ${mesg}\n" "$@" >&1 +} + +warning() { + local mesg=$1; shift + printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2 +} + +error() { + local mesg=$1; shift + printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2 +} \ No newline at end of file diff --git a/scripts/pacman-db-upgrade.sh.in b/scripts/pacman-db-upgrade.sh.in index 17d8beb..eaaa934 100644 --- a/scripts/pacman-db-upgrade.sh.in +++ b/scripts/pacman-db-upgrade.sh.in @@ -28,15 +28,8 @@ myver='@PACKAGE_VERSION@' eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf) dbroot="${DBPath:-@localstatedir@/lib/pacman/}"
-msg() { - local mesg=$1; shift - printf "==> ${mesg}\n" "$@" >&2 -} +m4_include(library/output_format.sh)
-error () { - local mesg=$1; shift - printf "==> ERROR: ${mesg}\n" "$@" >&2 -} usage() { printf "pacman-db-upgrade (pacman) %s\n\n" "$myver" printf "$(gettext "Usage: %s [pacman_db_root]")\n\n" "$0" diff --git a/scripts/pacman-key.sh.in b/scripts/pacman-key.sh.in index 20ec20f..a757c9b 100644 --- a/scripts/pacman-key.sh.in +++ b/scripts/pacman-key.sh.in @@ -26,26 +26,7 @@ export TEXTDOMAINDIR='@localedir@'
myver="@PACKAGE_VERSION@"
-msg() { - local mesg=$1; shift - printf "==> ${mesg}\n" "$@" >&1 -} - -msg2() { - (( QUIET )) && return - local mesg=$1; shift - printf " -> ${mesg}\n" "$@" >&1 -} - -warning() { - local mesg=$1; shift - printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2 -} - -error() { - local mesg=$1; shift - printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2 -} +m4_include(library/output_format.sh)
usage() { printf "pacman-key (pacman) %s\n" ${myver} diff --git a/scripts/pkgdelta.sh.in b/scripts/pkgdelta.sh.in index d17b41d..c5a4882 100644 --- a/scripts/pkgdelta.sh.in +++ b/scripts/pkgdelta.sh.in @@ -34,21 +34,7 @@ QUIET=0 # ensure we have a sane umask set umask 0022
-msg() { - (( QUIET )) && return - local mesg=$1; shift - printf "==> ${mesg}\n" "$@" >&1 -} - -warning() { - local mesg=$1; shift - printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2 -} - -error() { - local mesg=$1; shift - printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2 -} +m4_include(library/output_format.sh)
# print usage instructions usage() { diff --git a/scripts/repo-add.sh.in b/scripts/repo-add.sh.in index 0ffc0df..6ccefd7 100644 --- a/scripts/repo-add.sh.in +++ b/scripts/repo-add.sh.in @@ -38,27 +38,7 @@ CLEAN_LOCK=0 # ensure we have a sane umask set umask 0022
-msg() { - (( QUIET )) && return - local mesg=$1; shift - printf "==> ${mesg}\n" "$@" >&1 -} - -msg2() { - (( QUIET )) && return - local mesg=$1; shift - printf " -> ${mesg}\n" "$@" >&1 -} - -warning() { - local mesg=$1; shift - printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2 -} - -error() { - local mesg=$1; shift - printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2 -} +m4_include(library/output_format.sh)
# print usage instructions usage() { -- 1.7.5.4
Add a README file to brief document the code snippets in the scripts library folder. Signed-off-by: Allan McRae <allan@archlinux.org> --- scripts/library/README | 15 +++++++++++++++ 1 files changed, 15 insertions(+), 0 deletions(-) create mode 100644 scripts/library/README diff --git a/scripts/library/README b/scripts/library/README new file mode 100644 index 0000000..1e9c962 --- /dev/null +++ b/scripts/library/README @@ -0,0 +1,15 @@ +This folder contains code snippets that can be reused by multiple +scripts. A brief description of each file follows. + +output_format.sh: +Provides basic output formatting functions with levels 'msg', 'msg2', +'warning' and 'error'. The 'msg' amd 'msg2' functions print to stdout +and can be silenced by defining 'QUIET'. The 'warning' and 'error' +functions print to stderr with the appropriate prefix added to the +message. + +parse_options.sh: +A getopt replacement to avoids portability issues, in particular the +lack of long option name support in the default getopt provided by some +platforms. +Usage: parse_option $SHORT_OPTS $LONG_OPTS "$@" -- 1.7.5.4
Subject typo ^^ On Fri, Jun 10, 2011 at 4:36 AM, Allan McRae <allan@archlinux.org> wrote:
This move the getopt replacement function parse_options out of makepkg.sh.in and into a separate file. The code is inserted into the relevant place in makepkg using m4.
This will allow the reuse of the option parsing code in other scripts (i.e. pacman-key) while avoiding code duplication.
Signed-off-by: Allan McRae <allan@archlinux.org> --- scripts/Makefile.am | 10 +++- scripts/library/parse_options.sh | 87 +++++++++++++++++++++++++++++++++++++ scripts/makepkg.sh.in | 88 +------------------------------------- 3 files changed, 95 insertions(+), 90 deletions(-) create mode 100644 scripts/library/parse_options.sh
diff --git a/scripts/Makefile.am b/scripts/Makefile.am index daa5b2a..74ee180 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -23,6 +23,9 @@ EXTRA_DIST = \ rankmirrors.sh.in \ repo-add.sh.in
+LIBRARY = \ + library/parse_options.sh + # Files that should be removed, but which Automake does not know. MOSTLYCLEANFILES = $(bin_SCRIPTS) *.tmp
@@ -47,6 +50,7 @@ edit = sed \ -e 's|@SIZECMD[@]|$(SIZECMD)|g' \ -e 's|@SEDINPLACE[@]|$(SEDINPLACE)|g' \ -e 's|@DUPATH[@]|$(DUPATH)|g' \ + -e 's|@SCRIPTNAME[@]|$@|g' \ -e 's|@configure_input[@]|Generated from $@.sh.in; do not edit by hand.|g'
## All the scripts depend on Makefile so that they are rebuilt when the @@ -54,11 +58,11 @@ edit = sed \ ## wrong file by accident. # two 'test' lines- make sure we can handle both sh and py type scripts # third 'test' line- make sure one of the two checks succeeded -$(OURSCRIPTS): Makefile +$(OURSCRIPTS): Makefile $(LIBRARY)
Is this overkill for the deps? I think I'd rather just see explicitly down below what needs to go on, e.g. makepkg: $(srcdir)/makepkg.sh.in library/parse_options.sh
@echo ' ' GEN $@; @rm -f $@ $@.tmp - @test -f $(srcdir)/$@.sh.in && $(edit) $(srcdir)/$@.sh.in >$@.tmp || true - @test -f $(srcdir)/$@.py.in && $(edit) $(srcdir)/$@.py.in >$@.tmp || true + @test -f $(srcdir)/$@.sh.in && m4 -P $(srcdir)/$@.sh.in | $(edit) >$@.tmp || true + @test -f $(srcdir)/$@.py.in && m4 -P $(srcdir)/$@.py.in | $(edit) >$@.tmp || true
We don't actually have any python scripts anymore I just realized... I'll kill the cruft in another patch.
@test -f $@.tmp || false @chmod +x $@.tmp @chmod a-w $@.tmp diff --git a/scripts/library/parse_options.sh b/scripts/library/parse_options.sh new file mode 100644 index 0000000..32aa0be --- /dev/null +++ b/scripts/library/parse_options.sh @@ -0,0 +1,87 @@ +# getopt like parser +parse_options() { maybe a local scriptname="@@SCRIPTNAME@@" once here to avoid scattering it all over the place?
+ local short_options=$1; shift; + local long_options=$1; shift; + local ret=0; + local unused_options="" + local i + + while [[ -n $1 ]]; do + if [[ ${1:0:2} = '--' ]]; then + if [[ -n ${1:2} ]]; then + local match="" + for i in ${long_options//,/ }; do + if [[ ${1:2} = ${i//:} ]]; then + match=$i + break + fi + done + if [[ -n $match ]]; then + if [[ ${1:2} = $match ]]; then + printf ' %s' "$1" + else + if [[ -n $2 ]]; then + printf ' %s' "$1" + shift + printf " '%s'" "$1" + else + echo "@SCRIPTNAME@: option '$1' $(gettext "requires an argument")" >&2 + ret=1 + fi + fi + else + echo "@SCRIPTNAME@: $(gettext "unrecognized option") '$1'" >&2 + ret=1 + fi + else + shift + break + fi + 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 + if [[ -n ${1:$i+1} ]]; then + printf ' -%s' "${1:i:1}" + printf " '%s'" "${1:$i+1}" + else + if [[ -n $2 ]]; then + printf ' -%s' "${1:i:1}" + shift + printf " '%s'" "${1}" + else + echo "@SCRIPTNAME@: option $(gettext "requires an argument") -- '${1:i:1}'" >&2 + ret=1 + fi + fi + break + else + printf ' -%s' "${1:i:1}" + fi + else + echo "@SCRIPTNAME@: $(gettext "invalid option") -- '${1:i:1}'" >&2 + ret=1 + fi + done + else + unused_options="${unused_options} '$1'" + fi + shift + done + + printf " --" + if [[ -n $unused_options ]]; then + for i in ${unused_options[@]}; do + printf ' %s' "$i" + done + fi + if [[ -n $1 ]]; then + while [[ -n $1 ]]; do + printf " '%s'" "${1}" + shift + done + fi + printf "\n" + + return $ret +} \ No newline at end of file diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index b3081fc..0639fbd 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -1506,93 +1506,7 @@ canonicalize_path() { fi }
-# getopt like parser -parse_options() { - local short_options=$1; shift; - local long_options=$1; shift; - local ret=0; - local unused_options="" - local i - - while [[ -n $1 ]]; do - if [[ ${1:0:2} = '--' ]]; then - if [[ -n ${1:2} ]]; then - local match="" - for i in ${long_options//,/ }; do - if [[ ${1:2} = ${i//:} ]]; then - match=$i - break - fi - done - if [[ -n $match ]]; then - if [[ ${1:2} = $match ]]; then - printf ' %s' "$1" - else - if [[ -n $2 ]]; then - printf ' %s' "$1" - shift - printf " '%s'" "$1" - else - echo "makepkg: option '$1' $(gettext "requires an argument")" >&2 - ret=1 - fi - fi - else - echo "makepkg: $(gettext "unrecognized option") '$1'" >&2 - ret=1 - fi - else - shift - break - fi - 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 - if [[ -n ${1:$i+1} ]]; then - printf ' -%s' "${1:i:1}" - printf " '%s'" "${1:$i+1}" - else - if [[ -n $2 ]]; then - printf ' -%s' "${1:i:1}" - shift - printf " '%s'" "${1}" - else - echo "makepkg: option $(gettext "requires an argument") -- '${1:i:1}'" >&2 - ret=1 - fi - fi - break - else - printf ' -%s' "${1:i:1}" - fi - else - echo "makepkg: $(gettext "invalid option") -- '${1:i:1}'" >&2 - ret=1 - fi - done - else - unused_options="${unused_options} '$1'" - fi - shift - done - - printf " --" - if [[ -n $unused_options ]]; then - for i in ${unused_options[@]}; do - printf ' %s' "$i" - done - fi - if [[ -n $1 ]]; then - while [[ -n $1 ]]; do - printf " '%s'" "${1}" - shift - done - fi - printf "\n" - - return $ret -} +m4_include(library/parse_options.sh)
usage() { printf "makepkg (pacman) %s\n" "$myver" -- 1.7.5.4
On 11/06/11 07:18, Dan McGee wrote:
Subject typo ^^
On Fri, Jun 10, 2011 at 4:36 AM, Allan McRae<allan@archlinux.org> wrote:
This move the getopt replacement function parse_options out of makepkg.sh.in and into a separate file. The code is inserted into the relevant place in makepkg using m4.
This will allow the reuse of the option parsing code in other scripts (i.e. pacman-key) while avoiding code duplication.
Signed-off-by: Allan McRae<allan@archlinux.org> --- scripts/Makefile.am | 10 +++- scripts/library/parse_options.sh | 87 +++++++++++++++++++++++++++++++++++++ scripts/makepkg.sh.in | 88 +------------------------------------- 3 files changed, 95 insertions(+), 90 deletions(-) create mode 100644 scripts/library/parse_options.sh
diff --git a/scripts/Makefile.am b/scripts/Makefile.am index daa5b2a..74ee180 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -23,6 +23,9 @@ EXTRA_DIST = \ rankmirrors.sh.in \ repo-add.sh.in
+LIBRARY = \ + library/parse_options.sh + # Files that should be removed, but which Automake does not know. MOSTLYCLEANFILES = $(bin_SCRIPTS) *.tmp
@@ -47,6 +50,7 @@ edit = sed \ -e 's|@SIZECMD[@]|$(SIZECMD)|g' \ -e 's|@SEDINPLACE[@]|$(SEDINPLACE)|g' \ -e 's|@DUPATH[@]|$(DUPATH)|g' \ + -e 's|@SCRIPTNAME[@]|$@|g' \ -e 's|@configure_input[@]|Generated from $@.sh.in; do not edit by hand.|g'
## All the scripts depend on Makefile so that they are rebuilt when the @@ -54,11 +58,11 @@ edit = sed \ ## wrong file by accident. # two 'test' lines- make sure we can handle both sh and py type scripts # third 'test' line- make sure one of the two checks succeeded -$(OURSCRIPTS): Makefile +$(OURSCRIPTS): Makefile $(LIBRARY)
Is this overkill for the deps? I think I'd rather just see explicitly down below what needs to go on, e.g.
makepkg: $(srcdir)/makepkg.sh.in library/parse_options.sh
Hands up who was being lazy!
@echo ' ' GEN $@; @rm -f $@ $@.tmp - @test -f $(srcdir)/$@.sh.in&& $(edit) $(srcdir)/$@.sh.in>$@.tmp || true - @test -f $(srcdir)/$@.py.in&& $(edit) $(srcdir)/$@.py.in>$@.tmp || true + @test -f $(srcdir)/$@.sh.in&& m4 -P $(srcdir)/$@.sh.in | $(edit)>$@.tmp || true + @test -f $(srcdir)/$@.py.in&& m4 -P $(srcdir)/$@.py.in | $(edit)>$@.tmp || true
We don't actually have any python scripts anymore I just realized... I'll kill the cruft in another patch.
I have a patch for that but was deciding what to do with it... Maybe this should be kept in case we do get a python (or other language) script as a template for how to deal with it.
@test -f $@.tmp || false @chmod +x $@.tmp @chmod a-w $@.tmp diff --git a/scripts/library/parse_options.sh b/scripts/library/parse_options.sh new file mode 100644 index 0000000..32aa0be --- /dev/null +++ b/scripts/library/parse_options.sh @@ -0,0 +1,87 @@ +# getopt like parser +parse_options() {
maybe a local scriptname="@@SCRIPTNAME@@" once here to avoid scattering it all over the place?
OK. Not that it really matters give it is all one sed to replace it.
+ local short_options=$1; shift; + local long_options=$1; shift; + local ret=0; + local unused_options="" + local i + + while [[ -n $1 ]]; do + if [[ ${1:0:2} = '--' ]]; then + if [[ -n ${1:2} ]]; then + local match="" + for i in ${long_options//,/ }; do + if [[ ${1:2} = ${i//:} ]]; then + match=$i + break + fi + done + if [[ -n $match ]]; then + if [[ ${1:2} = $match ]]; then + printf ' %s' "$1" + else + if [[ -n $2 ]]; then + printf ' %s' "$1" + shift + printf " '%s'" "$1" + else + echo "@SCRIPTNAME@: option '$1' $(gettext "requires an argument")">&2 + ret=1 + fi + fi + else + echo "@SCRIPTNAME@: $(gettext "unrecognized option") '$1'">&2 + ret=1 + fi + else + shift + break + fi + 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 + if [[ -n ${1:$i+1} ]]; then + printf ' -%s' "${1:i:1}" + printf " '%s'" "${1:$i+1}" + else + if [[ -n $2 ]]; then + printf ' -%s' "${1:i:1}" + shift + printf " '%s'" "${1}" + else + echo "@SCRIPTNAME@: option $(gettext "requires an argument") -- '${1:i:1}'">&2 + ret=1 + fi + fi + break + else + printf ' -%s' "${1:i:1}" + fi + else + echo "@SCRIPTNAME@: $(gettext "invalid option") -- '${1:i:1}'">&2 + ret=1 + fi + done + else + unused_options="${unused_options} '$1'" + fi + shift + done + + printf " --" + if [[ -n $unused_options ]]; then + for i in ${unused_options[@]}; do + printf ' %s' "$i" + done + fi + if [[ -n $1 ]]; then + while [[ -n $1 ]]; do + printf " '%s'" "${1}" + shift + done + fi + printf "\n" + + return $ret +} \ No newline at end of file diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index b3081fc..0639fbd 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -1506,93 +1506,7 @@ canonicalize_path() { fi }
-# getopt like parser -parse_options() { - local short_options=$1; shift; - local long_options=$1; shift; - local ret=0; - local unused_options="" - local i - - while [[ -n $1 ]]; do - if [[ ${1:0:2} = '--' ]]; then - if [[ -n ${1:2} ]]; then - local match="" - for i in ${long_options//,/ }; do - if [[ ${1:2} = ${i//:} ]]; then - match=$i - break - fi - done - if [[ -n $match ]]; then - if [[ ${1:2} = $match ]]; then - printf ' %s' "$1" - else - if [[ -n $2 ]]; then - printf ' %s' "$1" - shift - printf " '%s'" "$1" - else - echo "makepkg: option '$1' $(gettext "requires an argument")">&2 - ret=1 - fi - fi - else - echo "makepkg: $(gettext "unrecognized option") '$1'">&2 - ret=1 - fi - else - shift - break - fi - 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 - if [[ -n ${1:$i+1} ]]; then - printf ' -%s' "${1:i:1}" - printf " '%s'" "${1:$i+1}" - else - if [[ -n $2 ]]; then - printf ' -%s' "${1:i:1}" - shift - printf " '%s'" "${1}" - else - echo "makepkg: option $(gettext "requires an argument") -- '${1:i:1}'">&2 - ret=1 - fi - fi - break - else - printf ' -%s' "${1:i:1}" - fi - else - echo "makepkg: $(gettext "invalid option") -- '${1:i:1}'">&2 - ret=1 - fi - done - else - unused_options="${unused_options} '$1'" - fi - shift - done - - printf " --" - if [[ -n $unused_options ]]; then - for i in ${unused_options[@]}; do - printf ' %s' "$i" - done - fi - if [[ -n $1 ]]; then - while [[ -n $1 ]]; do - printf " '%s'" "${1}" - shift - done - fi - printf "\n" - - return $ret -} +m4_include(library/parse_options.sh)
usage() { printf "makepkg (pacman) %s\n" "$myver" -- 1.7.5.4
On Sat, Jun 11, 2011 at 11:32 PM, Allan McRae <allan@archlinux.org> wrote:
On 11/06/11 07:18, Dan McGee wrote:
We don't actually have any python scripts anymore I just realized... I'll kill the cruft in another patch.
I have a patch for that but was deciding what to do with it... Maybe this should be kept in case we do get a python (or other language) script as a template for how to deal with it. If you got the patch, go ahead and throw it in your working branch whenever- I'd take the axe to it; if we need to bring it back git history is always there for us.
maybe a local scriptname="@@SCRIPTNAME@@" once here to avoid scattering it all over the place?
OK. Not that it really matters give it is all one sed to replace it. Yeah, I don't know why my gut says to do it this way- I guess just the less magic the better, and for most of these kind of replacements we try to do it this way. It also prevents silly mistakes like including @@dasfsdf@@ in a gettext-ized string.
-Dan
On 14/06/11 09:48, Dan McGee wrote:
On Sat, Jun 11, 2011 at 11:32 PM, Allan McRae<allan@archlinux.org> wrote:
On 11/06/11 07:18, Dan McGee wrote:
We don't actually have any python scripts anymore I just realized... I'll kill the cruft in another patch.
I have a patch for that but was deciding what to do with it... Maybe this should be kept in case we do get a python (or other language) script as a template for how to deal with it. If you got the patch, go ahead and throw it in your working branch whenever- I'd take the axe to it; if we need to bring it back git history is always there for us.
A bunch of stuff should be pushed there in the next few days. There are some advantages of being stuck in hospital! I'll add that patch to it.
maybe a local scriptname="@@SCRIPTNAME@@" once here to avoid scattering it all over the place?
OK. Not that it really matters give it is all one sed to replace it. Yeah, I don't know why my gut says to do it this way- I guess just the less magic the better, and for most of these kind of replacements we try to do it this way. It also prevents silly mistakes like including @@dasfsdf@@ in a gettext-ized string.
My gut is saying to go the other way. :P Anyway, I'll really have no objection either way so I will change it. Allan
participants (2)
-
Allan McRae
-
Dan McGee