Skip to content

Commit a58810a

Browse files
committed
Merge branch 'feature/funcionalise' into develop
2 parents 23c2238 + 9651f01 commit a58810a

File tree

1 file changed

+95
-92
lines changed

1 file changed

+95
-92
lines changed

argument-parser.sh

Lines changed: 95 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ lastWasArgument=0
4848
lastArgument=""
4949

5050
declare -A argExpected
51-
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"
5551

5652
argGetName() {
5753
for k in "${!argExpected[@]}"
@@ -84,122 +80,129 @@ argUnexpected() {
8480
echo "UNEXPECTED ARGUMENT $1"
8581
}
8682

87-
# Loop over all the argument chunks and determine if the argument type and value
88-
for argChunk in "${argChunks[@]}"; do
89-
90-
# Check if this chunk is a short form argument
91-
[[ $argChunk =~ $regexArgShort ]]
92-
if [ "${BASH_REMATCH[1]}" != "" ]; then
93-
argument="${BASH_REMATCH[1]}"
94-
lastWasArgument=1
95-
lastArgument="$argument"
83+
argExists() {
84+
if [ -z ${argv["$1"]+abc} ]; then
85+
return 1
86+
else
87+
return 0
88+
fi
89+
}
9690

97-
# Get the name of the argument
98-
argName="$(argGetName "$argument")"
91+
argValue() {
92+
if argExists "$1"; then
93+
echo "${argv["$1"]}"
94+
fi
95+
}
9996

100-
# Check we could get an argument, return code 2 means an error was returned
101-
if [ "$?" == "2" ]; then
102-
echo "$argName"
103-
exit 1
104-
fi
97+
argParse() {
98+
# Loop over all the argument chunks and determine if the argument type and value
99+
for argChunk in "${argChunks[@]}"; do
105100

106-
# Add the argument to the arguments array
107-
argv["$argName"]=''
101+
# Check if this chunk is a short form argument
102+
[[ $argChunk =~ $regexArgShort ]]
103+
if [ "${BASH_REMATCH[1]}" != "" ]; then
104+
argument="${BASH_REMATCH[1]}"
105+
lastWasArgument=1
106+
lastArgument="$argument"
108107

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

111-
continue;
112-
fi
111+
# Check we could get an argument, return code 2 means an error was returned
112+
if [ "$?" == "2" ]; then
113+
echo "$argName"
114+
exit 1
115+
fi
113116

114-
# Check if this chunk is a long form with value argument
115-
[[ $argChunk =~ $regexArgLongWithValue ]]
116-
if [ "${BASH_REMATCH[1]}" != "" ]; then
117-
argument="${BASH_REMATCH[1]}"
118-
lastArgument="$argument"
117+
# Add the argument to the arguments array
118+
argv["$argName"]=''
119119

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

123-
# Check we could get an argument, return code 2 means an error was returned
124-
if [ "$?" == "2" ]; then
125-
echo "$argName"
126-
exit 1
122+
continue;
127123
fi
128124

129-
# Add the argument to the arguments array
130-
argv["$argName"]="${BASH_REMATCH[2]}"
125+
# Check if this chunk is a long form with value argument
126+
[[ $argChunk =~ $regexArgLongWithValue ]]
127+
if [ "${BASH_REMATCH[1]}" != "" ]; then
128+
argument="${BASH_REMATCH[1]}"
129+
lastArgument="$argument"
131130

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

134-
continue;
135-
fi
134+
# Check we could get an argument, return code 2 means an error was returned
135+
if [ "$?" == "2" ]; then
136+
echo "$argName"
137+
exit 1
138+
fi
136139

137-
# Check if this chunk is a long form argument
138-
[[ $argChunk =~ $regexArgLong ]]
139-
if [ "${BASH_REMATCH[1]}" != "" ]; then
140-
argument="${BASH_REMATCH[1]}"
141-
lastWasArgument=1
142-
lastArgument="$argument"
140+
# Add the argument to the arguments array
141+
argv["$argName"]="${BASH_REMATCH[2]}"
143142

144-
# Get the name of the argument
145-
argName="$(argGetName "$argument")"
143+
[ $ARG_DEBUG == true ] && echo "Argument (long with value): ${BASH_REMATCH[1]}=${BASH_REMATCH[2]}"
146144

147-
# Check we could get an argument, return code 2 means an error was returned
148-
if [ "$?" == "2" ]; then
149-
echo "$argName"
150-
exit 1
145+
continue;
151146
fi
152147

153-
# Add the argument to the arguments array
154-
argv["$argName"]=''
148+
# Check if this chunk is a long form argument
149+
[[ $argChunk =~ $regexArgLong ]]
150+
if [ "${BASH_REMATCH[1]}" != "" ]; then
151+
argument="${BASH_REMATCH[1]}"
152+
lastWasArgument=1
153+
lastArgument="$argument"
155154

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

158-
continue;
159-
fi
158+
# Check we could get an argument, return code 2 means an error was returned
159+
if [ "$?" == "2" ]; then
160+
echo "$argName"
161+
exit 1
162+
fi
160163

161-
# If the last chunk was an argument and this wasn't assume its an argument value
162-
if [ $lastWasArgument == 1 ]; then
164+
# Add the argument to the arguments array
165+
argv["$argName"]=''
163166

164-
# Get the name of the argument
165-
argName="$(argGetName "$lastArgument")"
167+
[ $ARG_DEBUG == true ] && echo "Argument (long): ${BASH_REMATCH[1]}"
166168

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
169+
continue;
171170
fi
172171

173-
# Add the arguments value to the arguments array
174-
argv["$argName"]="$argChunk"
172+
# If the last chunk was an argument and this wasn't assume its an argument value
173+
if [ $lastWasArgument == 1 ]; then
175174

176-
[ $ARG_DEBUG == true ] && echo "Argument Value: $argChunk"
175+
# Get the name of the argument
176+
argName="$(argGetName "$lastArgument")"
177177

178-
lastWasArgument=0
179-
fi
180-
done
178+
# Check we could get an argument, return code 2 means an error was returned
179+
if [ "$?" == "2" ]; then
180+
echo "$argName"
181+
exit 1
182+
fi
181183

182-
[ $ARG_DEBUG == true ] && echo "Argument array:"
183-
[ $ARG_DEBUG == true ] && for k in "${!argv[@]}"
184-
do
185-
echo "ARG: $k = ${argv[$k]}"
186-
done
184+
# Add the arguments value to the arguments array
185+
argv["$argName"]="$argChunk"
187186

188-
argExists() {
189-
if [ -z ${argv["$1"]+abc} ]; then
190-
return 1
191-
else
192-
return 0
193-
fi
194-
}
187+
[ $ARG_DEBUG == true ] && echo "Argument Value: $argChunk"
195188

196-
argValue() {
197-
if argExists "$1"; then
198-
echo "${argv["$1"]}"
199-
fi
200-
}
189+
lastWasArgument=0
190+
fi
191+
done
192+
193+
[ $ARG_DEBUG == true ] && echo "Argument array:"
194+
[ $ARG_DEBUG == true ] && for k in "${!argv[@]}"
195+
do
196+
echo "ARG: $k = ${argv[$k]}"
197+
done
198+
199+
# Add the standard argc variable containing the number of arguments
200+
argc=${#argv[@]}
201201

202-
# Add the standard argc variable containing the number of arguments
203-
argc=${#argv[@]}
202+
[ $ARG_DEBUG == true ] && echo "Argument Count: $argc"
203+
}
204204

205-
[ $ARG_DEBUG == true ] && echo "Argument Count: $argc"
205+
# If we are accessing this script directly run the argument parser, useful for testing
206+
if [ "$0" == "$BASH_SOURCE" ]; then
207+
argParse
208+
fi

0 commit comments

Comments
 (0)