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
1 changed files with 39 additions and 33 deletions

18
ptgdp
View File

@ -320,7 +320,10 @@ playlist() {
# Main
main() {
# Getopts before anything else
# 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)
@ -349,6 +352,14 @@ main() {
;;
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
# Parse out a config file if it exists
@ -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