Fix lib/opt.sh not working on Bash 5 (#2)

This commit is contained in:
Ethan P 2019-09-08 11:16:00 -07:00
parent 85bb5f8bed
commit 5749d9ea2d
No known key found for this signature in database
GPG Key ID: 1F8DF8091CD46FBC

View File

@ -7,6 +7,13 @@
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
PROGRAM="$(basename "${BASH_SOURCE[0]}")" PROGRAM="$(basename "${BASH_SOURCE[0]}")"
# Sets the internal _ARGV and _ARGV_INDEX variables used when
# parsing options with the shiftopt and shiftval functions.
setargs() {
_ARGV=("$@")
_ARGV_INDEX="$((${#_ARGV[@]} - 1))"
}
# Gets the next option passed to the script. # Gets the next option passed to the script.
# #
# Variables: # Variables:
@ -22,12 +29,6 @@ PROGRAM="$(basename "${BASH_SOURCE[0]}")"
# echo "$OPT = $OPT_VAL" # echo "$OPT = $OPT_VAL"
# done # done
shiftopt() { shiftopt() {
# Ensure _ARGV exists and has the program arguments.
if [[ -z ${_ARGV+x} ]]; then
_ARGV=("${BASH_ARGV[@]}")
_ARGV_INDEX="$((${#_ARGV[@]} - 1))"
fi
# Read the top of _ARGV. # Read the top of _ARGV.
[[ "$_ARGV_INDEX" -lt 0 ]] && return 1 [[ "$_ARGV_INDEX" -lt 0 ]] && return 1
OPT="${_ARGV[$_ARGV_INDEX]}" OPT="${_ARGV[$_ARGV_INDEX]}"
@ -67,3 +68,7 @@ shiftval() {
fi fi
} }
# -----------------------------------------------------------------------------
setargs "$@"