-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for nested function arguments. (#31)
This PR reworks the default argument logic allowing it to collect arguments just for the current function, using BASH_ARGC as a boundary. It’s designed to make things more reliable in nested contexts, allowing the user to work only with the arguments relevant to the current function call. --------- Co-authored-by: Will Allan <billyzkid@yahoo.com>
- Loading branch information
Showing
11 changed files
with
600 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env bats | ||
|
||
load ../test_helper | ||
export GETOPTS_LONG_TEST_BIN='getopts_long-with_extdebug' | ||
|
||
@test "${FEATURE}: toggles, silent" { | ||
compare '-t -t -- user_arg' \ | ||
'toggles' | ||
} | ||
@test "${FEATURE}: toggles, verbose" { | ||
compare '-t -t -- user_arg' \ | ||
'toggles' | ||
} | ||
|
||
@test "${FEATURE}: options, silent" { | ||
compare '-o user_val1 -o user_val2 -- user_arg' \ | ||
'options' | ||
} | ||
@test "${FEATURE}: options, verbose" { | ||
compare '-o user_val1 -o user_val2 -- user_arg' \ | ||
'options' | ||
} | ||
|
||
@test "${FEATURE}: variables, silent" { | ||
compare '-vuser_val1 -vuser_val2 -- user_arg' \ | ||
'variables' | ||
} | ||
@test "${FEATURE}: variables, verbose" { | ||
compare '-vuser_val1 -vuser_val2 -- user_arg' \ | ||
'variables' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env bats | ||
|
||
load ../test_helper | ||
|
||
# Compare in the following tests is simply used to populate | ||
# bash_getopts and getopts_long arrays with identical results. | ||
export GETOPTS_TEST_BIN='getopts_long-without_extdebug' | ||
export GETOPTS_LONG_TEST_BIN='getopts_long-without_extdebug' | ||
|
||
@test "${FEATURE}: toggles, silent" { | ||
compare 'toggles' 'toggles' | ||
expect "${getopts_long[1]}" =~ "getopts_long-without_extdebug-silent: line 8: getopts_long failed" | ||
} | ||
@test "${FEATURE}: toggles, verbose" { | ||
compare 'toggles' 'toggles' | ||
expect "${getopts_long[1]}" =~ "getopts_long-without_extdebug-verbose: line 8: getopts_long failed" | ||
} | ||
|
||
@test "${FEATURE}: options, silent" { | ||
compare 'options' 'options' | ||
expect "${getopts_long[1]}" =~ "getopts_long-without_extdebug-silent: line 8: getopts_long failed" | ||
} | ||
@test "${FEATURE}: options, verbose" { | ||
compare 'options' 'options' | ||
expect "${getopts_long[1]}" =~ "getopts_long-without_extdebug-verbose: line 8: getopts_long failed" | ||
} | ||
|
||
@test "${FEATURE}: variables, silent" { | ||
compare 'variables' 'variables' | ||
expect "${getopts_long[1]}" =~ "getopts_long-without_extdebug-silent: line 8: getopts_long failed" | ||
} | ||
@test "${FEATURE}: variables, verbose" { | ||
compare 'variables' 'variables' | ||
expect "${getopts_long[1]}" =~ "getopts_long-without_extdebug-verbose: line 8: getopts_long failed" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env bats | ||
|
||
load ../test_helper | ||
export GETOPTS_LONG_TEST_BIN='getopts_long-explicit_args' | ||
|
||
@test "${FEATURE}: toggles, silent" { | ||
compare '-t -t -- user_arg' \ | ||
'toggles' | ||
} | ||
@test "${FEATURE}: toggles, verbose" { | ||
compare '-t -t -- user_arg' \ | ||
'toggles' | ||
} | ||
|
||
@test "${FEATURE}: options, silent" { | ||
compare '-o user_val1 -o user_val2 -- user_arg' \ | ||
'options' | ||
} | ||
@test "${FEATURE}: options, verbose" { | ||
compare '-o user_val1 -o user_val2 -- user_arg' \ | ||
'options' | ||
} | ||
|
||
@test "${FEATURE}: variables, silent" { | ||
compare '-vuser_val1 -vuser_val2 -- user_arg' \ | ||
'variables' | ||
} | ||
@test "${FEATURE}: variables, verbose" { | ||
compare '-vuser_val1 -vuser_val2 -- user_arg' \ | ||
'variables' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#!/usr/bin/env bash | ||
|
||
TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)" | ||
# shellcheck disable=SC1090 | ||
source "${TOPDIR}/lib/getopts_long.bash" | ||
|
||
getopts_function() { | ||
while getopts_long ':to:v: toggle option: variable:' OPTKEY "$@"; do | ||
case ${OPTKEY} in | ||
't'|'toggle') | ||
printf 'toggle triggered' | ||
;; | ||
'o'|'option') | ||
printf 'option supplied' | ||
;; | ||
'v'|'variable') | ||
printf 'value supplied' | ||
;; | ||
'?') | ||
printf "INVALID OPTION" | ||
;; | ||
':') | ||
printf "MISSING ARGUMENT" | ||
;; | ||
*) | ||
printf "NEVER REACHED" | ||
;; | ||
esac | ||
printf ' -- ' | ||
declare -p OPTARG 2>&1 | grep -oe 'OPTARG.*' | ||
done | ||
|
||
shift $(( OPTIND - 1 )) | ||
|
||
echo "OPTERR: ${OPTERR}" | ||
echo "OPTKEY: ${OPTKEY}" | ||
echo "OPTARG: ${OPTARG}" | ||
echo "OPTIND: ${OPTIND}" | ||
|
||
local function_args=("$@") | ||
declare -p function_args \ | ||
| sed -e 's/declare -a function_args=/$@: /' | ||
} | ||
|
||
proxy() { | ||
getopts_function "$@" | ||
} | ||
|
||
toggles() { | ||
getopts_function -t --toggle -- user_arg | ||
} | ||
|
||
options() { | ||
getopts_function -o user_val1 --option user_val2 -- user_arg | ||
} | ||
|
||
variables() { | ||
getopts_function -vuser_val1 --variable=user_val2 -- user_arg | ||
} | ||
|
||
enable_extdebug='false' | ||
if shopt -q extdebug; then | ||
enable_extdebug='true' | ||
shopt -u extdebug | ||
fi | ||
|
||
: "${1:?Missing required argument -- function name}" | ||
function_name=${1} | ||
shift | ||
|
||
if declare -f "$function_name" > /dev/null; then | ||
${function_name} "$@" | ||
else | ||
echo "Function not found -- ${function_name}" | ||
exit 1 | ||
fi | ||
|
||
if ${enable_extdebug}; then | ||
shopt -s extdebug | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#!/usr/bin/env bash | ||
|
||
TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)" | ||
# shellcheck disable=SC1090 | ||
source "${TOPDIR}/lib/getopts_long.bash" | ||
|
||
getopts_function() { | ||
while getopts_long 'to:v: toggle option: variable:' OPTKEY "$@"; do | ||
case ${OPTKEY} in | ||
't'|'toggle') | ||
printf 'toggle triggered' | ||
;; | ||
'o'|'option') | ||
printf 'option supplied' | ||
;; | ||
'v'|'variable') | ||
printf 'value supplied' | ||
;; | ||
'?') | ||
printf "INVALID OPTION" | ||
;; | ||
':') | ||
printf "MISSING ARGUMENT" | ||
;; | ||
*) | ||
printf "NEVER REACHED" | ||
;; | ||
esac | ||
printf ' -- ' | ||
declare -p OPTARG 2>&1 | grep -oe 'OPTARG.*' | ||
done | ||
|
||
shift $(( OPTIND - 1 )) | ||
|
||
echo "OPTERR: ${OPTERR}" | ||
echo "OPTKEY: ${OPTKEY}" | ||
echo "OPTARG: ${OPTARG}" | ||
echo "OPTIND: ${OPTIND}" | ||
|
||
local function_args=("$@") | ||
declare -p function_args \ | ||
| sed -e 's/declare -a function_args=/$@: /' | ||
} | ||
|
||
proxy() { | ||
getopts_function "$@" | ||
} | ||
|
||
toggles() { | ||
getopts_function -t --toggle -- user_arg | ||
} | ||
|
||
options() { | ||
getopts_function -o user_val1 --option user_val2 -- user_arg | ||
} | ||
|
||
variables() { | ||
getopts_function -vuser_val1 --variable=user_val2 -- user_arg | ||
} | ||
|
||
enable_extdebug='false' | ||
if shopt -q extdebug; then | ||
enable_extdebug='true' | ||
shopt -u extdebug | ||
fi | ||
|
||
: "${1:?Missing required parameter -- function name}" | ||
function_name=${1} | ||
shift | ||
|
||
if declare -f "$function_name" > /dev/null; then | ||
${function_name} "$@" | ||
else | ||
echo "Function not found -- ${function_name}" | ||
exit 1 | ||
fi | ||
|
||
if ${enable_extdebug}; then | ||
shopt -s extdebug | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#!/usr/bin/env bash | ||
|
||
TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)" | ||
# shellcheck disable=SC1090 | ||
source "${TOPDIR}/lib/getopts_long.bash" | ||
|
||
getopts_function() { | ||
while getopts_long ':to:v: toggle option: variable:' OPTKEY; do | ||
case ${OPTKEY} in | ||
't'|'toggle') | ||
printf 'toggle triggered' | ||
;; | ||
'o'|'option') | ||
printf 'option supplied' | ||
;; | ||
'v'|'variable') | ||
printf 'value supplied' | ||
;; | ||
'?') | ||
printf "INVALID OPTION" | ||
;; | ||
':') | ||
printf "MISSING ARGUMENT" | ||
;; | ||
*) | ||
printf "NEVER REACHED" | ||
;; | ||
esac | ||
printf ' -- ' | ||
declare -p OPTARG 2>&1 | grep -oe 'OPTARG.*' | ||
done | ||
|
||
shift $(( OPTIND - 1 )) | ||
|
||
echo "OPTERR: ${OPTERR}" | ||
echo "OPTKEY: ${OPTKEY}" | ||
echo "OPTARG: ${OPTARG}" | ||
echo "OPTIND: ${OPTIND}" | ||
|
||
local function_args=("$@") | ||
declare -p function_args \ | ||
| sed -e 's/declare -a function_args=/$@: /' | ||
} | ||
|
||
proxy() { | ||
getopts_function "$@" | ||
} | ||
|
||
toggles() { | ||
getopts_function -t --toggle -- user_arg | ||
} | ||
|
||
options() { | ||
getopts_function -o user_val1 --option user_val2 -- user_arg | ||
} | ||
|
||
variables() { | ||
getopts_function -vuser_val1 --variable=user_val2 -- user_arg | ||
} | ||
|
||
disable_extdebug='false' | ||
if ! shopt -q extdebug; then | ||
disable_extdebug='true' | ||
shopt -s extdebug | ||
fi | ||
|
||
: "${1:?Missing required argument -- function name}" | ||
function_name=${1} | ||
shift | ||
|
||
if declare -f "$function_name" > /dev/null; then | ||
${function_name} "$@" | ||
else | ||
echo "Function not found -- ${function_name}" | ||
exit 1 | ||
fi | ||
|
||
if ${disable_extdebug}; then | ||
shopt -u extdebug | ||
fi |
Oops, something went wrong.