lib: Add 'getargs' to 'lib_opt'

This commit is contained in:
Ethan P 2021-04-02 15:38:26 -07:00
parent 87525fca91
commit 9efc5362df
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA
2 changed files with 29 additions and 0 deletions

View File

@ -47,6 +47,17 @@ setargs() {
_ARGV_SUBINDEX=1
}
# Gets all the remaining unparsed arguments and saves them to a variable.
#
# Arguments:
# $1 -- The variable to save the args to.
#
# Example:
# getargs remaining_args
getargs() {
eval "$1=($(printf '%q ' "${_ARGV[@]:$_ARGV_INDEX}"))"
}
# Resets the internal _ARGV* variables to the original script arguments.
# This is the equivalent of storing the top-level $@ and using setargs with it.
resetargs() {

View File

@ -397,3 +397,21 @@ test:fn_resetargs() {
assert_opt_name "--long-implicit"
}
# shellcheck disable=SC2154
test:fn_getargs() {
description "Function getargs."
setargs "--one=two" "three" "--four"
shiftopt
getargs args
assert_equal 2 "${#args[@]}"
assert_equal "three" "${args[0]}"
assert_equal "--four" "${args[1]}"
# Ensure getargs doesn't remove remaining arguments.
shiftopt
assert_opt_name "three"
}