-
-
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.
Browse files
Browse the repository at this point in the history
This PR adds checks to prevent getopts_long errors when `set -u` (shorthand for `set -o nounset`) is enabled. This setting ensures that accessing an unset or undefined variable triggers an error, which helps catch scripting mistakes and avoid unintended behaviour caused by typos or missing variables. Co-authored-by: Will Allan <billyzkid@yahoo.com>
- Loading branch information
Showing
10 changed files
with
200 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env bats | ||
|
||
load ../test_helper | ||
export GETOPTS_LONG_TEST_BIN='getopts_long-nounset' | ||
|
||
@test "${FEATURE}: toggles, silent" { | ||
compare '-t -t -- user_arg' \ | ||
'-t --toggle -- user_arg' | ||
} | ||
@test "${FEATURE}: toggles, verbose" { | ||
compare '-t -t -- user_arg' \ | ||
'-t --toggle -- user_arg' | ||
} | ||
|
||
@test "${FEATURE}: options, silent" { | ||
compare '-o user_val1 -o user_val2 -- user_arg' \ | ||
'-o user_val1 --option user_val2 -- user_arg' | ||
} | ||
@test "${FEATURE}: options, verbose" { | ||
compare '-o user_val1 -o user_val2 -- user_arg' \ | ||
'-o user_val1 --option user_val2 -- user_arg' | ||
} | ||
|
||
@test "${FEATURE}: variables, silent" { | ||
compare '-vuser_val1 -vuser_val2 -- user_arg' \ | ||
'-vuser_val1 --variable=user_val2 -- user_arg' | ||
} | ||
@test "${FEATURE}: variables, verbose" { | ||
compare '-vuser_val1 -vuser_val2 -- user_arg' \ | ||
'-vuser_val1 --variable=user_val2 -- user_arg' | ||
} | ||
|
||
@test "${FEATURE}: missing last argument, silent" { | ||
compare '-vuser_val1 -vuser_val2 -- user_arg' \ | ||
'-vuser_val1 --variable=user_val2 -- user_arg' | ||
} | ||
@test "${FEATURE}: missing last argument, verbose" { | ||
compare '-o' \ | ||
'--option' \ | ||
'1{s/getopts.+verbose:/getopts-NORMALIZED:/}' \ | ||
'1{s/(option requires an argument --) (o|option)$/\1 NORMALIZED/}' | ||
} |
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,44 @@ | ||
#!/usr/bin/env bash | ||
|
||
TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)" | ||
# shellcheck disable=SC1090 | ||
source "${TOPDIR}/lib/getopts_long.bash" | ||
|
||
( | ||
set -o nounset | ||
|
||
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:-}" | ||
|
||
args=("$@") | ||
declare -p args | sed -e 's/declare -a args=/$@: /' | ||
) |
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,41 @@ | ||
#!/usr/bin/env bash | ||
|
||
TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)" | ||
# shellcheck disable=SC1090 | ||
source "${TOPDIR}/lib/getopts_long.bash" | ||
|
||
( | ||
set -o nounset | ||
|
||
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 or 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:-}" | ||
|
||
args=("$@") | ||
declare -p args | sed -e 's/declare -a args=/$@: /' | ||
) |
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
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