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
1 changed files with 15 additions and 11 deletions

View File

@ -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%.*}"