On 11/06/11 07:18, Dan McGee wrote:
Subject typo ^^

On Fri, Jun 10, 2011 at 4:36 AM, Allan McRae<[email protected]>  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<[email protected]>
---
  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 [email protected]; 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 $@ [email protected]
-       @test -f $(srcdir)/[email protected]&&  $(edit) $(srcdir)/[email protected]>[email protected] || true
-       @test -f $(srcdir)/[email protected]&&  $(edit) $(srcdir)/[email protected]>[email protected] || true
+       @test -f $(srcdir)/[email protected]&&  m4 -P $(srcdir)/[email protected] | 
$(edit)>[email protected] || true
+       @test -f $(srcdir)/[email protected]&&  m4 -P $(srcdir)/[email protected] | 
$(edit)>[email protected] || 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 [email protected] || false
        @chmod +x [email protected]
        @chmod a-w [email protected]
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



Reply via email to