From 5749d9ea2d6bcabec5b3f529b0df9c1382026c06 Mon Sep 17 00:00:00 2001 From: Ethan P Date: Sun, 8 Sep 2019 11:16:00 -0700 Subject: [PATCH] Fix lib/opt.sh not working on Bash 5 (#2) --- lib/opt.sh | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/opt.sh b/lib/opt.sh index 3ccc19d..4ea8755 100644 --- a/lib/opt.sh +++ b/lib/opt.sh @@ -7,6 +7,13 @@ # ----------------------------------------------------------------------------- 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. # # Variables: @@ -22,12 +29,6 @@ PROGRAM="$(basename "${BASH_SOURCE[0]}")" # echo "$OPT = $OPT_VAL" # done 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. [[ "$_ARGV_INDEX" -lt 0 ]] && return 1 OPT="${_ARGV[$_ARGV_INDEX]}" @@ -67,3 +68,7 @@ shiftval() { fi } + +# ----------------------------------------------------------------------------- +setargs "$@" +