From cc1dab3fdbb571fb976704932e99057a0c778799 Mon Sep 17 00:00:00 2001 From: stormdragon2976 Date: Sat, 4 Feb 2023 00:05:31 -0500 Subject: [PATCH] Experimental new code using getopt, it seems to work better, but who knows lol. --- ratatoskr.sh | 57 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/ratatoskr.sh b/ratatoskr.sh index 8140487..443cba6 100755 --- a/ratatoskr.sh +++ b/ratatoskr.sh @@ -256,16 +256,6 @@ done # make sure the configuration and soundpack paths exist: mkdir -p "${configPath}/soundpacks" -# Keep track of the backgrounded loop - -# Associative array of command line parameters and short description of what they do. -declare -A command=( - [C]="Recreate default configuration file. Acquire new oauth token." - [h]="Help, show usage information for ${0##*/}." - [M]="Post the currently playing music track, requires playerctl." - [p]="Post from the command line, e.g. ${0##*/} -p \"hello world\"" - [S]="Scrobble the currently playing music track, requires playerctl." -) # if the default file doesn't exist, create it if [[ ! -e "${configPath}/${configFile}" ]]; then @@ -275,23 +265,48 @@ else source "${configPath}/${configFile}" fi +# Associative array of command line parameters and short description of what they do. +declare -A command=( + [C]="Recreate default configuration file. Acquire new oauth token." + [h]="Help, show usage information for ${0##*/}." + [M]="Post the currently playing music track, requires playerctl." + [p::]="Post from the command line, e.g. ${0##*/} -p \"hello world\"" + [S]="Scrobble the currently playing music track, requires playerctl." +) + # Handle command line parameters -# Convert the keys of the associative array to a format usable by getopts -args="${!command[*]}" -args="${args//[[:space:]]/}" -while getopts "${args}" i ; do - case "$i" in - C) get_oauth_token;; - h) help;; - M) +# Convert the keys of the associative array to a format usable by getopt +shortOptions="${!command[*]}" +shortOptions="${shortOptions//[[:space:]]/}" + +options="$(getopt -o "$shortOptions" -- "$@")" + +while true; do + case $1 in + -C) + get_oauth_token + shift;; + -h) + help + exit 0;; + -M) post_music exit 0;; - p) - post_status "${OPTARG:-$(cat)}" + -p) + if [ -z "$2" ]; then + post_status "$(cat)" + else + shift + post_status "$@" + fi exit 0;; - S) + -S|--scrobble-music) scrobble_music exit 0;; + *) + # unexpected option + help + exit 1;; esac done