diff --git a/README.md b/README.md index 93aa4a61..acd1a5a5 100644 --- a/README.md +++ b/README.md @@ -1385,6 +1385,10 @@ exitIfCommandDoesNotExist "git" "please install it via https://git-scm.com/downl # meant to be used in a file which is sourced where a contract exists between the file which `source`s and the sourced file exitIfVarsNotAlreadySetBySource myVar1 var2 var3 + +declare myVar4 +exitIfVariablesNotDefined myVar4 myVar5 # would exit because myVar5 is not set +echo "myVar4 $myVar4" ``` diff --git a/spec/utility/parse-args_spec.sh b/spec/utility/parse-args_spec.sh index 84b3b104..4f5f4408 100644 --- a/spec/utility/parse-args_spec.sh +++ b/spec/utility/parse-args_spec.sh @@ -14,6 +14,8 @@ Describe 'parse-arg.sh' Describe 'parseArguments' Describe '--help' It 'without examples' + # shellcheck disable=SC2034 # withoutHelp is never used but that's fine + declare version withoutHelp declare params=( version -v 'The version' withoutHelp -wv '' @@ -22,11 +24,12 @@ Describe 'parse-arg.sh' The status should be successful The output should include 'Parameters' The output should include '-v' - The output should include '-wv' The output should include 'The version' + The output should include '-wv' The output should not include 'Examples' End It 'with examples' + declare version declare params=(version -v 'The version') When call parseArguments params "example code\non multiple lines" 'v1.0.0' --help The status should be successful @@ -38,6 +41,7 @@ Describe 'parse-arg.sh' End End It '--version' + declare version declare params=(version -v 'The version') When call parseArguments params "example code\non multiple lines" "v1.2.0-RC2" --version The status should be successful @@ -45,7 +49,7 @@ Describe 'parse-arg.sh' End Describe 'happy cases' It 'does not fail if not for all parameters an argument is passed' - declare version + declare version asdf declare params=( version -v '' asdf -a '' @@ -56,7 +60,8 @@ Describe 'parse-arg.sh' End It 'does not pollute parent "scope"' function foo() { - declare version + # shellcheck disable=SC2034 # asdf is not used but that's fine + declare version asdf declare params=( version -v '' asdf -a '' @@ -71,6 +76,7 @@ Describe 'parse-arg.sh' End Describe 'errors' It 'not enough arguments passed' + declare version declare params=(version -v '') When run parseArguments params The status should be failure @@ -78,6 +84,7 @@ Describe 'parse-arg.sh' End Describe 'wrong number in params' It 'one leftover' + declare version declare params=(version -v '' leftOver1) When run parseArguments params '' 'v1.0.0'--help The status should be failure @@ -88,6 +95,7 @@ Describe 'parse-arg.sh' The stderr should include 'leftOver1' End It 'two leftovers' + declare version declare params=(version -v '' leftOver1 leftOver2) When run parseArguments params '' 'v1.0.0' --help The status should be failure @@ -106,6 +114,16 @@ Describe 'parse-arg.sh' The stderr should include "$(printf "array \033[0;36massociativeParams\033[0m is broken")" The stderr should include 'The first argument to parse_args_exitIfParameterDefinitionIsNotTriple needs to be a non-associative array containing parameter definitions' End + It 'fails if variable not defined in scope' + declare version + declare params=( + version -v '' + asdf -a '' + ) + When run parseArguments params 'example' 'v1.0.0' -v v0.1.0 + The status should be failure + The stderr should include "$(printf "you need to define the variable \033[0;36masdf\033[0m")" + End End End diff --git a/src/utility/checks.doc.sh b/src/utility/checks.doc.sh index f10175f7..ba490227 100644 --- a/src/utility/checks.doc.sh +++ b/src/utility/checks.doc.sh @@ -53,3 +53,7 @@ exitIfCommandDoesNotExist "git" "please install it via https://git-scm.com/downl # meant to be used in a file which is sourced where a contract exists between the file which `source`s and the sourced file exitIfVarsNotAlreadySetBySource myVar1 var2 var3 + +declare myVar4 +exitIfVariablesNotDefined myVar4 myVar5 # would exit because myVar5 is not set +echo "myVar4 $myVar4" diff --git a/src/utility/checks.sh b/src/utility/checks.sh index 4d29dda2..501f3a99 100644 --- a/src/utility/checks.sh +++ b/src/utility/checks.sh @@ -70,6 +70,10 @@ # # meant to be used in a file which is sourced where a contract exists between the file which `source`s and the sourced file # exitIfVarsNotAlreadySetBySource myVar1 var2 var3 # +# declare myVar4 +# exitIfVariablesNotDefined myVar4 myVar5 # would exit because myVar5 is not set +# echo "myVar4 $myVar4" +# ################################### set -euo pipefail shopt -s inherit_errexit @@ -295,3 +299,14 @@ function exitIfVarsNotAlreadySetBySource() { fi done } + +function exitIfVariablesNotDefined() { + shift 1 || die "could not shift by 1" + for variableName in "$@"; do + if ! declare -p "$variableName" 2>/dev/null | grep -q 'declare --'; then + logError "you need to define the variable \033[0;36m%s\033[0m otherwise we write to the global scope" "$variableName" + printStackTrace + exit 1 + fi + done +} diff --git a/src/utility/parse-args.sh b/src/utility/parse-args.sh index 9cfa75b7..0dc4b546 100644 --- a/src/utility/parse-args.sh +++ b/src/utility/parse-args.sh @@ -135,6 +135,11 @@ function parseArgumentsInternal { parse_args_exitIfParameterDefinitionIsNotTriple parseArguments_paramArr + # shellcheck disable=SC2034 # passed by name to exitIfVariablesNotDefined + local -a parseArguments_variableNames + arrTakeEveryX parseArguments_paramArr parseArguments_variableNames 3 0 + exitIfVariablesNotDefined "${parseArguments_variableNames[@]}" + local -ri parseArguments_arrLength="${#parseArguments_paramArr[@]}" local -i parseArguments_numOfArgumentsParsed=0 diff --git a/src/utility/parse-fn-args.sh b/src/utility/parse-fn-args.sh index 48b2203e..5a20a2e4 100644 --- a/src/utility/parse-fn-args.sh +++ b/src/utility/parse-fn-args.sh @@ -127,6 +127,8 @@ function parseFnArgs() { exit 9 fi + exitIfVariablesNotDefined "${parseFnArgs_paramArr1[@]}" + for ((parseFnArgs_i = 0; parseFnArgs_i < parseFnArgs_minExpected; ++parseFnArgs_i)); do local parseFnArgs_name=${parseFnArgs_paramArr1[parseFnArgs_i]} # assign arguments to specified variables