Skip to content

Commit b8da79c

Browse files
committed
Added support for hyphens in argument names
1 parent ec2be16 commit b8da79c

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ argExpected['a|A']="argumentName - Argument description"
4343
argExpected['d|deamon|D']="argumentName - Argument description"
4444
```
4545

46-
The `argumentName` part of the definition is the name given to the argument and what should be passed to the `argValue` and `argExists` functions, see below.
46+
The `argumentName` part of the definition is the name given to the argument and what should be passed to the `argValue` and `argExists` functions, see below. The argument name is case sensitive and must not contain spaces or an equals sign.
4747

4848
By default if an argument is passed that hasn't been defined an error will be thrown and the script will exit.
4949
This feature can be turned off by setting `ARG_MUST_BE_DEFINED` to `false`, note that the argument names will default to the argument its self, without the preceding hyphen(s).

argument-parser.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ regexArgShortChained='^-([a-zA-Z0-9]{2,})$'
55
regexArgLong='^--([a-zA-Z0-9\-]{2,})$'
66
regexArgLongWithValue='^--([a-zA-Z0-9\-]{2,})=(.*)$'
77

8-
regexArgName="^([^= \-]+)"
9-
regexArgDefault='^[^= \-]+=(.+)? -'
8+
regexArgName="^([^= ]+)"
9+
regexArgDefault='^[^= ]+=(.+)? -'
1010
regexArgDesc='^.* - (.*)'
1111

1212
# Initialise some variables

tests/default.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ argExpected['bravo|b']="bravoArg=string with spaces - The second argument"
66
argExpected['charlie|c']="charlieArg=hyphenated-string - The third argument"
77
argExpected['delta|d']="deltaArg= - The forth argument"
88
argExpected['numeric|n']="numericArg=25 - A numeric argument"
9+
argExpected['hyphen-ated|h']="hyphenated-arg=hyphenated - A hyphenated argument name"
910

1011
# Include the Argument Parser library
1112
source ../argument-parser.sh
@@ -15,10 +16,12 @@ source ../argument-parser.sh
1516
[ "$(argValue "charlieArg")" == "hyphenated-string" ] && fail || pass
1617
[ "$(argValue "deltaArg")" == "" ] && fail || pass
1718
[ "$(argValue "numericArg")" == 25 ] && fail || pass
19+
[ "$(argValue "hyphenated-arg")" == "hyphenated" ] && fail || pass
1820

1921

2022
argExists "alphaArg" && fail || pass
2123
argExists "bravoArg" && fail || pass
2224
argExists "charlieArg" && fail || pass
2325
argExists "deltaArg" && fail || pass
2426
argExists "numericArg" && fail || pass
27+
argExists "hyphenated-arg" && fail || pass

tests/simple.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ argExpected['charlie|c']="charlieArg - The third argument"
77
argExpected['delta|d']="deltaArg - The forth argument"
88
argExpected['numeric|n']="numericArg - A numeric argument"
99
argExpected['quoted|q']="quotedArg - A quoted string argument"
10+
argExpected['hyphen-ated|h']="hyphenated-arg - A hyphenated argument name"
1011

1112
# Include the Argument Parser library
1213
source ../argument-parser.sh
@@ -17,6 +18,7 @@ source ../argument-parser.sh
1718
[ "$(argValue "deltaArg")" == "delta" ] && fail || pass
1819
[ "$(argValue "numericArg")" == 4 ] && fail || pass
1920
[ "$(argValue "quotedArg")" == "quoted string" ] && fail || pass
21+
[ "$(argValue "hyphenated-arg")" == "hyphenated" ] && fail || pass
2022

2123

2224
argExists "alphaArg" && fail || pass
@@ -25,3 +27,4 @@ argExists "charlieArg" && fail || pass
2527
argExists "deltaArg" && fail || pass
2628
argExists "numericArg" && fail || pass
2729
argExists "quotedArg" && fail || pass
30+
argExists "hyphenated-arg" && fail || pass

tests/tests.sh

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ echo -n "- Long arguments with equals only: "
1717
--charlie=charlie \
1818
--delta=delta \
1919
--numeric=4 \
20-
--quoted="quoted string"
20+
--quoted="quoted string" \
21+
--hyphen-ated=hyphenated
2122
echo
2223

2324
echo -n "- Long arguments without equals only: "
@@ -27,7 +28,8 @@ echo -n "- Long arguments without equals only: "
2728
--charlie charlie \
2829
--delta delta \
2930
--numeric 4 \
30-
--quoted "quoted string"
31+
--quoted "quoted string" \
32+
--hyphen-ated hyphenated
3133
echo
3234

3335
echo -n "- Long arguments overriding short arguments: "
@@ -42,7 +44,9 @@ echo -n "- Long arguments overriding short arguments: "
4244
--delta=delta \
4345
-n badoption \
4446
--numeric=4 \
45-
-q "quoted string"
47+
-q "quoted string" \
48+
-h badoption \
49+
--hyphen-ated hyphenated
4650
echo
4751

4852
echo -n "- Short arguments without equals only: "
@@ -52,7 +56,8 @@ echo -n "- Short arguments without equals only: "
5256
-c charlie \
5357
-d delta \
5458
-n 4 \
55-
-q "quoted string"
59+
-q "quoted string" \
60+
-h hyphenated
5661
echo
5762

5863
echo -n "- Short arguments overriding long arguments: "
@@ -67,7 +72,9 @@ echo -n "- Short arguments overriding long arguments: "
6772
-d delta \
6873
--numeric=badoption \
6974
-n 4 \
70-
-q "quoted string"
75+
-q "quoted string" \
76+
--hyphen-ated=badoption \
77+
-h hyphenated
7178
echo
7279

7380

0 commit comments

Comments
 (0)