Skip to content

Commit 23c2238

Browse files
committed
Merge branch 'feature/expected-arguments' into develop
2 parents 0d5cec7 + 12ca381 commit 23c2238

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

argument-parser.sh

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ lastArgument=""
4949

5050
declare -A argExpected
5151
argExpected['v|verbose']="verbose - verbosity level"
52+
argExpected['t|test']="test - testing mode"
53+
argExpected['r|recursive']="recursive - Recurse in to directory"
54+
argExpected['u']="uniform - All the same"
5255

5356
argGetName() {
5457
for k in "${!argExpected[@]}"
@@ -61,9 +64,20 @@ argGetName() {
6164
[[ "${argExpected[$k]}" =~ $regexArgName ]]
6265

6366
echo "${BASH_REMATCH[1]}"
64-
break
67+
exit 0
6568
fi
6669
done
70+
71+
# Check if the argument must be defined
72+
if [ $ARG_MUST_BE_DEFINED == true ]; then
73+
argUnexpected "$argChunk"
74+
exit 2
75+
fi
76+
77+
# Default to using the argument as the name
78+
echo "$1"
79+
80+
exit 1
6781
}
6882

6983
argUnexpected() {
@@ -83,9 +97,9 @@ for argChunk in "${argChunks[@]}"; do
8397
# Get the name of the argument
8498
argName="$(argGetName "$argument")"
8599

86-
# Check we could get an argument
87-
if [ "$argName" == "" ]; then
88-
argUnexpected "$argChunk"
100+
# Check we could get an argument, return code 2 means an error was returned
101+
if [ "$?" == "2" ]; then
102+
echo "$argName"
89103
exit 1
90104
fi
91105

@@ -106,9 +120,9 @@ for argChunk in "${argChunks[@]}"; do
106120
# Get the name of the argument
107121
argName="$(argGetName "$argument")"
108122

109-
# Check we could get an argument
110-
if [ "$argName" == "" ]; then
111-
argUnexpected "$argChunk"
123+
# Check we could get an argument, return code 2 means an error was returned
124+
if [ "$?" == "2" ]; then
125+
echo "$argName"
112126
exit 1
113127
fi
114128

@@ -130,9 +144,9 @@ for argChunk in "${argChunks[@]}"; do
130144
# Get the name of the argument
131145
argName="$(argGetName "$argument")"
132146

133-
# Check we could get an argument
134-
if [ "$argName" == "" ]; then
135-
argUnexpected "$argChunk"
147+
# Check we could get an argument, return code 2 means an error was returned
148+
if [ "$?" == "2" ]; then
149+
echo "$argName"
136150
exit 1
137151
fi
138152

@@ -150,6 +164,12 @@ for argChunk in "${argChunks[@]}"; do
150164
# Get the name of the argument
151165
argName="$(argGetName "$lastArgument")"
152166

167+
# Check we could get an argument, return code 2 means an error was returned
168+
if [ "$?" == "2" ]; then
169+
echo "$argName"
170+
exit 1
171+
fi
172+
153173
# Add the arguments value to the arguments array
154174
argv["$argName"]="$argChunk"
155175

0 commit comments

Comments
 (0)