Skip to content

Commit dc1b851

Browse files
authored
Allow hyphen and underscore in keys in ArgsConfiguration (#37)
1 parent a1395a1 commit dc1b851

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

src/main/java/com/teragrep/cnf_01/ArgsConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,21 @@ public ArgsConfiguration(final String[] args) {
6969
* Produces a Map of configurations from the args.
7070
*
7171
* @return immutable map of the args
72-
* @throws ConfigurationException If the args are not given in the format of this regex: ([a-z]+)(=.+)
72+
* @throws ConfigurationException If the args are not given in the format of this regex: ([A-Za-z.\-_]+)(=.+)
7373
*/
7474
@Override
7575
public Map<String, String> asMap() throws ConfigurationException {
7676
final Map<String, String> map = new HashMap<>();
7777

7878
if (args.length != 0) {
79-
final Pattern ptn = Pattern.compile("([A-Za-z.]+)(=.+)");
79+
final Pattern ptn = Pattern.compile("([A-Za-z.\\-_]+)(=.+)");
8080
for (final String arg : args) {
8181
final Matcher matcher = ptn.matcher(arg);
8282
if (!matcher.matches() || matcher.group(1) == null | matcher.group(2) == null) {
8383
throw new ConfigurationException(
8484
String
8585
.format(
86-
"Can't parse argument '%s'. Args have to be given in \"key=value\" format.",
86+
"Can't parse argument '%s'. It might contain an unsupported character or is not given in \"key=value\" format.",
8787
arg
8888
)
8989
);

src/test/java/com/teragrep/cnf_01/ArgsConfigurationTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,32 @@ public void testDotArgs() {
9999
Assertions.assertEquals("value", map.get("example.option"));
100100
}
101101

102+
@Test
103+
public void testUnderscoreArgs() {
104+
String[] args = {
105+
"example_option=value"
106+
};
107+
ArgsConfiguration config = new ArgsConfiguration(args);
108+
Map<String, String> map = Assertions.assertDoesNotThrow(config::asMap);
109+
110+
Assertions.assertEquals(1, map.size());
111+
Assertions.assertTrue(map.containsKey("example_option"));
112+
Assertions.assertEquals("value", map.get("example_option"));
113+
}
114+
115+
@Test
116+
public void testHyphenArgs() {
117+
String[] args = {
118+
"example-option=value"
119+
};
120+
ArgsConfiguration config = new ArgsConfiguration(args);
121+
Map<String, String> map = Assertions.assertDoesNotThrow(config::asMap);
122+
123+
Assertions.assertEquals(1, map.size());
124+
Assertions.assertTrue(map.containsKey("example-option"));
125+
Assertions.assertEquals("value", map.get("example-option"));
126+
}
127+
102128
@Test
103129
public void testInvalidArgs() {
104130
String[] args = {
@@ -111,7 +137,7 @@ public void testInvalidArgs() {
111137

112138
Assertions
113139
.assertEquals(
114-
"Can't parse argument 'foo'. Args have to be given in \"key=value\" format.",
140+
"Can't parse argument 'foo'. It might contain an unsupported character or is not given in \"key=value\" format.",
115141
exception.getMessage()
116142
);
117143
}

0 commit comments

Comments
 (0)