File tree Expand file tree Collapse file tree 2 files changed +43
-3
lines changed
Expand file tree Collapse file tree 2 files changed +43
-3
lines changed Original file line number Diff line number Diff line change 2020# --debug 3
2121DEBUG_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
5962fi
6063
64+ # --protocol=HTTP
65+ if argExists ' protocol' ; then
66+ # Do something awesome
67+ fi
68+
6169# -O 43
6270argExists ' 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
7685newlines"
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
Original file line number Diff line number Diff line change 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
89argChunks=()
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
You can’t perform that action at this time.
0 commit comments