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

How to pass multiple values to a Map? #753

Closed
feinstein opened this issue Jun 26, 2019 · 12 comments
Closed

How to pass multiple values to a Map? #753

feinstein opened this issue Jun 26, 2019 · 12 comments
Milestone

Comments

@feinstein
Copy link

I have a Map<String, Object> object that's supposed to hold variables such as "role","admin", "level","10", "enabled","true".

I tried to define it like:

@Option(names = {"-m", "--map"}, description = "A map of values.")
    private Map<String, Object> myMap;

And call it like this in cli:

-m role=admin,level=10,enabled=true

But this results in a map of: "role" -> "admin,level=10,enabled=true"

What's the correct way to call it in the cli to get the full map of values?

@remkop
Copy link
Owner

remkop commented Jun 26, 2019

Looks like you need to add split = "," to the option annotation.

@feinstein
Copy link
Author

Shouldn't this be the default for a Map?

@remkop
Copy link
Owner

remkop commented Jun 26, 2019

Picking good defaults is always tricky.

I’m aware that JCommander splits automatically, and this may trip up people migrating from JCommander to picocli. That would be an argument for splitting by default. (Were you using JCommander previously?)

Currently picocli doesn’t split unless you tell it to. The drawback is somewhat more verbosity, the benefit is self-documentation: it’s clear which options split and which don’t, and it’s clear what the separator is.

@feinstein
Copy link
Author

I wasn't in JCommander, I was just expecting a Map to be automatically populated as the docs imply it would. Actually the docs don't cover this particular case so it gets open to interpretation.

@remkop
Copy link
Owner

remkop commented Jun 27, 2019

I see, yes, the map docs don't have an example that uses a split regex. Let's improve that.

Do you think you'll be able to provide a patch or pull request?

@remkop remkop added this to the 4.0 milestone Jun 27, 2019
@feinstein
Copy link
Author

I don't think I can because I am not familiar with all the docs references that can be impacted by this issue, so I am afraid I might leave one out.

@remkop
Copy link
Owner

remkop commented Jun 27, 2019

I was thinking to just add an example to the section on Maps (from line 614 in https://github.com/remkop/picocli/blob/master/docs/index.adoc). I don't think there are any references.

The HTML is generated from that AsciiDoc file (run gradlew ascii to generate build/docs/html5/index.html).

@remkop
Copy link
Owner

remkop commented Jun 27, 2019

Alternatively, you can help by providing the text/example code in a comment here, and I will take care of integrating it into the user manual.

@feinstein
Copy link
Author

This is easier for me now. I would add something like this at the end of the map section:

Be aware that if you wish to parse a multi-valued Map in a single input, a split character will be required. That is, if you expect to receive something like:

-m role=admin,level=10,enabled=true

You have to define your @options configurations as:

@Option(names = {"-m", "--map"}, split = ",", description = "A map of multiple values.")
private Map<String, String> map;

Which will be converted to a Map containing {"role"="admin","level"="10","enabled"="true"}

@remkop
Copy link
Owner

remkop commented Jun 27, 2019

Cool, thank you! I'll update here after I merged this in (hopefully tonight).

@remkop remkop closed this as completed in 50b826b Jun 27, 2019
remkop added a commit that referenced this issue Jun 27, 2019
@remkop
Copy link
Owner

remkop commented Jun 27, 2019

I decided to improve the example in the Split Regex section of the user manual, and point to that section from the Maps section (and also from the Arrays and Collections section).

Thanks for calling this out! 👍
The release notes for the next release mention you as a contributor.

@feinstein
Copy link
Author

Thank you, it was my pleasure :)

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