diff options
author | Helmut Grohne <helmut@subdivi.de> | 2022-12-27 13:05:00 +0100 |
---|---|---|
committer | Helmut Grohne <helmut@subdivi.de> | 2022-12-27 13:05:00 +0100 |
commit | c42caac69b5d32c6330716a829d33086b04f2b76 (patch) | |
tree | 868df92df0e44a8ee4b89fc7a562ecc8e7443181 /debvm-run | |
parent | b44e90f2139b97ac92bd69123679d04d16c9bd15 (diff) | |
download | debvm-c42caac69b5d32c6330716a829d33086b04f2b76.tar.gz |
support long options
This change extends option parsing of the debvm tools:
* Uses posix getopts. No additional dependencies.
* Long option are supported. The option value may be a separate
argument or separated with =.
* Option value may follow short option immediately, e.g. -as390x
* Multiple short options may be combined, e.g. -gs 2222
Technical limitation:
* Short options are parsed inside a double dash, e.g.
-g-image=rootfs.ext4
Diffstat (limited to 'debvm-run')
-rwxr-xr-x | debvm-run | 66 |
1 files changed, 47 insertions, 19 deletions
@@ -8,40 +8,68 @@ IMAGE=rootfs.ext2 SSHPORT= GRAPHICAL= +nth_arg() { + shift "$1" + printf "%s" "$1" +} + die() { echo "$*" 1>&2 exit 1 } - usage() { die "usage: $0 [-g] [-i image] [-s sshport] [-- qemu options]" } +usage_error() { + echo "error: $*" 1>&2 + usage +} -while test "$#" -gt 0; do - case "$1" in - -g) - GRAPHICAL=1 - shift - ;; - -i) - test "$#" -eq 1 && usage - IMAGE=$2 - shift 2 +opt_graphical() { + GRAPHICAL=1 +} +opt_image() { + IMAGE=$1 +} +opt_sshport() { + SSHPORT=$1 +} + +while getopts :gi:s:-: OPTCHAR; do + case "$OPTCHAR" in + g) opt_graphical ;; + i) opt_image "$OPTARG" ;; + s) opt_sshport "$OPTARG" ;; + -) + case "$OPTARG" in + help) + usage + ;; + graphical|image|sshport) + test "$OPTIND" -gt "$#" && usage_error "missing argument for --$OPTARG" + "opt_$OPTARG" "$(nth_arg "$OPTIND" "$@")" + OPTIND=$((OPTIND+1)) + ;; + image=*|sshport=*) + "opt_${OPTARG%%=*}" "${OPTARG#*=}" + ;; + *) + usage_error "unrecognized option --$OPTARG" + ;; + esac ;; - -s) - test "$#" -eq 1 && usage - SSHPORT=$2 - shift 2 + :) + usage_error "missing argument for -$OPTARG" ;; - --) - shift - break + '?') + usage_erro "unrecognized option -$OPTARG" ;; *) - usage + die "internal error while parsing command options, please report a bug" ;; esac done +shift "$((OPTIND - 1))" test -f "$IMAGE" || die "image '$IMAGE' not found" test -s "$IMAGE" || die "image '$IMAGE' is empty" |