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

Option with split property should not split quoted strings #379

Closed
MarkusKramer opened this issue May 16, 2018 · 5 comments
Closed

Option with split property should not split quoted strings #379

MarkusKramer opened this issue May 16, 2018 · 5 comments
Milestone

Comments

@MarkusKramer
Copy link

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

My class:

public class MyCommand implements Callable<Object> {
    @Option(names = {"-p", "--parameter"}, split = ",")
    @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 for AppOptions, instead I'm only getting -Dspring.profiles.active=foo

I believe the correct behavior should be that the split operation is not applied on quoted strings.

@remkop
Copy link
Owner

remkop commented May 16, 2018

I was wondering if it would be possible to accomplish this with a more sophisticated split regular expression...

@remkop
Copy link
Owner

remkop commented Jul 12, 2018

The following regex can be used to split on commas that are not inside a quoted region:

(,)(?=(?:[^"]|"[^"]*")*$)

Example:

String val = "\"-Dspring.profiles.active=foo,bar -Dspring.mail.host=smtp.mailtrap.io\",OtherOptions=\"\"";
String regex = "(,)(?=(?:[^\"]|\"[^\"]*\")*$)";
String[] parts = val.split(regex);

for (String p : parts) {
    System.out.println(p);
}

Prints

"-Dspring.profiles.active=foo,bar -Dspring.mail.host=smtp.mailtrap.io"
OtherOptions=""

The alternative is to simply gather the values and parse them seperately with a CSV parser in your program.

@remkop remkop added the status: declined ❌ A suggestion or change that we don't feel we should currently apply label Jul 19, 2018
@remkop remkop closed this as completed Jul 19, 2018
@remkop remkop reopened this Oct 20, 2018
@remkop
Copy link
Owner

remkop commented Oct 20, 2018

I closed this too soon.
I think I see a way to accomplish this now.

@remkop remkop added this to the 3.7 milestone Oct 20, 2018
@remkop remkop removed the status: declined ❌ A suggestion or change that we don't feel we should currently apply label Oct 20, 2018
@remkop remkop closed this as completed in cfa8aa3 Oct 21, 2018
@remkop
Copy link
Owner

remkop commented Oct 21, 2018

Fixed in master.
This will be in the upcoming 3.7 release.

@remkop
Copy link
Owner

remkop commented Jan 7, 2019

@MarkusKramer please be aware that the next picocli release (3.9.1) will include a change in the quoting behaviour to satisfy this use case (#594) where map options can have quoted keys with embedded '=' characters.

Please let me know if you have any concerns or suggestions.

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

No branches or pull requests

2 participants