Skip to content

Commit

Permalink
Allow hyphen and underscore in keys in ArgsConfiguration (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
51-code authored Feb 18, 2025
1 parent a1395a1 commit dc1b851
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/teragrep/cnf_01/ArgsConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,21 @@ public ArgsConfiguration(final String[] args) {
* Produces a Map of configurations from the args.
*
* @return immutable map of the args
* @throws ConfigurationException If the args are not given in the format of this regex: ([a-z]+)(=.+)
* @throws ConfigurationException If the args are not given in the format of this regex: ([A-Za-z.\-_]+)(=.+)
*/
@Override
public Map<String, String> asMap() throws ConfigurationException {
final Map<String, String> map = new HashMap<>();

if (args.length != 0) {
final Pattern ptn = Pattern.compile("([A-Za-z.]+)(=.+)");
final Pattern ptn = Pattern.compile("([A-Za-z.\\-_]+)(=.+)");
for (final String arg : args) {
final Matcher matcher = ptn.matcher(arg);
if (!matcher.matches() || matcher.group(1) == null | matcher.group(2) == null) {
throw new ConfigurationException(
String
.format(
"Can't parse argument '%s'. Args have to be given in \"key=value\" format.",
"Can't parse argument '%s'. It might contain an unsupported character or is not given in \"key=value\" format.",
arg
)
);
Expand Down
28 changes: 27 additions & 1 deletion src/test/java/com/teragrep/cnf_01/ArgsConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,32 @@ public void testDotArgs() {
Assertions.assertEquals("value", map.get("example.option"));
}

@Test
public void testUnderscoreArgs() {
String[] args = {
"example_option=value"
};
ArgsConfiguration config = new ArgsConfiguration(args);
Map<String, String> map = Assertions.assertDoesNotThrow(config::asMap);

Assertions.assertEquals(1, map.size());
Assertions.assertTrue(map.containsKey("example_option"));
Assertions.assertEquals("value", map.get("example_option"));
}

@Test
public void testHyphenArgs() {
String[] args = {
"example-option=value"
};
ArgsConfiguration config = new ArgsConfiguration(args);
Map<String, String> map = Assertions.assertDoesNotThrow(config::asMap);

Assertions.assertEquals(1, map.size());
Assertions.assertTrue(map.containsKey("example-option"));
Assertions.assertEquals("value", map.get("example-option"));
}

@Test
public void testInvalidArgs() {
String[] args = {
Expand All @@ -111,7 +137,7 @@ public void testInvalidArgs() {

Assertions
.assertEquals(
"Can't parse argument 'foo'. Args have to be given in \"key=value\" format.",
"Can't parse argument 'foo'. It might contain an unsupported character or is not given in \"key=value\" format.",
exception.getMessage()
);
}
Expand Down

0 comments on commit dc1b851

Please sign in to comment.