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

Allow user to overwrite Collection field default value #216

Closed
wiwie opened this issue Oct 26, 2017 · 4 comments
Closed

Allow user to overwrite Collection field default value #216

wiwie opened this issue Oct 26, 2017 · 4 comments
Milestone

Comments

@wiwie
Copy link

wiwie commented Oct 26, 2017

Right now, if an option field is a Collection and is initialized with a default value in the code, and at the same time the user code is invoked with parameters for that collection field, these parameters are appended to the existing collection.

However, in my eyes it would make more sense to reinitialize the field with a new collection only containing the user-passed parameters. This is at least, what I expected.

Right now, there is no way of me saying "use only the parameters I passed in, and forget about the default one".

@remkop
Copy link
Owner

remkop commented Oct 26, 2017

Yes, that makes sense. It will require some work to keep track of the original default values so we only replace the original collection with a new collection when the first user-specified option is matched, and for subsequent options add to (and don't replace) the new collection.

@wiwie
Copy link
Author

wiwie commented Oct 26, 2017

true, that was an oversight I had in the temporary "fix" I incorporated in my fork. Any idea, how to achieve this? Maybe introduce a boolean flag for each option, whether the current field value is user-specified?

@remkop
Copy link
Owner

remkop commented Oct 27, 2017

If you want to create a pull request, perhaps add a Set<Field> field in Interpreter with fields that have been modified during parsing? Clear the set at the start of parsing, and check if it already contains the field before creating a new collection or array? Any time a field is modified while parsing the command line, add the field to the set.

Otherwise (without modifying the code), the workaround is similar to #213: keep the default value null, document the default value in the option description and initialize the default in the application if the user did not specify any values.

Note that this use case (a collection or array with multiple default values) is not covered nicely by JCommander's IDefaultProvider, so still thinking about what would be a nice way to do #213...

@remkop remkop added this to the 2.1.0 milestone Nov 2, 2017
@remkop remkop modified the milestones: 2.1.0, 2.2.0 Nov 28, 2017
@remkop remkop modified the milestones: 3.0, 3.1 Mar 9, 2018
remkop added a commit that referenced this issue Apr 3, 2018
[#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
@remkop
Copy link
Owner

remkop commented Apr 3, 2018

Implemented: parsed values are no longer appended to, but instead replace, the default value of multi-value (array, Collection or Map) options and positional parameters.

@remkop remkop closed this as completed Apr 3, 2018
@remkop remkop modified the milestones: 3.1, 3.0 Apr 3, 2018
remkop added a commit that referenced this issue Apr 3, 2018
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