You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
45
47
46
48
By default if an argument is passed that hasn't been defined an error will be thrown and the script will exit.
47
49
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).
48
50
51
+
### Defining Argument Default Values
52
+
53
+
You can define a default value that will be used if the argument isn't passed:
Now if the script is called and the `-e` argument is omitted `argValue "argumentName"` will return `900`
61
+
The default value can also be set to an empty string (`argExpected['e']="argumentName= - Argument description"`)
62
+
49
63
### Get An Arguments Value
50
64
51
65
There is a helper function named `argValue()` which takes the name of
52
66
an argument as its only parameter and returns the value given to the argument.
53
67
54
-
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.
55
70
56
71
```bash
57
72
# -a 'some text'
@@ -86,30 +101,32 @@ esac
86
101
87
102
### Check If An Argument Has Been Passed
88
103
89
-
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
90
105
an argument as its only parameter and returns a boolean.
91
106
107
+
`argPassed` will return false if the argument has fallen back to its default value*
108
+
92
109
```bash
93
110
# -v
94
-
ifargExists'v';then
111
+
ifargPassed'v';then
95
112
echo"The -v argument has been passed"
96
113
fi
97
114
98
115
# -rMd
99
-
argExists'r'&&echo"The -r argument was passed"
116
+
argPassed'r'&&echo"The -r argument was passed"
100
117
101
118
# --long-argument-name
102
-
ifargExists'long-argument-name';then
119
+
ifargPassed'long-argument-name';then
103
120
# Do something awesome
104
121
fi
105
122
106
123
# --protocol=HTTP
107
-
ifargExists'protocol';then
124
+
ifargPassed'protocol';then
108
125
# Do something awesome
109
126
fi
110
127
111
128
# -O 43
112
-
argExists'O'&&echo"Found the -O argument"
129
+
argPassed'O'&&echo"Found the -O argument"
113
130
```
114
131
115
132
## Supported Argument Formats
@@ -169,10 +186,35 @@ The order the arguments are passed on the command line makes a difference
169
186
### Examples
170
187
171
188
* Calling `my-script.sh -f first -f last` will cause `argValue "f"` to return the value `last`
172
-
* Calling `my-script.sh -g 345 -g` will mean cause `argValue "g"` to return nothing
173
-
* Calling `my-script.sh --size 512 --size=1024` will mean cause `argValue "size"` to return `1024`
189
+
* Calling `my-script.sh -g 345 -g` will cause `argValue "g"` to return nothing
190
+
* Calling `my-script.sh --size 512 --size=1024` will cause `argValue "size"` to return `1024`
191
+
192
+
## Passing Additional Non-Arguments Strings
193
+
194
+
If you need to pass in non-argument stings along side your arguments you just need to add the end of arguments marker `--` and anything that follows wont be parsed as an argument but instead will be assigned to a numbered positional argument:
195
+
196
+
For example running: `./script.sh --arg1 --arg2 -- file1 file2 -f file5`
197
+
198
+
Will be parsed as:
199
+
200
+
* Argument: `--arg1`
201
+
* Argument: `--arg2`
202
+
* Non-Argument: `file1` (accessible via `$1`)
203
+
* Non-Argument: `file2` (accessible via `$2`)
204
+
* Non-Argument: `-f` (accessible via `$3`)
205
+
* Non-Argument: `file5` (accessible via `$4`)
206
+
207
+
This way you can define arguments along side an arbitrary number of strings you may want to operate on at the same time!
174
208
175
209
## Debug Mode
176
210
177
-
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
211
+
There is a debug mode that can be enabled by setting the `ARG_DEBUG` variable to `true` right before including the library.
212
+
This will cause the script to dump out information about which flags it finds and of what kind etc
213
+
214
+
## Testing
215
+
216
+
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.
217
+
218
+
### Running Tests
219
+
220
+
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
0 commit comments