Skip to content

Commit 39e855c

Browse files
committed
Moved the default argument values to a new array (argd) and added logic to the argValue method to fallback to the default value if no value was passed
1 parent dc090ff commit 39e855c

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

argument-parser.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ regexArgDesc='^.* - (.*)'
1212
# Initialise some variables
1313
declare -A argv;
1414
argv=()
15+
declare -A argd;
16+
argd=()
1517
declare -a argChunks
1618
argChunks=()
1719
declare -a parameters
@@ -131,9 +133,22 @@ argExists() {
131133
fi
132134
}
133135

136+
argHasDefault() {
137+
if [ -z ${argd["$1"]+abc} ]; then
138+
return 1
139+
else
140+
return 0
141+
fi
142+
}
143+
134144
argValue() {
135145
if argExists "$1"; then
136146
echo "${argv["$1"]}"
147+
exit 0
148+
fi
149+
150+
if argHasDefault "$1"; then
151+
echo "${argd["$1"]}"
137152
fi
138153
}
139154

@@ -149,7 +164,7 @@ argParseDefaults() {
149164
# Get the name of this argument
150165
local argumentName="$(argGetName "$arguments")"
151166

152-
argv["$argumentName"]="${BASH_REMATCH[1]}"
167+
argd["$argumentName"]="${BASH_REMATCH[1]}"
153168
done
154169
}
155170

0 commit comments

Comments
 (0)