Skip to content

Commit 1cc4938

Browse files
committed
Merge branch 'feature/argument-defaults' into develop
2 parents bbab3fa + 3e5e57f commit 1cc4938

File tree

5 files changed

+185
-12
lines changed

5 files changed

+185
-12
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,12 @@ The order the arguments are passed on the command line makes a difference
175175
## Debug Mode
176176

177177
There is a debug mode that can be enabled by setting the `ARG_DEBUG` variable to `true` right before calling `argParse`.
178-
This will cause the script to dump out information about which flags it finds and of what kind etc
178+
This will cause the script to dump out information about which flags it finds and of what kind etc
179+
180+
## Testing
181+
182+
There is a rudimentary test suite included with the project that can be used to check that changes haven't broken any other part of the code.
183+
184+
### Running Tests
185+
186+
It's as simple as executing `test.sh` in the `tests` directory, if you see any red blips there is a problem, if it's all green then everything should be ok

argument-parser.sh

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

9+
regexArgName="^([^= \-]+)"
10+
regexArgDefault='^([^=]+)=(.*) -'
11+
12+
# Initialise some variables
13+
declare -A argv;
14+
argv=()
15+
declare -A argExpected
16+
argExpected=()
17+
declare -a argChunks
918
argChunks=()
1019

20+
lastWasArgument=0
21+
lastArgument=""
22+
1123
# Expand chained short form arguments, eg -aih => -a -i -h
1224
for argChunk in "$@"; do
1325

@@ -39,21 +51,13 @@ done
3951

4052
[ "$ARG_DEBUG" == true ] && echo "Expanded argument list: ${argChunks[@]}"
4153

42-
# Initialise some variables
43-
declare -A argv
44-
lastWasArgument=0
45-
lastArgument=""
46-
47-
declare -A argExpected
48-
4954
argGetName() {
5055
for k in "${!argExpected[@]}"
5156
do
5257
regexArg="\|($1)\|"
5358
[[ "|$k|" =~ $regexArg ]]
5459
if [ "${BASH_REMATCH[1]}" != "" ]; then
5560

56-
regexArgName="(.+) - "
5761
[[ "${argExpected[$k]}" =~ $regexArgName ]]
5862

5963
echo "${BASH_REMATCH[1]}"
@@ -92,13 +96,19 @@ argList() {
9296
done
9397
done <<< "$arguments"
9498

95-
regexArgName=".+ - (.+)"
99+
regexArgName="^[^=]+=?(.+)? - (.+)"
96100
[[ "${argExpected[$arguments]}" =~ $regexArgName ]]
97101

98102
local argumentList="${argumentsPrefixed[@]}"
99-
local argumentDesc="${BASH_REMATCH[1]}"
103+
local argumentDesc="${BASH_REMATCH[2]}"
104+
local argumentDefault="${BASH_REMATCH[1]}"
105+
100106
echo " $argumentList"
101-
echo " $argumentDesc"
107+
if [[ "$argumentDefault" == '' ]]; then
108+
echo " $argumentDesc"
109+
else
110+
echo " $argumentDesc Default: $argumentDefault"
111+
fi
102112
echo
103113
done
104114
}
@@ -121,7 +131,24 @@ argValue() {
121131
fi
122132
}
123133

134+
argParseDefaults() {
135+
136+
for arguments in "${!argExpected[@]}"; do
137+
[[ ${argExpected[$arguments]} =~ $regexArgDefault ]]
138+
139+
if [[ "${BASH_REMATCH[@]}" == '' ]]; then
140+
continue;
141+
fi
142+
143+
argv["${BASH_REMATCH[1]}"]="${BASH_REMATCH[2]}"
144+
done
145+
}
146+
124147
argParse() {
148+
149+
# Populate the argv array with the defaults
150+
argParseDefaults
151+
125152
# Loop over all the argument chunks and determine if the argument type and value
126153
for argChunk in "${argChunks[@]}"; do
127154

tests/default.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
# Include the Argument Parser library
4+
source ../argument-parser.sh
5+
6+
# Define the expected arguments
7+
argExpected['alpha|a']="alphaArg=alpha - The first argument"
8+
argExpected['bravo|b']="bravoArg=string with spaces - The second argument"
9+
argExpected['charlie|c']="charlieArg=hyphenated-string - The third argument"
10+
argExpected['delta|d']="deltaArg= - The forth argument"
11+
argExpected['numeric|n']="numericArg=25 - A numeric argument"
12+
13+
# Parse any arguments
14+
argParse
15+
16+
[ "$(argValue "alphaArg")" == "alpha" ] && fail || pass
17+
[ "$(argValue "bravoArg")" == "string with spaces" ] && fail || pass
18+
[ "$(argValue "charlieArg")" == "hyphenated-string" ] && fail || pass
19+
[ "$(argValue "deltaArg")" == "" ] && fail || pass
20+
[ "$(argValue "numericArg")" == 25 ] && fail || pass
21+
22+
23+
argExists "alphaArg" && fail || pass
24+
argExists "bravoArg" && fail || pass
25+
argExists "charlieArg" && fail || pass
26+
argExists "deltaArg" && fail || pass
27+
argExists "numericArg" && fail || pass

tests/simple.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
# Include the Argument Parser library
4+
source ../argument-parser.sh
5+
6+
# Define the expected arguments
7+
argExpected['alpha|a']="alphaArg - The first argument"
8+
argExpected['bravo|b']="bravoArg - The second argument"
9+
argExpected['charlie|c']="charlieArg - The third argument"
10+
argExpected['delta|d']="deltaArg - The forth argument"
11+
argExpected['numeric|n']="numericArg - A numeric argument"
12+
argExpected['quoted|q']="quotedArg - A quoted string argument"
13+
14+
# Parse any arguments
15+
argParse
16+
17+
[ "$(argValue "alphaArg")" == "alpha" ] && fail || pass
18+
[ "$(argValue "bravoArg")" == "bravo" ] && fail || pass
19+
[ "$(argValue "charlieArg")" == "charlie" ] && fail || pass
20+
[ "$(argValue "deltaArg")" == "delta" ] && fail || pass
21+
[ "$(argValue "numericArg")" == 4 ] && fail || pass
22+
[ "$(argValue "quotedArg")" == "quoted string" ] && fail || pass
23+
24+
25+
argExists "alphaArg" && fail || pass
26+
argExists "bravoArg" && fail || pass
27+
argExists "charlieArg" && fail || pass
28+
argExists "deltaArg" && fail || pass
29+
argExists "numericArg" && fail || pass
30+
argExists "quotedArg" && fail || pass

tests/tests.sh

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/bin/bash
2+
3+
pass() {
4+
echo -en "\033[31m\xE2\x80\xA2\033[0m"
5+
}
6+
7+
fail() {
8+
echo -en "\033[32m\xE2\x80\xA2\033[0m"
9+
}
10+
11+
echo "Running Tests"
12+
13+
echo -n "- Long arguments with equals only: "
14+
. simple.sh \
15+
--alpha=alpha \
16+
--bravo=bravo \
17+
--charlie=charlie \
18+
--delta=delta \
19+
--numeric=4 \
20+
--quoted="quoted string"
21+
echo
22+
23+
echo -n "- Long arguments without equals only: "
24+
. simple.sh \
25+
--alpha alpha \
26+
--bravo bravo \
27+
--charlie charlie \
28+
--delta delta \
29+
--numeric 4 \
30+
--quoted "quoted string"
31+
echo
32+
33+
echo -n "- Long arguments overriding short arguments: "
34+
. simple.sh \
35+
-a badoption \
36+
--alpha=alpha \
37+
-b badoption \
38+
--bravo=bravo \
39+
-c badoption \
40+
--charlie=charlie \
41+
-d badoption \
42+
--delta=delta \
43+
-n badoption \
44+
--numeric=4 \
45+
-q "quoted string"
46+
echo
47+
48+
echo -n "- Short arguments without equals only: "
49+
. simple.sh \
50+
-a alpha \
51+
-b bravo \
52+
-c charlie \
53+
-d delta \
54+
-n 4 \
55+
-q "quoted string"
56+
echo
57+
58+
echo -n "- Short arguments overriding long arguments: "
59+
. simple.sh \
60+
--alpha=badoption \
61+
-a alpha \
62+
--bravo=badoption \
63+
-b bravo \
64+
--charlie=badoption \
65+
-c charlie \
66+
--delta=badoption \
67+
-d delta \
68+
--numeric=badoption \
69+
-n 4 \
70+
-q "quoted string"
71+
echo
72+
73+
74+
75+
76+
77+
78+
79+
echo -n "- Default values: "
80+
. default.sh
81+
echo

0 commit comments

Comments
 (0)