lib: Fix 'getargs --append' adding an empty value to the array

This commit is contained in:
Ethan P 2021-04-02 16:09:10 -07:00
parent 47152d69c3
commit 93e2e4d048
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA
2 changed files with 17 additions and 4 deletions

View File

@ -57,7 +57,9 @@ setargs() {
# getargs remaining_args
getargs() {
if [[ "$1" = "-a" || "$1" = "--append" ]]; then
eval "$2=(\"\${$2[@]}\" $(printf '%q ' "${_ARGV[@]:$_ARGV_INDEX}"))"
if [[ "${_ARGV_INDEX}" -ne "$((_ARGV_LAST+1))" ]]; then
eval "$2=(\"\${$2[@]}\" $(printf '%q ' "${_ARGV[@]:$_ARGV_INDEX}"))"
fi
else
eval "$1=($(printf '%q ' "${_ARGV[@]:$_ARGV_INDEX}"))"
fi

View File

@ -420,13 +420,24 @@ test:fn_getargs_append() {
description "Function getargs -a."
setargs "--two=three" "four"
args=(zero one)
getargs -a args
assert_equal 4 "${#args[@]}"
assert_equal "zero" "${args[0]}"
assert_equal "one" "${args[1]}"
assert_equal "--two=three" "${args[2]}"
assert_equal "four" "${args[3]}"
assert_equal "four" "${args[3]}"
# Ensure it doesn't append empty strings.
args=()
setargs "zero" "one"
getargs -a args
assert_equal 2 "${#args[@]}"
args=(zero one)
setargs
getargs -a args
assert_equal 2 "${#args[@]}"
}