imporved option parsing and GUI detection

This commit is contained in:
Tilman Kranz 2025-05-25 05:59:24 +02:00
parent a8d4304076
commit 67cbc09f43

View file

@ -44,7 +44,7 @@ log() {
if [[ -t 1 ]] && $gui && [[ -n $(type -p zenity) ]] ; then
case "$level" in
ERROR)
ERROR|WARNING)
zenity --error --text="$msg"
;;
*)
@ -58,6 +58,10 @@ error() {
log ERROR "$@"
}
warning() {
log WARNING "$@"
}
info() {
log INFO "$@"
}
@ -447,9 +451,16 @@ do_stop() {
# Arguments
operation=
no_more_options=false
for arg in "$@" ; do
case "$arg" in
--)
no_more_options=true
;;
--*)
"$no_more_options" && { gui=false error "Option arguments may not preceed non-option arguments." ; exit 1 ; }
;;&
--debug)
debug_cmdline=true
;;
@ -460,11 +471,11 @@ for arg in "$@" ; do
help_cmdline=true
;;
start|stop|restart|setup|status)
[[ -z $operation ]] || { error "Multiple operations are not supported." ; exit 1 ; }
[[ -z $operation ]] || { gui=false error "Multiple operations are not supported." ; exit 1 ; }
operation=$arg
;;
*)
error "Unsupported argument (try \"--help\")"
gui=false error "Unsupported argument (try \"--help\")"
exit 1
;;
esac
@ -489,6 +500,12 @@ fi
if [[ $gui_cmdline = false ]] ; then
gui=false
elif [[ -z $(type -p zenity) ]] ; then
gui=false
warning "Disabling GUI support, because command \"zenity\" was not found."
elif ! [[ -v DISPLAY ]] && ! [[ -v XDG_SESSION_TYPE ]] ; then
gui=false
warning "Disabling GUI support, because neither \"DISPLAY\" not \"XDG_SESSION_TYPE\ is set."
else
gui=true
fi
@ -523,7 +540,6 @@ else
fi
required_cmds=( jq pactl ssh )
"$gui" && required_cmds+=( zenity )
for exe in "${required_cmds[@]}" ; do
if [[ -z $(type -p "$exe") ]] ; then