Use getopt for argument parsing

This commit is contained in:
deadcxap
2025-08-25 11:01:21 +03:00
parent 1768a58344
commit 860babcedc
+15 -2
View File
@@ -103,8 +103,17 @@ ROLE=""
NETBIRD_KEY="" NETBIRD_KEY=""
NETBIRD_IP="" NETBIRD_IP=""
NETBIRD_PORT="" NETBIRD_PORT=""
parse_args() {
while [[ $# -gt 0 ]]; do local opts
if ! opts=$(getopt \
-o u:k:s:m:v:r:n:i:p:h \
--long user:,sshkey:,ssh-ip:,monitor-ip:,vector:,role:,netbird-key:,netbird-ip:,netbird-port:,help \
-n "$0" -- "$@"); then
usage
exit 1
fi
eval set -- "$opts"
while true; do
case "$1" in case "$1" in
-u|--user) USERNAME="$2"; shift 2 ;; -u|--user) USERNAME="$2"; shift 2 ;;
-k|--sshkey) SSH_KEY="$2"; shift 2 ;; -k|--sshkey) SSH_KEY="$2"; shift 2 ;;
@@ -116,9 +125,13 @@ while [[ $# -gt 0 ]]; do
-i|--netbird-ip) NETBIRD_IP="$2"; shift 2 ;; -i|--netbird-ip) NETBIRD_IP="$2"; shift 2 ;;
-p|--netbird-port) NETBIRD_PORT="$2"; shift 2 ;; -p|--netbird-port) NETBIRD_PORT="$2"; shift 2 ;;
-h|--help) usage; exit 0 ;; -h|--help) usage; exit 0 ;;
--) shift; break ;;
*) log "Unknown parameter: $1"; usage; exit 1 ;; *) log "Unknown parameter: $1"; usage; exit 1 ;;
esac esac
done done
}
parse_args "$@"
if [[ -z "$USERNAME" || -z "$SSH_KEY" ]]; then if [[ -z "$USERNAME" || -z "$SSH_KEY" ]]; then
usage usage