Skip to content

Commit 1ed94a3

Browse files
committed
Merge branch 'feature/default-not-marked-as-exists' into develop
2 parents dc090ff + f405b44 commit 1ed94a3

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ argExpected['a|A']="argumentName - Argument description"
4343
argExpected['d|deamon|D']="argumentName - Argument description"
4444
```
4545

46-
The `argumentName` part of the definition is the name given to the argument and what should be passed to the `argValue` and `argExists` functions, see below. The argument name is case sensitive and must not contain spaces or an equals sign.
46+
The `argumentName` part of the definition is the name given to the argument and what should be passed to the `argValue` and `argPassed` functions, see below. The argument name is case sensitive and must not contain spaces or an equals sign.
4747

4848
By default if an argument is passed that hasn't been defined an error will be thrown and the script will exit.
4949
This feature can be turned off by setting `ARG_MUST_BE_DEFINED` to `false`, note that the argument names will default to the argument its self, without the preceding hyphen(s).
@@ -65,7 +65,8 @@ The default value can also be set to an empty string (`argExpected['e']="argumen
6565
There is a helper function named `argValue()` which takes the name of
6666
an argument as its only parameter and returns the value given to the argument.
6767

68-
If the argument doesn't have a value or hasn't been passed nothing is returned.
68+
If the argument doesn't have a value or hasn't been passed nothing is returned
69+
unless it's been given a default, in which case the default value will be returned.
6970

7071
```bash
7172
# -a 'some text'
@@ -100,30 +101,32 @@ esac
100101

101102
### Check If An Argument Has Been Passed
102103

103-
There is a helper function named `argExists()` which takes the name of
104+
There is a helper function named `argPassed` which takes the name of
104105
an argument as its only parameter and returns a boolean.
105106

107+
`argPassed` will return false if the argument has fallen back to its default value*
108+
106109
```bash
107110
# -v
108-
if argExists 'v'; then
111+
if argPassed 'v'; then
109112
echo "The -v argument has been passed"
110113
fi
111114

112115
# -rMd
113-
argExists 'r' && echo "The -r argument was passed"
116+
argPassed 'r' && echo "The -r argument was passed"
114117

115118
# --long-argument-name
116-
if argExists 'long-argument-name'; then
119+
if argPassed 'long-argument-name'; then
117120
# Do something awesome
118121
fi
119122

120123
# --protocol=HTTP
121-
if argExists 'protocol'; then
124+
if argPassed 'protocol'; then
122125
# Do something awesome
123126
fi
124127

125128
# -O 43
126-
argExists 'O' && echo "Found the -O argument"
129+
argPassed 'O' && echo "Found the -O argument"
127130
```
128131

129132
## Supported Argument Formats

argument-parser.sh

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ regexArgDesc='^.* - (.*)'
1212
# Initialise some variables
1313
declare -A argv;
1414
argv=()
15+
declare -A argd;
16+
argd=()
1517
declare -a argChunks
1618
argChunks=()
1719
declare -a parameters
@@ -123,17 +125,30 @@ argUnexpected() {
123125
echo "UNEXPECTED ARGUMENT $1"
124126
}
125127

126-
argExists() {
128+
argPassed() {
127129
if [ -z ${argv["$1"]+abc} ]; then
128-
return 1
130+
return 1 # false
129131
else
130-
return 0
132+
return 0 # true
133+
fi
134+
}
135+
136+
argHasDefault() {
137+
if [ -z ${argd["$1"]+abc} ]; then
138+
return 1 # false
139+
else
140+
return 0 # true
131141
fi
132142
}
133143

134144
argValue() {
135-
if argExists "$1"; then
145+
if argPassed "$1"; then
136146
echo "${argv["$1"]}"
147+
exit 0
148+
fi
149+
150+
if argHasDefault "$1"; then
151+
echo "${argd["$1"]}"
137152
fi
138153
}
139154

@@ -149,7 +164,7 @@ argParseDefaults() {
149164
# Get the name of this argument
150165
local argumentName="$(argGetName "$arguments")"
151166

152-
argv["$argumentName"]="${BASH_REMATCH[1]}"
167+
argd["$argumentName"]="${BASH_REMATCH[1]}"
153168
done
154169
}
155170

tests/default.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ source ../argument-parser.sh
1919
[ "$(argValue "hyphenated-arg")" == "hyphenated" ] && pass || fail
2020

2121

22-
argExists "alphaArg" && pass || fail
23-
argExists "bravoArg" && pass || fail
24-
argExists "charlieArg" && pass || fail
25-
argExists "deltaArg" && pass || fail
26-
argExists "numericArg" && pass || fail
27-
argExists "hyphenated-arg" && pass || fail
22+
argPassed "alphaArg" && fail || pass
23+
argPassed "bravoArg" && fail || pass
24+
argPassed "charlieArg" && fail || pass
25+
argPassed "deltaArg" && fail || pass
26+
argPassed "numericArg" && fail || pass
27+
argPassed "hyphenated-arg" && fail || pass

tests/simple.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ source ../argument-parser.sh
2121
[ "$(argValue "hyphenated-arg")" == "hyphenated" ] && pass || fail
2222

2323

24-
argExists "alphaArg" && pass || fail
25-
argExists "bravoArg" && pass || fail
26-
argExists "charlieArg" && pass || fail
27-
argExists "deltaArg" && pass || fail
28-
argExists "numericArg" && pass || fail
29-
argExists "quotedArg" && pass || fail
30-
argExists "hyphenated-arg" && pass || fail
24+
argPassed "alphaArg" && pass || fail
25+
argPassed "bravoArg" && pass || fail
26+
argPassed "charlieArg" && pass || fail
27+
argPassed "deltaArg" && pass || fail
28+
argPassed "numericArg" && pass || fail
29+
argPassed "quotedArg" && pass || fail
30+
argPassed "hyphenated-arg" && pass || fail

0 commit comments

Comments
 (0)