Experimental new code using getopt, it seems to work better, but who knows lol.
This commit is contained in:
parent
da6a960bcd
commit
cc1dab3fdb
57
ratatoskr.sh
57
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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user