From 5c549b0e1df1ff13a94fbf3849391c1b7e2ac7fb Mon Sep 17 00:00:00 2001 From: Gordian Edenhofer Date: Sun, 4 Sep 2016 18:13:59 +0200 Subject: bacman: proper option handling Switch to parseopts instead of merely checking the first argument. Signed-off-by: Gordian Edenhofer Signed-off-by: Allan McRae --- contrib/bacman.sh.in | 57 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/contrib/bacman.sh.in b/contrib/bacman.sh.in index e7a7f576..47439031 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,32 +63,46 @@ version() { echo 'Copyright (C) 2008-2016 Pacman Development Team ' } -while [[ ! -z $1 ]]; do - if [[ $1 == "--nocolor" ]]; then - USE_COLOR='n' - shift - elif [[ $1 == "--pacnew" ]]; then - INCLUDE_PACNEW='y' - shift - else - break - fi -done -m4_include(../scripts/library/term_colors.sh) +# Printing the usage information takes precedence over every other parameter +for option in "$@"; do + [[ $option == "-h" || $option == "--help" ]] && usage && exit 0 +done -# Break if no argument was given -if (( $# < 1 )); then +# Parse arguments +OPT_SHORT='mv' +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 + -m|--nocolor) + USE_COLOR='n' ;; + --pacnew) + INCLUDE_PACNEW='y' ;; + -v|--version) + version + exit 0 ;; + --) + shift + break 2 ;; + esac + shift +done + +# Configure colored output +m4_include(../scripts/library/term_colors.sh) -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 # @@ -355,7 +370,9 @@ fakebuild() { } -for PKG in $@; do fakebuild $PKG; done +for PKG in ${pkg_list[@]}; do + fakebuild $PKG +done msg "Done." exit 0 -- cgit v1.2.3-24-g4f1b