Skip to content

Commit d9cacd5

Browse files
authored
Merge pull request #273 from tegonal/bugfix/check-isNotArrayOr-check-if-array-initialised
checkIsInitialisedArray, use in exitIfArgIsNotArrayOr...
2 parents d0d2103 + 4ab59e2 commit d9cacd5

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,9 @@ function foo() {
13881388
}
13891389
# check array with 2-tuples
13901390
exitIfArgIsNotArrayWithTuples arr 2 "names" 1 describePair
1391+
1392+
# returns 0 if the array was initialised (i.e. a value assigned) and non-0 otherwise
1393+
checkIsInitialisedArray arr
13911394
}
13921395

13931396
if checkCommandExists "cat"; then

src/utility/checks.doc.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ function foo() {
4040
}
4141
# check array with 2-tuples
4242
exitIfArgIsNotArrayWithTuples arr 2 "names" 1 describePair
43+
44+
# returns 0 if the array was initialised (i.e. a value assigned) and non-0 otherwise
45+
checkIsInitialisedArray arr
4346
}
4447

4548
if checkCommandExists "cat"; then

src/utility/checks.sh

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
# }
5757
# # check array with 2-tuples
5858
# exitIfArgIsNotArrayWithTuples arr 2 "names" 1 describePair
59+
#
60+
# # returns 0 if the array was initialised (i.e. a value assigned) and non-0 otherwise
61+
# checkIsInitialisedArray arr
5962
# }
6063
#
6164
# if checkCommandExists "cat"; then
@@ -130,19 +133,30 @@ function exitIfArgIsNotArray() {
130133
function exitIfArgIsNotArrayOrIsEmpty() {
131134
exitIfArgIsNotArray "$@"
132135
local -rn exitIfArgIsNotArrayOrIsEmpty_arr=$1
133-
if [[ ${#exitIfArgIsNotArrayOrIsEmpty_arr[@]} -lt 1 ]]; then
136+
# shellcheck disable=SC2310 # we are aware of that if and ! will disable set -e for checkIsInitialisedArray
137+
if ! checkIsInitialisedArray exitIfArgIsNotArrayOrIsEmpty_arr; then
138+
traceAndDie "the passed argument \033[0;36m%s\033[0m is an uninitialised array" "${!exitIfArgIsNotArrayOrIsEmpty_arr}"
139+
elif [[ ${#exitIfArgIsNotArrayOrIsEmpty_arr[@]} -lt 1 ]]; then
134140
traceAndDie "the passed argument \033[0;36m%s\033[0m is an empty array" "${!exitIfArgIsNotArrayOrIsEmpty_arr}"
135141
fi
136142
}
137143

138144
function exitIfArgIsNotArrayOrIsNonEmpty() {
139145
exitIfArgIsNotArray "$@"
140146
local -rn exitIfArgIsNotArrayOrIsNonEmpty_arr=$1
141-
if [[ ${#exitIfArgIsNotArrayOrIsNonEmpty_arr[@]} -gt 0 ]]; then
147+
# shellcheck disable=SC2310 # we are aware of that if and ! will disable set -e for checkIsInitialisedArray
148+
if checkIsInitialisedArray exitIfArgIsNotArrayOrIsNonEmpty_arr && [[ ${#exitIfArgIsNotArrayOrIsNonEmpty_arr[@]} -gt 0 ]]; then
142149
traceAndDie "the passed argument \033[0;36m%s\033[0m is a non empty array" "${!exitIfArgIsNotArrayOrIsNonEmpty_arr}"
143150
fi
144151
}
145152

153+
function checkIsInitialisedArray() {
154+
if (($# != 1)); then
155+
traceAndDie "One argument needs to be passed to checkIsInitialisedArray, the array name, given \033[0;36m%s\033[0m\n" "$#"
156+
fi
157+
recursiveDeclareP "$1" | grep '(' >/dev/null
158+
}
159+
146160
function checkArgIsArrayWithTuples() {
147161
if (($# != 5)); then
148162
logError "Five arguments need to be passed to checkArgIsArrayWithTuples, given \033[0;36m%s\033[0m\n" "$#"

0 commit comments

Comments
 (0)