-
Notifications
You must be signed in to change notification settings - Fork 418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support quoted map keys with embedded '=' characters #594
Comments
That is a good suggestion, I like it! (NOTE TO SELF: apply this patch)
I did a quick experiment and with the above patch picocli should be able to support both quoted keys and quoted values. This would allow you to put as many @Test
public void testQuotedMapKeysDefault() {
class App {
@Option(names = "-e")
Map<String, String> runtimeParams = new HashMap<String, String>();
}
App app = new App();
new CommandLine(app).parseArgs("-e", "\"a=b=c\"=foo");
assertTrue(app.runtimeParams.containsKey("\"a=b=c\""));
assertEquals("foo", app.runtimeParams.get("\"a=b=c\""));
new CommandLine(app).parseArgs("-e", "\"a=b=c\"=\"x=y=z\"");
assertTrue(app.runtimeParams.containsKey("\"a=b=c\""));
assertEquals("\"x=y=z\"", app.runtimeParams.get("\"a=b=c\""));
} This gives you a map with @Test
public void testQuotedMapKeysTrimQuotes() {
class App {
@Option(names = "-e")
Map<String, String> map = new HashMap<String, String>();
}
App app = new App();
new CommandLine(app).setTrimQuotes(true).parseArgs("-e", "\"a=b=c\"=foo");
assertTrue(app.map.containsKey("a=b=c"));
assertEquals("foo", app.map.get("a=b=c"));
new CommandLine(app).setTrimQuotes(true).parseArgs("-e", "\"a=b=c\"=x=y=z");
assertTrue(app.map.keySet().toString(), app.map.containsKey("a=b=c"));
assertEquals("x=y=z", app.map.get("a=b=c"));
} One potential drawback: if @Test
public void testQuotedMapKeysAndValuesBothHaveQuotes() {
class App {
@Option(names = "-e")
Map<String, String> map = new HashMap<String, String>();
}
App app = new App();
new CommandLine(app).setTrimQuotes(true).parseArgs("-e", "\"\"a=b=c\"=\"x y z\"\"");
assertTrue(app.map.keySet().toString(), app.map.containsKey("a=b=c"));
assertEquals("x y z", app.map.get("a=b=c"));
} |
This has been pushed to master. Can you verify? |
Note: During testing I found some edge cases when the option has a class App {
@Option(names = "--option", split = ",")
Map<String, String> map;
} Some examples:
It is not yet possible to combine quoted keys (with embedded '=') with quoted key-value pairs. I raised #595 to support nested quoting in the future. |
Thanks for the prompt response and solution! I tested it and it's working. :D I need the key with the quotes, so no worries with the problems with trimming. |
Thanks for the confirmation! |
I have the following map:
If I give the key/val pair as follows, it splits the pair at the first "=" character it encounters.
$ <cmd> -e a=b=c=foo
Is there a way to indicate to the parser from where to split this pair? For example, I was expecting something like the following to work:
$ <cmd> -e "a=b=c"=foo
In both of the above cases, the pair is split as
a
andb=c=foo
.The text was updated successfully, but these errors were encountered: