Rework argument parsing to support position-independent arguments

This commit is contained in:
Salt 2020-02-20 00:17:22 -06:00
parent 56fb8bfa50
commit 3086d9a5c5

72
ptgdp
View File

@ -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