improved argument parsing

This commit is contained in:
Daniel Sommer 2021-04-08 17:09:44 +02:00
parent 5231d683b0
commit 37aba9f6a5

View file

@ -3,6 +3,8 @@
# author: Daniel Sommer <daniel.sommer@velvettear.de> # author: Daniel Sommer <daniel.sommer@velvettear.de>
# license: MIT # license: MIT
# default_
# blinky # blinky
blinky_url="127.0.0.1:3000" blinky_url="127.0.0.1:3000"
blinky_mode="morph" blinky_mode="morph"
@ -17,11 +19,10 @@ thermal_zones=(
"3" "3"
) )
# argument to lowercase #############
arg="${1,,}" # functions #
#############
# functions # check for root permissions
function checkRoot() { function checkRoot() {
[[ "$EUID" != 0 ]] && printf "error: permission denied!\n" && exit 1 [[ "$EUID" != 0 ]] && printf "error: permission denied!\n" && exit 1
} }
@ -74,8 +75,36 @@ function getTemperature() {
[[ "$result" -ge "$max_temp" ]] && printf "warning: maximum temperature reached\n" && blinky_mode="pulse" && blinky_duration="1500" && return 0 [[ "$result" -ge "$max_temp" ]] && printf "warning: maximum temperature reached\n" && blinky_mode="pulse" && blinky_duration="1500" && return 0
} }
# main part
case "$arg" in #############
# main part #
#############
argument_count="$#"
index=1
[[ "$argument_count" -lt 1 ]] && printf "error: missing arguments\n" && exit 1
url="${@: $argument_count: 1}"
while [[ "$index" -lt $(( $argument_count -1 )) ]]; do
argument="${@: $index: 1}"
argument="${argument,,}"
(( index++ ))
case "$argument" in
-bm|--blinky-mode)
blinky_mode="${@: $index: 1}"
;;
-bd|--blinky-duration)
blinky_duration="${@: $index: 1}"
;;
-m|--mode)
mode="${@: $index: 1}"
;;
esac
done
printf "checking mode '$mode'...\n"
case "$mode" in
-t|--temp|--temperature) -t|--temp|--temperature)
checkRoot checkRoot
getTemperature getTemperature
@ -83,6 +112,6 @@ case "$arg" in
sendPOST "color=$result" "mode=$blinky_mode" "duration=$blinky_duration" sendPOST "color=$result" "mode=$blinky_mode" "duration=$blinky_duration"
;; ;;
*) *)
printf "error: no argument given or argument unknown!\n" && exit 1 sendPOST "color=random" "mode=$blinky_mode" "duration=$blinky_duration"
;; ;;
esac esac