Skip to content

Commit 3b42a2f

Browse files
committed
Added a debug mode that is off by default
1 parent 86d3681 commit 3b42a2f

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

argument-parser.sh

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ regexArgLong='^--([a-zA-Z\-]{2,})$'
77

88
argChunks=()
99

10+
ARG_DEBUG=false
11+
1012
# Expand chained short form arguments, eg -aih => -a -i -h
1113
for argChunk in "$@"; do
1214

@@ -16,6 +18,9 @@ for argChunk in "$@"; do
1618

1719
# Get the chunk or arguments
1820
chainedChunk="${BASH_REMATCH[1]}";
21+
22+
[ $ARG_DEBUG == true ] && echo "Expanding chained argument chunk: $chainedChunk"
23+
1924
i=0
2025
# Expand out the chunk into individual arguments
2126
while (( i++ < ${#chainedChunk} )); do
@@ -33,6 +38,8 @@ for argChunk in "$@"; do
3338
argChunks+=("$argChunk")
3439
done
3540

41+
[ $ARG_DEBUG == true ] && echo "Expanded argument list: ${argChunks[@]}"
42+
3643
# Initialise some variables
3744
declare -A args
3845
lastWasArgument=0
@@ -51,7 +58,7 @@ for argChunk in "${argChunks[@]}"; do
5158
# Add the argument to the arguments array
5259
args["${BASH_REMATCH[1]}"]=''
5360

54-
#echo "Argument (short): ${BASH_REMATCH[1]}"
61+
[ $ARG_DEBUG == true ] && echo "Argument (short): ${BASH_REMATCH[1]}"
5562

5663
continue;
5764
fi
@@ -66,7 +73,7 @@ for argChunk in "${argChunks[@]}"; do
6673
# Add the argument to the arguments array
6774
args["${BASH_REMATCH[1]}"]=''
6875

69-
#echo "Argument (long): ${BASH_REMATCH[1]}"
76+
[ $ARG_DEBUG == true ] && echo "Argument (long): ${BASH_REMATCH[1]}"
7077

7178
continue;
7279
fi
@@ -77,14 +84,14 @@ for argChunk in "${argChunks[@]}"; do
7784
# Add the arguments value to the arguments array
7885
args["$lastArgument"]="$argChunk"
7986

80-
#echo "Argument Value: $argChunk"
87+
[ $ARG_DEBUG == true ] && echo "Argument Value: $argChunk"
8188

8289
lastWasArgument=0
8390
fi
8491
done
8592

86-
echo "---------"
87-
for k in "${!args[@]}"
93+
[ $ARG_DEBUG == true ] && echo "Argument array:"
94+
[ $ARG_DEBUG == true ] && for k in "${!args[@]}"
8895
do
8996
echo "ARG: $k = ${args[$k]}"
9097
done

0 commit comments

Comments
 (0)