Skip to content

Commit 29ded6d

Browse files
committed
Added a function to populate the argv array with default values
1 parent bbab3fa commit 29ded6d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

argument-parser.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ regexArgShortChained='^-([a-zA-Z0-9]{2,})$'
66
regexArgLong='^--([a-zA-Z0-9\-]{2,})$'
77
regexArgLongWithValue='^--([a-zA-Z0-9\-]{2,})=(.*)$'
88

9+
regexArgDefault='^([^=]+)=(.+) -'
10+
911
argChunks=()
1012

1113
# Expand chained short form arguments, eg -aih => -a -i -h
@@ -121,7 +123,24 @@ argValue() {
121123
fi
122124
}
123125

126+
argParseDefaults() {
127+
128+
for arguments in "${!argExpected[@]}"; do
129+
[[ ${argExpected[$arguments]} =~ $regexArgDefault ]]
130+
131+
if [[ "${BASH_REMATCH[@]}" == '' ]]; then
132+
continue;
133+
fi
134+
135+
argv["${BASH_REMATCH[1]}"]="${BASH_REMATCH[2]}"
136+
done
137+
}
138+
124139
argParse() {
140+
141+
# Populate the argv array with the defaults
142+
argParseDefaults
143+
125144
# Loop over all the argument chunks and determine if the argument type and value
126145
for argChunk in "${argChunks[@]}"; do
127146

0 commit comments

Comments
 (0)