Skip to content

Commit 22cb040

Browse files
committed
Merge branch 'feature/assign-value-with-equals' into develop
2 parents 335adb0 + 4d34c0a commit 22cb040

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ fi
2020
# --debug 3
2121
DEBUG_LEVEL="$(argValue "debug")" # 3
2222

23+
# --max-size=1024
24+
MAX_SIZE="$(argValue "max-size")" # 1024
25+
2326
# -R 0
2427
[ "$(argValue "R")" == "0" ] && echo "Recursive depth 0"
2528

@@ -58,6 +61,11 @@ if argExists 'long-argument-name'; then
5861
# Do something awesome
5962
fi
6063

64+
# --protocol=HTTP
65+
if argExists 'protocol'; then
66+
# Do something awesome
67+
fi
68+
6169
# -O 43
6270
argExists 'r' && echo "Found the -O argument"
6371
```
@@ -92,6 +100,18 @@ newlines"
92100
--365 "days"
93101
```
94102

103+
### Long Form With Value
104+
105+
```bash
106+
--lang="en"
107+
--crawl=false
108+
--match-pattern=".+"
109+
--newline="
110+
"
111+
--UpperCase=sensitive
112+
--45="25+20"
113+
```
114+
95115
### Chained Short Form Arguments
96116

97117
```bash

argument-parser.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
regexArgShort='^-([a-zA-Z0-9])$'
55
regexArgShortChained='^-([a-zA-Z0-9]{2,})$'
66
regexArgLong='^--([a-zA-Z0-9\-]{2,})$'
7+
regexArgLongWithValue='^--([a-zA-Z0-9\-]{2,})=(.*)$'
78

89
argChunks=()
910

@@ -63,6 +64,20 @@ for argChunk in "${argChunks[@]}"; do
6364
continue;
6465
fi
6566

67+
# Check if this chunk is a long form with value argument
68+
[[ $argChunk =~ $regexArgLongWithValue ]]
69+
if [ "${BASH_REMATCH[1]}" != "" ]; then
70+
argument="${BASH_REMATCH[1]}"
71+
lastArgument="$argument"
72+
73+
# Add the argument to the arguments array
74+
args["${BASH_REMATCH[1]}"]="${BASH_REMATCH[2]}"
75+
76+
[ $ARG_DEBUG == true ] && echo "Argument (long with value): ${BASH_REMATCH[1]}=${BASH_REMATCH[2]}"
77+
78+
continue;
79+
fi
80+
6681
# Check if this chunk is a long form argument
6782
[[ $argChunk =~ $regexArgLong ]]
6883
if [ "${BASH_REMATCH[1]}" != "" ]; then

0 commit comments

Comments
 (0)