From e2eeccd34fdc1f0c7ce03c8c4a1fc7a238e3d5fa Mon Sep 17 00:00:00 2001 From: Shy Date: Wed, 7 Aug 2024 18:19:19 +0200 Subject: [PATCH] Reworked option parsing. --- wled | 78 +++++++++++++++++++++++++++++------------------------------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/wled b/wled index c6e28d4..00b1fe8 100755 --- a/wled +++ b/wled @@ -10,6 +10,9 @@ NAME="wled.sh" COMMAND="wled" VERSION="0.1.3" +exit_code=0 +host_list= + # Make the exit status of command pipelines the status of the last command # with non-zero exit status - if supported by this shell. (set -o pipefail 2>/dev/null) && set -o pipefail @@ -67,44 +70,39 @@ current_brightness() { sed -n 's/.*"state"[[:space:]]*:[[:space:]]*{[^}]*"bri"[[:space:]]*:[[:space:]]*\([0-9]\{1,3\}\).*/\1/p' } -arg_inst="$1" -arg_cmnd="$2" -arg_param="$3" -exit_code=0 - -# Clear positional arguments. -set -- - -# Parse instance argument and store hostnames as positional arguments. -case "$arg_inst" in - ""|-h|--help) # Missing instance parameter. - print_help - exit 0 - ;; - -v|--version) - echo "$NAME version $VERSION" - exit 0 - ;; - -a|--all) - set -- 'wled-fnordcenter.local' \ - 'wled-wohnzimmer.local' - ;; - -f|--fnordcenter) - set -- 'wled-fnordcenter.local' - ;; - -w|--wohnzimmer) - set -- 'wled-wohnzimmer.local' - ;; - *) # Unknown instance parameter. - echo "Error: unknown instance \"$arg_inst\"." >&2 - print_usage - exit 1 - ;; -esac +# Parse instance argument and store hostnames. +while test $# -gt 0; do + case "$1" in + ""|-h|--help) # Missing instance parameter. + print_help + exit 0 + ;; + -v|--version) + echo "$NAME version $VERSION" + exit 0 + ;; + -a|--all) + host_list="wled-fnordcenter.local wled-wohnzimmer.local" + ;; + -f|--fnordcenter) + host_list="$host_list wled-fnordcenter.local" + ;; + -w|--wohnzimmer) + host_list="$host_list wled-wohnzimmer.local" + ;; + *) # Unknown instance parameter. + test -n "$host_list" && break + echo "Error: not a valid instance option (\"$1\")." >&2 + print_usage + exit 1 + ;; + esac + shift +done # Parse and execute command argument against given instances. -for wled_host; do - case "$arg_cmnd" in +for wled_host in $host_list; do + case "$1" in on) # Switch on. api_resp=$(curl_send '{"on":true}' "http://$wled_host/json") ;; @@ -112,13 +110,13 @@ for wled_host; do api_resp=$(curl_send '{"on":false}' "http://$wled_host/json") ;; -b|--brightness) # Set brightness. - if test -z "$arg_param"; then + if test -z "$2"; then # No parameter given. Print current brightness. echo "Brightness of $wled_host:" current_brightness "$wled_host" else # Try to convert 3rd parameter to an integer between 1 an 255. - bright=$(printf '%u' "$arg_param" 2>/dev/null) + bright=$(printf '%u' "$2" 2>/dev/null) if test $? -gt 0; then # Conversion failed. echo "Error: unable to parse parameter for brightness." >&2 exit 1 @@ -130,7 +128,7 @@ for wled_host; do fi ;; [1-9]|1[0-7]) # Switch to preset. - api_resp=$(curl_send "{'ps':$arg_cmnd}" "http://$wled_host/json") + api_resp=$(curl_send "{'ps':$1}" "http://$wled_host/json") ;; l|list) # List presets. printf "loading ...\r" @@ -164,7 +162,7 @@ for wled_host; do exit 1 ;; *) # Unknown command. - echo "Error: unknown command \"$arg_cmnd\"." >&2 + echo "Error: unknown command \"$1\"." >&2 print_usage exit 1 ;;