Skip to content

Commit 30cf457

Browse files
committed
Merge branch 'develop'
2 parents 49bb2c1 + 20a30c4 commit 30cf457

File tree

5 files changed

+400
-114
lines changed

5 files changed

+400
-114
lines changed

README.md

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
# BASH Argument Parser
22

3-
Takes arguments passed in nearly any format to a bash script and allows easy access to them and their values
3+
Takes arguments passed to a bash script in nearly any format and allows easy access to them and their values
44

55
## Use
66

77
### How To Use
88

9-
Just include the library in the head of script, define all the arguments you need and call the parser function
9+
Just define all the arguments you need and include the library in the head of script, that's it.
1010

1111
```bash
12-
# Include the Argument Parser library
13-
source ./my/lib/path/argument-parser.sh
14-
1512
# Define the expected arguments
13+
declare -A argExpected
1614
argExpected['test']="argName - This is a short description of the argument and what it does"
1715
argExpected['R']="secondArgName - This is another argument that can be passed"
1816

19-
# Parse any arguments
20-
argParse
17+
# Include and run the Argument Parser library
18+
source ./my/lib/path/argument-parser.sh
2119
```
2220

2321
### Defining Expected Arguments
2422

25-
The argument parser can take an array of arguments to expect, it has the following format:
23+
The argument parser takes an array of arguments to expect, it has the following format:
2624

2725
```bash
26+
# Define argExpected as an associative array
27+
# This must occur once before you build the array of argument definitions
28+
declare -A argExpected
29+
2830
# Define the -r argument
2931
argExpected['r']="argumentName - Argument description"
3032

@@ -41,17 +43,30 @@ argExpected['a|A']="argumentName - Argument description"
4143
argExpected['d|deamon|D']="argumentName - Argument description"
4244
```
4345

44-
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.
4547

4648
By default if an argument is passed that hasn't been defined an error will be thrown and the script will exit.
4749
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).
4850

51+
### Defining Argument Default Values
52+
53+
You can define a default value that will be used if the argument isn't passed:
54+
55+
```bash
56+
# Set the -e arguments default value to 900
57+
argExpected['e']="argumentName=900 - Argument description"
58+
```
59+
60+
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+
4963
### Get An Arguments Value
5064

5165
There is a helper function named `argValue()` which takes the name of
5266
an argument as its only parameter and returns the value given to the argument.
5367

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.
5570

5671
```bash
5772
# -a 'some text'
@@ -86,30 +101,32 @@ esac
86101

87102
### Check If An Argument Has Been Passed
88103

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
90105
an argument as its only parameter and returns a boolean.
91106

107+
`argPassed` will return false if the argument has fallen back to its default value*
108+
92109
```bash
93110
# -v
94-
if argExists 'v'; then
111+
if argPassed 'v'; then
95112
echo "The -v argument has been passed"
96113
fi
97114

98115
# -rMd
99-
argExists 'r' && echo "The -r argument was passed"
116+
argPassed 'r' && echo "The -r argument was passed"
100117

101118
# --long-argument-name
102-
if argExists 'long-argument-name'; then
119+
if argPassed 'long-argument-name'; then
103120
# Do something awesome
104121
fi
105122

106123
# --protocol=HTTP
107-
if argExists 'protocol'; then
124+
if argPassed 'protocol'; then
108125
# Do something awesome
109126
fi
110127

111128
# -O 43
112-
argExists 'O' && echo "Found the -O argument"
129+
argPassed 'O' && echo "Found the -O argument"
113130
```
114131

115132
## Supported Argument Formats
@@ -169,10 +186,35 @@ The order the arguments are passed on the command line makes a difference
169186
### Examples
170187

171188
* 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!
174208

175209
## Debug Mode
176210

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

Comments
 (0)