Switch to parseopts instead of merely checking the first argument.

Signed-off-by: Gordian Edenhofer <gordian.edenho...@gmail.com>
---
Remove the leading colon in the OPT_SHORT variable.
Thanks Allen for the hint.

 contrib/bacman.sh.in | 63 ++++++++++++++++++++++++++++++++++------------------
 1 file changed, 41 insertions(+), 22 deletions(-)

diff --git a/contrib/bacman.sh.in b/contrib/bacman.sh.in
index a718a18..51283f3 100644
--- a/contrib/bacman.sh.in
+++ b/contrib/bacman.sh.in
@@ -32,6 +32,7 @@ INCLUDE_PACNEW='n'
 ARGS=("$@")
 
 m4_include(../scripts/library/output_format.sh)
+m4_include(../scripts/library/parseopts.sh)
 
 # Lazy recursive clean up of temporary dirs
 work_dir_root="${TMPDIR:-/tmp}/bacman"
@@ -62,35 +63,51 @@ version() {
        echo 'Copyright (C) 2008-2016 Pacman Development Team 
<pacman-dev@archlinux.org>'
 }
 
-# Check for specified arguments
-while [[ ! -z $1 ]]; do
-       if [[ $1 == "--nocolor" ]]; then
-               USE_COLOR='n'
-               shift
-       elif [[ $1 == "--pacnew" ]]; then
-               INCLUDE_PACNEW='y'
-               shift
-       else
-               break
-       fi
-done
-
 # Configure colored output
 m4_include(../scripts/library/term_colors.sh)
 
 # Break if no argument was given
-if (( $# < 1 )); then
+if (( $# == 0 )); then
+       usage
+       exit 1
+fi
+
+# Printing the usage information takes precedence over every other parameter
+for option in "$@"; do
+       [[ $option == "-h" || $option == "--help" ]] && usage && exit 0
+done
+
+# Parse arguments
+OPT_SHORT='v'
+OPT_LONG=('nocolor' 'pacnew' 'version')
+if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
        usage
        exit 1
 fi
+set -- "${OPTRET[@]}"
+unset OPT_SHORT OPT_LONG OPTRET
+
+while :; do
+       case "$1" in
+               --nocolor)
+                       USE_COLOR='n' ;;
+               --pacnew)
+                       INCLUDE_PACNEW='y' ;;
+               -v|--version)
+                       version
+                       exit 0 ;;
+               --)
+                       shift
+                       break 2 ;;
+       esac
+       shift
+done
 
-# Print usage or version if requested
-if [[ $1 = -@(h|-help) ]]; then
+# Retrieve the list of packages to be assembled and break if none was specified
+pkg_list=($*)
+if [[ ${#pkg_list[@]} == 0 ]]; then
        usage
-       exit 0
-elif [[ $1 = -@(V|-version) ]]; then
-       version
-       exit 0
+       exit 1
 fi
 
 # Run with fake root privileges if EUID is not root
@@ -366,9 +383,11 @@ parallelize() {
 
 # Initiate assembly function
 if [[ $MAX_JOBS -gt "1" ]]; then
-       parallelize "$@"
+       parallelize "${pkg_list[@]}"
 else
-       for PKG in $@; do fakebuild $PKG; done
+       for PKG in ${pkg_list[@]}; do
+               fakebuild $PKG
+       done
 fi
 msg "Done."
 
-- 
2.9.3

Reply via email to