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
This commit is contained in:
Josh Poimboeuf 2014-03-31 16:02:50 -05:00
parent 47b0b2a45c
commit ece4124a45

View File

@ -95,20 +95,24 @@ usage() {
echo " -d, --debug Keep scratch files in /tmp" >&2 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 eval set -- "$options"
case "${PARSED_OPT_ARRAY[$index]}" in
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help) -h|--help)
usage usage
exit 0 exit 0
;; ;;
-s|--sourcedir) -s|--sourcedir)
USERSRCDIR="$(readlink -f ${PARSED_OPT_ARRAY[$(( $index+1 ))]})" USERSRCDIR=$(readlink -f "$2")
shift
[[ ! -d "$USERSRCDIR" ]] && die "source dir $USERSRCDIR not found" [[ ! -d "$USERSRCDIR" ]] && die "source dir $USERSRCDIR not found"
;; ;;
-c|--config) -c|--config)
CONFIGFILE="$(readlink -f ${PARSED_OPT_ARRAY[$(( $index+1 ))]})" CONFIGFILE=$(readlink -f "$2")
shift
[[ ! -f "$CONFIGFILE" ]] && die "config file $CONFIGFILE not found" [[ ! -f "$CONFIGFILE" ]] && die "config file $CONFIGFILE not found"
;; ;;
-d|--debug) -d|--debug)
@ -116,18 +120,18 @@ for index in ${!PARSED_OPT_ARRAY[*]}; do
DEBUG=1 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" [[ ! -f "$PATCHFILE" ]] && die "patch file $PATCHFILE not found"
break break
;; ;;
esac esac
shift
done done
if [[ -z "$PATCHFILE" ]]; then
usage
exit 1
fi
PATCHNAME="$(basename $PATCHFILE)" PATCHNAME="$(basename $PATCHFILE)"
if [[ "$PATCHNAME" =~ \.patch ]] || [[ "$PATCHNAME" =~ \.diff ]]; then if [[ "$PATCHNAME" =~ \.patch ]] || [[ "$PATCHNAME" =~ \.diff ]]; then
PATCHNAME="${PATCHNAME%.*}" PATCHNAME="${PATCHNAME%.*}"