From ece4124a45468cc0fded91415b46cc2bdffe6d3b Mon Sep 17 00:00:00 2001 From: Josh Poimboeuf Date: Mon, 31 Mar 2014 16:02:50 -0500 Subject: [PATCH] kpatch-build: getopts arg parsing cleanup Cleanup the kpatch-build argument parsing a little bit: - gracefully handle no args - allow white space in filenames - use 'eval set -- $options' to allow use of $1 and $2 variables --- kpatch-build/kpatch-build | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/kpatch-build/kpatch-build b/kpatch-build/kpatch-build index f86cf2f..0f50a78 100755 --- a/kpatch-build/kpatch-build +++ b/kpatch-build/kpatch-build @@ -95,20 +95,24 @@ usage() { echo " -d, --debug Keep scratch files in /tmp" >&2 } -PARSED_OPT_ARRAY=($(getopt -u -n "$0" -o hs:c:d -l "help,sourcedir:,config:,debug" -- "$@")) || die "getopt failed" +options=$(getopt -o hs:c:d -l "help,sourcedir:,config:,debug" -- "$@") || die "getopt failed" -for index in ${!PARSED_OPT_ARRAY[*]}; do - case "${PARSED_OPT_ARRAY[$index]}" in +eval set -- "$options" + +while [[ $# -gt 0 ]]; do + case "$1" in -h|--help) usage exit 0 ;; -s|--sourcedir) - USERSRCDIR="$(readlink -f ${PARSED_OPT_ARRAY[$(( $index+1 ))]})" + USERSRCDIR=$(readlink -f "$2") + shift [[ ! -d "$USERSRCDIR" ]] && die "source dir $USERSRCDIR not found" ;; -c|--config) - CONFIGFILE="$(readlink -f ${PARSED_OPT_ARRAY[$(( $index+1 ))]})" + CONFIGFILE=$(readlink -f "$2") + shift [[ ! -f "$CONFIGFILE" ]] && die "config file $CONFIGFILE not found" ;; -d|--debug) @@ -116,18 +120,18 @@ for index in ${!PARSED_OPT_ARRAY[*]}; do DEBUG=1 ;; --) - PATCHFILE="$(readlink -f ${PARSED_OPT_ARRAY[$(( $index+1 ))]})" + if [[ -z "$2" ]]; then + usage + exit 1 + fi + PATCHFILE=$(readlink -f "$2") [[ ! -f "$PATCHFILE" ]] && die "patch file $PATCHFILE not found" break ;; esac + shift done -if [[ -z "$PATCHFILE" ]]; then - usage - exit 1 -fi - PATCHNAME="$(basename $PATCHFILE)" if [[ "$PATCHNAME" =~ \.patch ]] || [[ "$PATCHNAME" =~ \.diff ]]; then PATCHNAME="${PATCHNAME%.*}"