From dc1b8515699d69cc0d841a146a81ec11007b0f73 Mon Sep 17 00:00:00 2001 From: 51-code <146736881+51-code@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:52:35 +0200 Subject: [PATCH] Allow hyphen and underscore in keys in ArgsConfiguration (#37) --- .../teragrep/cnf_01/ArgsConfiguration.java | 6 ++-- .../cnf_01/ArgsConfigurationTest.java | 28 ++++++++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/teragrep/cnf_01/ArgsConfiguration.java b/src/main/java/com/teragrep/cnf_01/ArgsConfiguration.java index c86c702..09d9e47 100644 --- a/src/main/java/com/teragrep/cnf_01/ArgsConfiguration.java +++ b/src/main/java/com/teragrep/cnf_01/ArgsConfiguration.java @@ -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 asMap() throws ConfigurationException { final Map 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 ) ); diff --git a/src/test/java/com/teragrep/cnf_01/ArgsConfigurationTest.java b/src/test/java/com/teragrep/cnf_01/ArgsConfigurationTest.java index 62baf42..82bfaca 100644 --- a/src/test/java/com/teragrep/cnf_01/ArgsConfigurationTest.java +++ b/src/test/java/com/teragrep/cnf_01/ArgsConfigurationTest.java @@ -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 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 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 = { @@ -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() ); }