summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHelmut Grohne <helmutg@debian.org>2022-12-28 08:43:22 +0000
committerHelmut Grohne <helmutg@debian.org>2022-12-28 08:43:22 +0000
commit98e14b14b6b6ffbf6f5173790808745a38b1d616 (patch)
tree733d2d4b8638ac94a35629c9e1755a3ee28fe934
parentd43e76f6f4c3f2e6ce196c5544b7ff5653c024c8 (diff)
parentc42caac69b5d32c6330716a829d33086b04f2b76 (diff)
downloaddebvm-98e14b14b6b6ffbf6f5173790808745a38b1d616.tar.gz
Merge branch 'longoptions' into 'main'
support long options See merge request helmutg/debvm!13
-rwxr-xr-xdebvm-create111
-rwxr-xr-xdebvm-run66
2 files changed, 113 insertions, 64 deletions
diff --git a/debvm-create b/debvm-create
index c4159e1..2362beb 100755
--- a/debvm-create
+++ b/debvm-create
@@ -15,67 +15,88 @@ SSHKEY=
SUITE=unstable
VMNAME=testvm
+nth_arg() {
+ shift "$1"
+ printf "%s" "$1"
+}
+
die() {
echo "$*" 1>&2
exit 1
}
-
usage() {
die "usage: $0 [-a architecture] [-h hostname] [-k sshkey] [-m mirror] [-o output] [-p packages] [-r release] [-s size_in_GB] [-- mmdebstrap options]"
}
+usage_error() {
+ echo "error: $*" 1>&2
+ usage
+}
+opt_architecture() {
+ ARCHITECTURE=$1
+}
+opt_hostname() {
+ VMNAME=$1
+}
+opt_mirror() {
+ MIRROR=$1
+}
+opt_sshkey() {
+ SSHKEY=$1
+}
+opt_output() {
+ IMAGE=$1
+}
+opt_package() {
+ INCLUDE_PACKAGES="$INCLUDE_PACKAGES,$1"
+}
+opt_release() {
+ SUITE=$1
+}
+opt_size() {
+ SIZE=$(($1*1024*1024*1024))
+}
-while test "$#" -gt 0; do
- case "$1" in
- -a)
- test "$#" -eq 1 && usage
- ARCHITECTURE=$2
- shift 2
- ;;
- -h)
- test "$#" -eq 1 && usage
- VMNAME=$2
- shift 2
- ;;
- -k)
- test "$#" -eq 1 && usage
- SSHKEY=$2
- shift 2
- ;;
- -m)
- test "$#" -eq 1 && usage
- MIRROR=$2
- shift 2
- ;;
- -o)
- test "$#" -eq 1 && usage
- IMAGE=$2
- shift 2
- ;;
- -p)
- test "$#" -eq 1 && usage
- INCLUDE_PACKAGES="$INCLUDE_PACKAGES,$2"
- shift 2
- ;;
- -r)
- test "$#" -eq 1 && usage
- SUITE=$2
- shift 2
+while getopts :a:h:k:m:o:p:r:s:-: OPTCHAR; do
+ case "$OPTCHAR" in
+ a) opt_architecture "$OPTARG" ;;
+ h) opt_hostname "$OPTARG" ;;
+ k) opt_sshkey "$OPTARG" ;;
+ m) opt_mirror "$OPTARG" ;;
+ o) opt_output "$OPTARG" ;;
+ p) opt_package "$OPTARG" ;;
+ r) opt_release "$OPTARG" ;;
+ s) opt_size "$OPTARG" ;;
+ -)
+ case "$OPTARG" in
+ help)
+ usage
+ ;;
+ architecture|hostname|mirror|output|package|release|size|sshkey)
+ test "$OPTIND" -gt "$#" && usage_error "missing argument for --$OPTARG"
+ "opt_$OPTARG" "$(nth_arg "$OPTIND" "$@")"
+ OPTIND=$((OPTIND+1))
+ ;;
+ architecture=*|hostname=*|mirror=*|output=*|package=*|release=*|size=*|sshkey=*)
+ "opt_${OPTARG%%=*}" "${OPTARG#*=}"
+ ;;
+ *)
+ usage_error "unrecognized option --$OPTARG"
+ ;;
+ esac
;;
- -s)
- test "$#" -eq 1 && usage
- SIZE=$(($2*1024*1024*1024))
- shift 2
+ :)
+ usage_error "missing argument for -$OPTARG"
;;
- --)
- shift
- break
+ '?')
+ usage_error "unrecognized option -$OPTARG"
;;
*)
- usage
+ die "internal error while parsing command options, please report a bug"
;;
esac
done
+shift "$((OPTIND - 1))"
if test -n "$SSHKEY" && ! test -f "$SSHKEY"; then
die "error: ssh keyfile '$SSHKEY' not found"
diff --git a/debvm-run b/debvm-run
index 972eac6..c800215 100755
--- a/debvm-run
+++ b/debvm-run
@@ -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"