-
Notifications
You must be signed in to change notification settings - Fork 425
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
Set optional parameters to empty container if type is array or collection #286
Comments
You are correct that this is the current behaviour: fields are only instantiated if picocli finds a matching argument on the command line. For single-value fields, this allows applications to detect which options and parameters were specified on the command line, and which ones were not. For consistency I kept the same behaviour with multi-value fields (arrays and collections). Overall, picocli can be improved in the area of assigning default values. I recently started thinking about this more after discussion in issue #261. It seems to me that your request falls into the category of "picocli needs better defaulting", so is related to #261. I can easily imagine a Question though, if the main concern is avoiding null-checking, why not simply instantiate the field with the declaration, like this: @Parameters(arity = "0..*", description = "tbd")
private Set<Type> someParams = new LinkedHashSet<>(); |
I didn't know that this was possible because I do not know when picocli is going to set the command's members. So, I understand that I could specify "default values" directly as plain old Java member initializer? Picocli will not touch them if it finds no match? This would be acceptable for me (may be a good point to include into the documentation though). FYI: My whole point is somewhat influenced by Josh Bloch's Effective Java Item 54: Return empty collections or arrays, not nulls. |
Yes, that is correct. Field initializers are currently the only way to provide default values. Thanks for pointing out that the documentation for this can be improved, I'll take a look! |
I like! Thx for the quick answers. |
Glad to hear that solved the issue. Are you okay to close this ticket? |
The user manual has been updated with better docs for the 2.3 release. |
Hi there,
I recently had the following situation:
@Parameters(arity = "0..*", description = "tbd") private Set<Type> someParams;
If I do not specify any params at command line,
someParams
isnull
which is a bit surprising / annoying when it comes to working with arrays / collections.The Documentation says:
Which does not seem to be the case if no params are specified.
Is this the default picocli behavior? Do I miss something?
I would like picocli to create an empty array / collection in this case. This helps to keep the code simple and comprehensible as checking for
null
on container members is odd and causes boiler plate.Thank you very much for any input on this.
The text was updated successfully, but these errors were encountered: