Skip to content
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

Map option problem when value contains '=' separator (was: How to quote / escape option values) #378

Closed
MarkusKramer opened this issue May 14, 2018 · 8 comments
Milestone

Comments

@MarkusKramer
Copy link

I would like to pass a map of options into my application like:
-p AppOptions="-Dspring.profiles.active=test -Dspring.mail.host=smtp.mailtrap.io"

picocli seems to get confused by the equals sign in the value. My class:

public class MyCommand implements Callable<Object> {
    @Option(names = {"-p", "--parameter"})
    @Getter
    Map<String, String> parameters;

    public static void main(String[] args) throws Exception {
        Object result = CommandLine.call(new MyCommand(), System.out, args);
        System.exit(result != null ? 0 : 1);
    }

    @Override
    public Object call() throws Exception {
        System.out.println(this.parameters.get("AppOptions"));
        return new Object();
    }
}

I expect it to output the entire value, but I'm only getting -Dspring.profiles.active. How can I escape the value correctly?

I don't want to make AppOptions part of my CLI directly, because I don't control the content and users should be able to pass in an arbitary set of parameters.

@remkop
Copy link
Owner

remkop commented May 14, 2018

Thank you for reporting this. Looks like you have found a bug. The problem is that for Map options, picocli currently does not handle values containing a '=' character correctly.

The fix is to call value.split("=", 2) instead of value.split("=") in CommandLine.Interpreter.splitKeyValue (line 5259). I will try to roll out a bugfix release for this in the next day or two.

@remkop
Copy link
Owner

remkop commented May 14, 2018

Note to self (Markus, please ignore): a test to reproduce the issue:

@Test
public void testMapValuesContainingSeparator() {
    class MyCommand {
        @Option(names = {"-p", "--parameter"})
        Map<String, String> parameters;
    }
    String[] args = new String[] {"-p", "AppOptions=\"-Dspring.profiles.active=test -Dspring.mail.host=smtp.mailtrap.io\""};
    MyCommand c = CommandLine.populateCommand(new MyCommand(), args);
    assertEquals("\"-Dspring.profiles.active=test -Dspring.mail.host=smtp.mailtrap.io\"", c.parameters.get("AppOptions"));

    args = new String[] {"-p", "\"AppOptions=-Dspring.profiles.active=test -Dspring.mail.host=smtp.mailtrap.io\""};
    c = CommandLine.populateCommand(new MyCommand(), args);
    assertEquals("-Dspring.profiles.active=test -Dspring.mail.host=smtp.mailtrap.io", c.parameters.get("AppOptions"));
}

@MarkusKramer
Copy link
Author

Awesome! Thanks for the very fast response. Looking forward to the fix.

@remkop remkop added this to the 3.0.1 milestone May 14, 2018
@remkop remkop changed the title How to quote / escape option values Map option problem when value contains '=' separator (was: How to quote / escape option values) May 14, 2018
@remkop remkop closed this as completed in 8e0fb8f May 14, 2018
@remkop
Copy link
Owner

remkop commented May 14, 2018

Picocli 3.0.1 is released with the fix for this bug. Thanks again for the bug report!

@remkop
Copy link
Owner

remkop commented May 15, 2018

Can you verify that this solves the problem?

@MarkusKramer
Copy link
Author

Yes, it's fixed. Thanks again!

Btw, I've noticed that it doesn't work as expected if I use

@Option(names = {"-p", "--parameter"}, split = ",")

with arguments -p AppOptions="-Dspring.profiles.active=foo,bar -Dspring.mail.host=smtp.mailtrap.io",OtherOptions=""
But it's not a big problem problem for me.

@remkop
Copy link
Owner

remkop commented May 15, 2018

Would you mind creating a new ticket for that to discuss further?

@MarkusKramer
Copy link
Author

Sure, I've created #379

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants