From 3086d9a5c5e8df15367257671d677fdd06e2eee2 Mon Sep 17 00:00:00 2001 From: Salt Date: Thu, 20 Feb 2020 00:17:22 -0600 Subject: [PATCH] Rework argument parsing to support position-independent arguments --- ptgdp | 72 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/ptgdp b/ptgdp index 1ebbfbd..7b00011 100755 --- a/ptgdp +++ b/ptgdp @@ -320,34 +320,45 @@ playlist() { # Main main() { - # Getopts before anything else - while getopts ":c:dhmpv" opt; do - case $opt in - c) - _optconfigfile="$OPTARG" - ;; - d) - _optdlonly=1 - ;; - h) - _opthelp=1 - ;; - m) - _optmachinemode=1 - ;; - p) - _optautoplay=1 - ;; - v) - _optverbose+=1 - ;; - :) - error "Option requires argument: -$OPTARG" 2 - ;; - *) - error "Invalid option: -$OPTARG" 2 - ;; - esac + # Parse out arguments + # Done in a nested loop so that flags are position-independent + while [ -n "$1" ]; do + # Parse out flags + while getopts ":c:dhmpv" opt; do + case $opt in + c) + _optconfigfile="$OPTARG" + ;; + d) + _optdlonly=1 + ;; + h) + _opthelp=1 + ;; + m) + _optmachinemode=1 + ;; + p) + _optautoplay=1 + ;; + v) + _optverbose+=1 + ;; + :) + error "Option requires argument: -$OPTARG" 2 + ;; + *) + error "Invalid option: -$OPTARG" 2 + ;; + esac + done + # Store arguments + shift $((OPTIND - 1)) + if [ -n "$1" ]; then + _args+=("$1") + shift + fi + unset OPTIND done # Early hook for help [ -n "$_opthelp" ] && printhelp && exit 0 @@ -366,11 +377,6 @@ main() { else warn "Could not find configuration file" 2 fi - # Store arguments - shift $((OPTIND - 1)) - for arg in "$@"; do - _args+=("$arg") - done # Validate critical options if [ -z "$_optmachinemode" ]; then # Ensure we have a good backend, assuming we're not in a scripting mode