Skip to content

Commit 0d5cec7

Browse files
committed
Merge branch 'feature/expected-arguments' into develop
2 parents 90dbe1e + c313775 commit 0d5cec7

File tree

1 file changed

+59
-5
lines changed

1 file changed

+59
-5
lines changed

argument-parser.sh

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ regexArgLongWithValue='^--([a-zA-Z0-9\-]{2,})=(.*)$'
88

99
argChunks=()
1010

11-
ARG_DEBUG=false
11+
ARG_DEBUG=true
12+
ARG_MUST_BE_DEFINED=true
1213

1314
# Expand chained short form arguments, eg -aih => -a -i -h
1415
for argChunk in "$@"; do
@@ -46,6 +47,29 @@ declare -A argv
4647
lastWasArgument=0
4748
lastArgument=""
4849

50+
declare -A argExpected
51+
argExpected['v|verbose']="verbose - verbosity level"
52+
53+
argGetName() {
54+
for k in "${!argExpected[@]}"
55+
do
56+
regexArg="\|($1)\|"
57+
[[ "|$k|" =~ $regexArg ]]
58+
if [ "${BASH_REMATCH[1]}" != "" ]; then
59+
60+
regexArgName="(.+) - "
61+
[[ "${argExpected[$k]}" =~ $regexArgName ]]
62+
63+
echo "${BASH_REMATCH[1]}"
64+
break
65+
fi
66+
done
67+
}
68+
69+
argUnexpected() {
70+
echo "UNEXPECTED ARGUMENT $1"
71+
}
72+
4973
# Loop over all the argument chunks and determine if the argument type and value
5074
for argChunk in "${argChunks[@]}"; do
5175

@@ -56,8 +80,17 @@ for argChunk in "${argChunks[@]}"; do
5680
lastWasArgument=1
5781
lastArgument="$argument"
5882

83+
# Get the name of the argument
84+
argName="$(argGetName "$argument")"
85+
86+
# Check we could get an argument
87+
if [ "$argName" == "" ]; then
88+
argUnexpected "$argChunk"
89+
exit 1
90+
fi
91+
5992
# Add the argument to the arguments array
60-
argv["${BASH_REMATCH[1]}"]=''
93+
argv["$argName"]=''
6194

6295
[ $ARG_DEBUG == true ] && echo "Argument (short): ${BASH_REMATCH[1]}"
6396

@@ -70,8 +103,17 @@ for argChunk in "${argChunks[@]}"; do
70103
argument="${BASH_REMATCH[1]}"
71104
lastArgument="$argument"
72105

106+
# Get the name of the argument
107+
argName="$(argGetName "$argument")"
108+
109+
# Check we could get an argument
110+
if [ "$argName" == "" ]; then
111+
argUnexpected "$argChunk"
112+
exit 1
113+
fi
114+
73115
# Add the argument to the arguments array
74-
argv["${BASH_REMATCH[1]}"]="${BASH_REMATCH[2]}"
116+
argv["$argName"]="${BASH_REMATCH[2]}"
75117

76118
[ $ARG_DEBUG == true ] && echo "Argument (long with value): ${BASH_REMATCH[1]}=${BASH_REMATCH[2]}"
77119

@@ -85,8 +127,17 @@ for argChunk in "${argChunks[@]}"; do
85127
lastWasArgument=1
86128
lastArgument="$argument"
87129

130+
# Get the name of the argument
131+
argName="$(argGetName "$argument")"
132+
133+
# Check we could get an argument
134+
if [ "$argName" == "" ]; then
135+
argUnexpected "$argChunk"
136+
exit 1
137+
fi
138+
88139
# Add the argument to the arguments array
89-
argv["${BASH_REMATCH[1]}"]=''
140+
argv["$argName"]=''
90141

91142
[ $ARG_DEBUG == true ] && echo "Argument (long): ${BASH_REMATCH[1]}"
92143

@@ -96,8 +147,11 @@ for argChunk in "${argChunks[@]}"; do
96147
# If the last chunk was an argument and this wasn't assume its an argument value
97148
if [ $lastWasArgument == 1 ]; then
98149

150+
# Get the name of the argument
151+
argName="$(argGetName "$lastArgument")"
152+
99153
# Add the arguments value to the arguments array
100-
argv["$lastArgument"]="$argChunk"
154+
argv["$argName"]="$argChunk"
101155

102156
[ $ARG_DEBUG == true ] && echo "Argument Value: $argChunk"
103157

0 commit comments

Comments
 (0)