-
Notifications
You must be signed in to change notification settings - Fork 428
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
Allow Options with defaults to be always non-required #261
Comments
Oh, actually I can see that in 3.x it's possible to do that by manipulating the CommandSpec... yay ! closing non-issue :) |
Actually i spoke too soon :-( commandSpec.requiredArgs() is protected against modification, and there's no method that allows to remove something from that list. Would it be possible to make that modifiable ? |
Alternatively it would be nice for required to support default values, and not fail if a default value exists (also usage would need to support that to not include it's marker when default value exists). This solution architecturally make actually make the more sense (if there's a default value then required should always be fulfilled) |
@ymenager Very sorry for the late response. I must have accidentally switched off email notifications because I used to get emails for new issues but I did not for yours... What you are saying makes sense. Options or positional parameters that have a value (either a user specified value or a default value) should not be the cause of a "missing required parameter" error. I still need to take a closer look at your pull request but my first thought is that it is okay to change the current behaviour to the previous paragraph. If so, the |
Sure I'm happy with it being the default behavior, I'm going to change the code in the PR branch accordingly |
ok cool. Is it possible to separate this pull request from #262 ? |
Sure |
Just as note, one consequence of this change is that all primitives will never be "required" since they always have a default value |
Thanks for pointing that out. That is not good... It's getting a bit late for me so I need to sleep on it, but perhaps This could be a simple interface IDefaultValueProvider {
/** Returns the default value for an option or positional parameter.
* The returned value is converted to the type of the option/positional parameter
* via the same type converter used when populating this option/positional
* parameter from a command line argument.
*/
String defaultValue();
} Thoughts? |
Yeah, i noticed when a whole bunch of the existing tests broke after i made those changes lol Yeah that would work.. I would say both defaultValue and defaultValueProvider would give extra flexibility to handle all use cases. Ideally IDefaultValueProvider should be able to access the field value so the same behavior as today (using the field's value) could be implemented |
Something like this? interface IDefaultValueProvider {
/** Returns the default value for an option or positional parameter.
* The returned value is converted to the type of the option/positional parameter
* via the same type converter used when populating this option/positional
* parameter from a command line argument.
* @param previousValue the current value of the option or positional parameter, may be {@code null}
*/
String defaultValue(Object previousValue);
} |
Nope... I would give some way to get to the command object as well as to the field |
like interface IDefaultValueProvider {
/** Returns the default value for an option or positional parameter.
* The returned value is converted to the type of the option/positional parameter
* via the same type converter used when populating this option/positional
* parameter from a command line argument.
* @param previousValue the current value of the option or positional parameter, may be {@code null}
*/
String defaultValue(OptionSpec optionSpec);
} |
Yes that is better. Perhaps pass in an And the In addition to declarative, users may also want to set this programmatically on the |
best have it both on @command as well as @parameter / @option (the later two being given priority over the prior) |
I think it’d be good to start by adding a If someone has a good use case for having a separate DefaultProvider class on a per-option basis we can add add this attribute to I do wonder whether the
|
yup sounds good |
Would you be interested in creating a pull request for this? (Ideally a new PR without the masking stuff.) |
Sure but not sure when I'll have time, my todo list is quite large at the moment |
Understood. FYI, I've started to work on #262. |
K cool, I'll let you know when i have some time to help out |
[#216] Parsed values are no longer appended to, but instead replace, the default value of multi-value (array, Collection or Map) options and positional parameters. Thanks to [wiwie](https://github.com/wiwie) for the request. [#261] Options and positional parameters with a `defaultValue` are never required. Thanks to [ymenager](https://github.com/ymenager) for the request. [#315] Initialize ArgSpec value with `defaultValue` before parsing command line. Closes 216 Closes 261 Closes 315
This is now implemented: Note that I split out separate tickets for adding a |
It would be useful to be able to programatically make a required field to "not required" before doing the parsing.
My use case is that I have defaults which are in a configuration file, so I'd like to be able to switch a field to non-required when a default value was saved in the config file
The text was updated successfully, but these errors were encountered: