Skip to content

Commit 0185687

Browse files
committed
Merge branch 'develop'
2 parents 12572ec + 22cb040 commit 0185687

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

README.md

Lines changed: 25 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
```
@@ -69,11 +77,13 @@ argExists 'r' && echo "Found the -O argument"
6977
```bash
7078
-a
7179
-X
80+
-2
7281
-b somevalue
7382
-c 38
7483
-d "some value with spaces"
7584
-e "some value with
7685
newlines"
86+
-7 "ate nine"
7787
```
7888

7989
### Long Form
@@ -86,13 +96,28 @@ newlines"
8696
--long-parameter
8797
--newline "
8898
"
99+
--1337
100+
--365 "days"
101+
```
102+
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"
89113
```
90114

91115
### Chained Short Form Arguments
92116

93117
```bash
94118
-aih # Equivalent to -a -i -h
95119
-dav 4 # Equivalent to -d -a -v 4
120+
-H3cx # Equivalent to -H -3 -c -x
96121
```
97122

98123
## Argument Order

argument-parser.sh

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#!/bin/bash
22

33
# Define the regex for matching the arguments
4-
regexArgShort='^-([a-zA-Z])$'
5-
regexArgShortChained='^-([a-zA-Z]{2,})$'
6-
regexArgLong='^--([a-zA-Z\-]{2,})$'
4+
regexArgShort='^-([a-zA-Z0-9])$'
5+
regexArgShortChained='^-([a-zA-Z0-9]{2,})$'
6+
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)