makepkg: move option parsing code to separate 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>
Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Allan McRae 2011-06-08 23:36:46 +10:00 committed by Dan McGee
parent 5f404f2cb7
commit 6f3a2145b0
3 changed files with 97 additions and 91 deletions

View file

@ -21,7 +21,11 @@ EXTRA_DIST = \
pacman-optimize.sh.in \
pkgdelta.sh.in \
rankmirrors.sh.in \
repo-add.sh.in
repo-add.sh.in \
$(LIBRARY)
LIBRARY = \
library/parse_options.sh
# Files that should be removed, but which Automake does not know.
MOSTLYCLEANFILES = $(bin_SCRIPTS) *.tmp
@ -47,6 +51,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
@ -57,14 +62,14 @@ edit = sed \
$(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 $(srcdir)/$@.sh.in && m4 -P -I $(srcdir) $(srcdir)/$@.sh.in | $(edit) >$@.tmp || true
@test -f $(srcdir)/$@.py.in && m4 -P -I $(srcdir) $(srcdir)/$@.py.in | $(edit) >$@.tmp || true
@test -f $@.tmp || false
@chmod +x $@.tmp
@chmod a-w $@.tmp
@mv $@.tmp $@
makepkg: $(srcdir)/makepkg.sh.in
makepkg: $(srcdir)/makepkg.sh.in $(srcdir)/library/parse_options.sh
pacman-db-upgrade: $(srcdir)/pacman-db-upgrade.sh.in
pacman-key: $(srcdir)/pacman-key.sh.in
pacman-optimize: $(srcdir)/pacman-optimize.sh.in

View file

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

View file

@ -1589,93 +1589,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"