Skip to content

Commit 785219b

Browse files
authored
Merge pull request #241 from tegonal/feature/assignToVariableInOuterScope
introduce assignToVariableInOuterScope
2 parents d3a9c78 + 6ab6a06 commit 785219b

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/utility/parse-args.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ function parseArgumentsInternal {
186186
parseArgumentsInternal_ask_printHelp
187187
exit 9
188188
fi
189-
# that's where the black magic happens, we are assigning to global (not local to this function) variables here
190-
printf -v "$parseArguments_paramName" "%s" "$2" || traceAndDie "could not assign value to $parseArguments_paramName"
189+
assignToVariableInOuterScope "$parseArguments_paramName" "$2"
191190
parseArguments_expectedName=1
192191
((++parseArguments_numOfArgumentsParsed))
193192
shift 1 || traceAndDie "could not shift by 1"

src/utility/parse-utils.sh

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,30 @@ if ! [[ -v dir_of_tegonal_scripts ]]; then
5757
dir_of_tegonal_scripts="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null && pwd 2>/dev/null)/.."
5858
source "$dir_of_tegonal_scripts/setup.sh" "$dir_of_tegonal_scripts"
5959
fi
60+
sourceOnce "$dir_of_tegonal_scripts/utility/checks.sh"
6061

6162
function printVersion() {
62-
if ! (($# == 1)) && ! (($# == 2)); then
63-
logError "One argument needs to be passed to printVersion, given \033[0;36m%s\033[0m\nFollowing a description of the parameters:" "$#"
63+
if (($# != 1)) && (($# != 2)); then
64+
logError "Either one or two arguments needs to be passed to printVersion, given \033[0;36m%s\033[0m\nFollowing a description of the parameters:" "$#"
6465
echo >&2 '1: version the version which shall be shown if one uses --version'
65-
echo >&2 '2: stackFrame number of frames to drop to determine the source of the call'
66+
echo >&2 '2: stackFrame number of frames to drop to determine the source of the call -- default 3'
6667
printStackTrace
6768
exit 9
6869
fi
6970
local version=$1
7071
local stackFrame=${2:-3}
71-
logInfo "Version of %s is:\n%s" "$(basename "${BASH_SOURCE[stackFrame]:-${BASH_SOURCE[((stackFrame-1))]}}")" "$version"
72+
logInfo "Version of %s is:\n%s" "$(basename "${BASH_SOURCE[stackFrame]:-${BASH_SOURCE[((stackFrame - 1))]}}")" "$version"
73+
}
74+
75+
function assignToVariableInOuterScope() {
76+
if (($# != 2)); then
77+
logError "Exactly two arguments needs to be passed to assignToOuterScopeVariable, given \033[0;36m%s\033[0m\nFollowing a description of the parameters:" "$#"
78+
echo >&2 '1: variableName the name of the variable in the outer scope to which the given value shall be assigned'
79+
echo >&2 '2: value the value which shall be assigned to the variable'
80+
printStackTrace
81+
exit 9
82+
fi
83+
exitIfVariablesNotDeclared "$1"
84+
# that's where the black magic happens, we are assigning to global (not local to this function) variables here
85+
printf -v "$1" "%s" "$2" || traceAndDie "could not assign value to %s" "$1"
7286
}

0 commit comments

Comments
 (0)