Takes arguments passed in nearly any format to a bash script and allows easy access to them and their values
Just include the library in the head of script, define all the arguments you need and call the parser function
# Include the Argument Parser library
source ./my/lib/path/argument-parser.sh
# Define the expected arguments
argExpected['test']="argName - This is a short description of the argument and what it does"
argExpected['R']="secondArgName - This is another argument that can be passed"
# Parse any arguments
argParseThe argument parser can take an array of arguments to expect, it has the following format:
# Define the -r argument
argExpected['r']="argumentName - Argument description"
# Define the --test argument
argExpected['test']="argumentName - Argument description"
# Define both the -u and --uniform arguments
argExpected['u|uniform']="argumentName - Argument description"
# Define both the -a and -A arguments
argExpected['a|A']="argumentName - Argument description"
# Define the -d, --deamon and -D arguments
argExpected['d|deamon|D']="argumentName - Argument description"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.
By default if an argument is passed that hasn't been defined an error will be thrown and the script will exit.
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).
There is a helper function named argValue() which takes the name of
an argument as its only parameter and returns the value given to the argument.
If the argument doesn't have a value or hasn't been passed nothing is returned.
# -a 'some text'
if [ "$(argValue "a")" == 'some text' ]; then
# Do something awesome
fi
# --debug 3
DEBUG_LEVEL="$(argValue "debug")" # 3
# --max-size=1024
MAX_SIZE="$(argValue "max-size")" # 1024
# -R 0
[ "$(argValue "R")" == "0" ] && echo "Recursive depth 0"
# -aXb 'cookie'
BISCUIT="$(argValue "b")" # 'cookie'
# -g 'hello' -G 'world'
ALPHA="$(argValue "g")" # 'hello'
BRAVO="$(argValue "G")" # 'world'
# -S d
case $(argValue 'S') in
a) echo "something" ;;
b) echo "something" ;;
c) echo "something" ;;
d) echo "darkside" ;;
esacThere is a helper function named argExists() which takes the name of
an argument as its only parameter and returns a boolean.
# -v
if argExists 'v'; then
echo "The -v argument has been passed"
fi
# -rMd
argExists 'r' && echo "The -r argument was passed"
# --long-argument-name
if argExists 'long-argument-name'; then
# Do something awesome
fi
# --protocol=HTTP
if argExists 'protocol'; then
# Do something awesome
fi
# -O 43
argExists 'O' && echo "Found the -O argument"-a
-X
-2
-b somevalue
-c 38
-d "some value with spaces"
-e "some value with
newlines"
-7 "ate nine"--debug
--test mode0
--test "all the code"
--Case-Sensitive true
--long-parameter
--newline "
"
--1337
--365 "days"--lang="en"
--crawl=false
--match-pattern=".+"
--newline="
"
--UpperCase=sensitive
--45="25+20"-aih # Equivalent to -a -i -h
-dav 4 # Equivalent to -d -a -v 4
-H3cx # Equivalent to -H -3 -c -xThe order the arguments are passed on the command line makes a difference
- Calling
my-script.sh -f first -f lastwill causeargValue "f"to return the valuelast - Calling
my-script.sh -g 345 -gwill mean causeargValue "g"to return nothing - Calling
my-script.sh --size 512 --size=1024will mean causeargValue "size"to return1024
There is a debug mode that can be enabled by setting the ARG_DEBUG variable to true right before calling argParse.
This will cause the script to dump out information about which flags it finds and of what kind etc