Skip to content

Commit fe10e65

Browse files
committed
Removed the argParse function definition so the argParse code runs on load
1 parent b47fdb1 commit fe10e65

File tree

1 file changed

+89
-92
lines changed

1 file changed

+89
-92
lines changed

argument-parser.sh

Lines changed: 89 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -156,130 +156,127 @@ argParseDefaults() {
156156
done
157157
}
158158

159-
argParse() {
159+
# Populate the argv array with the defaults
160+
argParseDefaults
160161

161-
# Populate the argv array with the defaults
162-
argParseDefaults
162+
# Loop over all the argument chunks and determine if the argument type and value
163+
for argChunk in "${argChunks[@]}"; do
163164

164-
# Loop over all the argument chunks and determine if the argument type and value
165-
for argChunk in "${argChunks[@]}"; do
165+
# Check if we've passed the last argument marker
166+
if [ $endOfArguments == 1 ]; then
167+
parameters+=("$argChunk")
168+
echo "#:$argChunk"
169+
continue
170+
fi
166171

167-
# Check if we've passed the last argument marker
168-
if [ $endOfArguments == 1 ]; then
169-
parameters+=("$argChunk")
170-
echo "#:$argChunk"
171-
continue
172-
fi
172+
# Check if this chunk is the last argument marker
173+
if [ "$argChunk" == "--" ]; then
174+
endOfArguments=1
175+
continue;
176+
fi
173177

174-
# Check if this chunk is the last argument marker
175-
if [ "$argChunk" == "--" ]; then
176-
endOfArguments=1
177-
continue;
178+
# Check if this chunk is a short form argument
179+
[[ $argChunk =~ $regexArgShort ]]
180+
if [ "${BASH_REMATCH[1]}" != "" ]; then
181+
argument="${BASH_REMATCH[1]}"
182+
lastWasArgument=1
183+
lastArgument="$argument"
184+
185+
# Get the name of the argument
186+
argName="$(argGetName "$argument")"
187+
188+
# Check we could get an argument, return code 2 means an error was returned
189+
if [ "$?" == "2" ]; then
190+
echo "$argName"
191+
exit 1
178192
fi
179193

180-
# Check if this chunk is a short form argument
181-
[[ $argChunk =~ $regexArgShort ]]
182-
if [ "${BASH_REMATCH[1]}" != "" ]; then
183-
argument="${BASH_REMATCH[1]}"
184-
lastWasArgument=1
185-
lastArgument="$argument"
194+
# Add the argument to the arguments array
195+
argv["$argName"]=''
186196

187-
# Get the name of the argument
188-
argName="$(argGetName "$argument")"
197+
[ "$ARG_DEBUG" == true ] && echo "Argument (short): ${BASH_REMATCH[1]}"
189198

190-
# Check we could get an argument, return code 2 means an error was returned
191-
if [ "$?" == "2" ]; then
192-
echo "$argName"
193-
exit 1
194-
fi
199+
continue;
200+
fi
195201

196-
# Add the argument to the arguments array
197-
argv["$argName"]=''
202+
# Check if this chunk is a long form with value argument
203+
[[ $argChunk =~ $regexArgLongWithValue ]]
204+
if [ "${BASH_REMATCH[1]}" != "" ]; then
205+
argument="${BASH_REMATCH[1]}"
206+
lastArgument="$argument"
198207

199-
[ "$ARG_DEBUG" == true ] && echo "Argument (short): ${BASH_REMATCH[1]}"
208+
# Get the name of the argument
209+
argName="$(argGetName "$argument")"
200210

201-
continue;
211+
# Check we could get an argument, return code 2 means an error was returned
212+
if [ "$?" == "2" ]; then
213+
echo "$argName"
214+
exit 1
202215
fi
203216

204-
# Check if this chunk is a long form with value argument
205-
[[ $argChunk =~ $regexArgLongWithValue ]]
206-
if [ "${BASH_REMATCH[1]}" != "" ]; then
207-
argument="${BASH_REMATCH[1]}"
208-
lastArgument="$argument"
217+
# Add the argument to the arguments array
218+
argv["$argName"]="${BASH_REMATCH[2]}"
209219

210-
# Get the name of the argument
211-
argName="$(argGetName "$argument")"
220+
[ "$ARG_DEBUG" == true ] && echo "Argument (long with value): ${BASH_REMATCH[1]}=${BASH_REMATCH[2]}"
212221

213-
# Check we could get an argument, return code 2 means an error was returned
214-
if [ "$?" == "2" ]; then
215-
echo "$argName"
216-
exit 1
217-
fi
222+
continue;
223+
fi
218224

219-
# Add the argument to the arguments array
220-
argv["$argName"]="${BASH_REMATCH[2]}"
225+
# Check if this chunk is a long form argument
226+
[[ $argChunk =~ $regexArgLong ]]
227+
if [ "${BASH_REMATCH[1]}" != "" ]; then
228+
argument="${BASH_REMATCH[1]}"
229+
lastWasArgument=1
230+
lastArgument="$argument"
221231

222-
[ "$ARG_DEBUG" == true ] && echo "Argument (long with value): ${BASH_REMATCH[1]}=${BASH_REMATCH[2]}"
232+
# Get the name of the argument
233+
argName="$(argGetName "$argument")"
223234

224-
continue;
235+
# Check we could get an argument, return code 2 means an error was returned
236+
if [ "$?" == "2" ]; then
237+
echo "$argName"
238+
exit 1
225239
fi
226240

227-
# Check if this chunk is a long form argument
228-
[[ $argChunk =~ $regexArgLong ]]
229-
if [ "${BASH_REMATCH[1]}" != "" ]; then
230-
argument="${BASH_REMATCH[1]}"
231-
lastWasArgument=1
232-
lastArgument="$argument"
241+
# Add the argument to the arguments array
242+
argv["$argName"]=''
233243

234-
# Get the name of the argument
235-
argName="$(argGetName "$argument")"
244+
[ "$ARG_DEBUG" == true ] && echo "Argument (long): ${BASH_REMATCH[1]}"
236245

237-
# Check we could get an argument, return code 2 means an error was returned
238-
if [ "$?" == "2" ]; then
239-
echo "$argName"
240-
exit 1
241-
fi
246+
continue;
247+
fi
242248

243-
# Add the argument to the arguments array
244-
argv["$argName"]=''
249+
# If the last chunk was an argument and this wasn't assume its an argument value
250+
if [ $lastWasArgument == 1 ]; then
245251

246-
[ "$ARG_DEBUG" == true ] && echo "Argument (long): ${BASH_REMATCH[1]}"
252+
# Get the name of the argument
253+
argName="$(argGetName "$lastArgument")"
247254

248-
continue;
255+
# Check we could get an argument, return code 2 means an error was returned
256+
if [ "$?" == "2" ]; then
257+
echo "$argName"
258+
exit 1
249259
fi
250260

251-
# If the last chunk was an argument and this wasn't assume its an argument value
252-
if [ $lastWasArgument == 1 ]; then
253-
254-
# Get the name of the argument
255-
argName="$(argGetName "$lastArgument")"
256-
257-
# Check we could get an argument, return code 2 means an error was returned
258-
if [ "$?" == "2" ]; then
259-
echo "$argName"
260-
exit 1
261-
fi
262-
263-
# Add the arguments value to the arguments array
264-
argv["$argName"]="$argChunk"
261+
# Add the arguments value to the arguments array
262+
argv["$argName"]="$argChunk"
265263

266-
[ "$ARG_DEBUG" == true ] && echo "Argument Value: $argChunk"
264+
[ "$ARG_DEBUG" == true ] && echo "Argument Value: $argChunk"
267265

268-
lastWasArgument=0
269-
fi
270-
done
266+
lastWasArgument=0
267+
fi
268+
done
271269

272-
[ "$ARG_DEBUG" == true ] && echo "Argument array:"
273-
[ "$ARG_DEBUG" == true ] && for k in "${!argv[@]}"
274-
do
275-
echo "ARG: $k = ${argv[$k]}"
276-
done
270+
[ "$ARG_DEBUG" == true ] && echo "Argument array:"
271+
[ "$ARG_DEBUG" == true ] && for k in "${!argv[@]}"
272+
do
273+
echo "ARG: $k = ${argv[$k]}"
274+
done
277275

278-
# Add the standard argc variable containing the number of arguments
279-
argc=${#argv[@]}
276+
# Add the standard argc variable containing the number of arguments
277+
argc=${#argv[@]}
280278

281-
[ "$ARG_DEBUG" == true ] && echo "Argument Count: $argc"
282-
}
279+
[ "$ARG_DEBUG" == true ] && echo "Argument Count: $argc"
283280

284281
# If we are accessing this script directly run the argument parser, useful for testing
285282
if [ "$0" == "$BASH_SOURCE" ]; then

0 commit comments

Comments
 (0)