On Wed, Jun 08, 2011 at 11:40:41PM +1000, Allan McRae 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> ---
Is there a better way to do this that I am missing?
I'm no m4 ninja, but this all looks good. The only alternative I can think of is distributing the library as a separate entity. That, however, doesn't seem sane given the minimal space savings for added complexity. Ack from me. d
scripts/Makefile.am | 10 +++-- scripts/library/parse_options.sh | 87 +++++++++++++++++++++++++++++++++++++ scripts/makepkg.sh.in | 88 +------------------------------------- 3 files changed, 94 insertions(+), 91 deletions(-) create mode 100644 scripts/library/parse_options.sh
diff --git a/scripts/Makefile.am b/scripts/Makefile.am index fd9b20d..6335ba1 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -56,10 +56,12 @@ edit = sed \ # third 'test' line- make sure one of the two checks succeeded $(OURSCRIPTS): Makefile @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 $@.tmp || false + @rm -f $@ $@.m4 $@.tmp + @test -f $(srcdir)/$@.sh.in && $(edit) $(srcdir)/$@.sh.in >$@.m4 || true + @test -f $(srcdir)/$@.py.in && $(edit) $(srcdir)/$@.py.in >$@.m4 || true + @test -f $@.m4 || false + @m4 -P $@.m4 >$@.tmp + @rm -f $@.m4 @chmod +x $@.tmp @chmod a-w $@.tmp @mv $@.tmp $@ diff --git a/scripts/library/parse_options.sh b/scripts/library/parse_options.sh new file mode 100644 index 0000000..878cff1 --- /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 "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 +} \ 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