Skip to content

Commit ce9366d

Browse files
committed
Merge branch 'develop'
2 parents 0185687 + 90dbe1e commit ce9366d

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ if argExists 'protocol'; then
6767
fi
6868

6969
# -O 43
70-
argExists 'r' && echo "Found the -O argument"
70+
argExists 'O' && echo "Found the -O argument"
7171
```
7272

7373
## Supported Argument Formats
@@ -128,6 +128,7 @@ The order the arguments are passed on the command line makes a difference
128128

129129
* Calling `my-script.sh -f first -f last` will cause `argValue "f"` to return the value `last`
130130
* Calling `my-script.sh -g 345 -g` will mean cause `argValue "g"` to return nothing
131+
* Calling `my-script.sh --size 512 --size=1024` will mean cause `argValue "size"` to return 1024
131132

132133
## Debug Mode
133134

argument-parser.sh

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ done
4242
[ $ARG_DEBUG == true ] && echo "Expanded argument list: ${argChunks[@]}"
4343

4444
# Initialise some variables
45-
declare -A args
45+
declare -A argv
4646
lastWasArgument=0
4747
lastArgument=""
4848

@@ -57,7 +57,7 @@ for argChunk in "${argChunks[@]}"; do
5757
lastArgument="$argument"
5858

5959
# Add the argument to the arguments array
60-
args["${BASH_REMATCH[1]}"]=''
60+
argv["${BASH_REMATCH[1]}"]=''
6161

6262
[ $ARG_DEBUG == true ] && echo "Argument (short): ${BASH_REMATCH[1]}"
6363

@@ -71,7 +71,7 @@ for argChunk in "${argChunks[@]}"; do
7171
lastArgument="$argument"
7272

7373
# Add the argument to the arguments array
74-
args["${BASH_REMATCH[1]}"]="${BASH_REMATCH[2]}"
74+
argv["${BASH_REMATCH[1]}"]="${BASH_REMATCH[2]}"
7575

7676
[ $ARG_DEBUG == true ] && echo "Argument (long with value): ${BASH_REMATCH[1]}=${BASH_REMATCH[2]}"
7777

@@ -86,7 +86,7 @@ for argChunk in "${argChunks[@]}"; do
8686
lastArgument="$argument"
8787

8888
# Add the argument to the arguments array
89-
args["${BASH_REMATCH[1]}"]=''
89+
argv["${BASH_REMATCH[1]}"]=''
9090

9191
[ $ARG_DEBUG == true ] && echo "Argument (long): ${BASH_REMATCH[1]}"
9292

@@ -97,7 +97,7 @@ for argChunk in "${argChunks[@]}"; do
9797
if [ $lastWasArgument == 1 ]; then
9898

9999
# Add the arguments value to the arguments array
100-
args["$lastArgument"]="$argChunk"
100+
argv["$lastArgument"]="$argChunk"
101101

102102
[ $ARG_DEBUG == true ] && echo "Argument Value: $argChunk"
103103

@@ -106,13 +106,13 @@ for argChunk in "${argChunks[@]}"; do
106106
done
107107

108108
[ $ARG_DEBUG == true ] && echo "Argument array:"
109-
[ $ARG_DEBUG == true ] && for k in "${!args[@]}"
109+
[ $ARG_DEBUG == true ] && for k in "${!argv[@]}"
110110
do
111-
echo "ARG: $k = ${args[$k]}"
111+
echo "ARG: $k = ${argv[$k]}"
112112
done
113113

114114
argExists() {
115-
if [ -z ${args["$1"]+abc} ]; then
115+
if [ -z ${argv["$1"]+abc} ]; then
116116
return 1
117117
else
118118
return 0
@@ -121,6 +121,11 @@ argExists() {
121121

122122
argValue() {
123123
if argExists "$1"; then
124-
echo "${args["$1"]}"
124+
echo "${argv["$1"]}"
125125
fi
126126
}
127+
128+
# Add the standard argc variable containing the number of arguments
129+
argc=${#argv[@]}
130+
131+
[ $ARG_DEBUG == true ] && echo "Argument Count: $argc"

0 commit comments

Comments
 (0)