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

Add method ArgSpec.hasInitialValue #355

Closed
remkop opened this issue Apr 13, 2018 · 0 comments
Closed

Add method ArgSpec.hasInitialValue #355

remkop opened this issue Apr 13, 2018 · 0 comments

Comments

@remkop
Copy link
Owner

remkop commented Apr 13, 2018

Problem

The same CommandSpec instance may be used multiple times to parse different command lines. The previous values should not be retained when parsing new input.

Analysis

Annotated fields always have an initial value, so even if no default value is specified, their value can be reset to the initial value before parsing. Annotated methods are different: we cannot always query them for their initial value. Setting a default value on the annotation solves the problem but requiring users to set a default in order to get consistent behaviour seems draconian. Is it possible to avoid this?

Interface with proxied annotated getters

If the option is not specified, picocli will not modify the value. Hence the previous value (if any) is returned. Picocli can ask the IGetter for an initial value, and always call the ISetter with this initial value before parsing. This often gives good results, but there may be cases where custom IGetter implementations may not want this behaviour.

Concrete class with annotated setters

Again, if the option is not specified, picocli will not modify the value, so the previous value (if any) is returned. However, since only the setter method is annotated, picocli cannot query the IGetter for an initial value: the IGetter cannot see the value of the field where the setter writes the value to...

In this case, the IGetter or picocli could make up some initial value, but this would result in the actual initial values being overwritten by made-up values. This is often undesirable.

For annotated setters we do not want to call the ISetter with a made-up initial value before parsing.

Conclusion

Add a boolean value ArgSpec::hasInitialValue. Picocli checks if an initial value is available before calling the ISetter with a made-up initial value before parsing.

Needed to support #182.

@remkop remkop added this to the 3.0 milestone Apr 13, 2018
@remkop remkop changed the title Add method hasInitialValue Add method ArgSpec.hasInitialValue Apr 13, 2018
@remkop remkop closed this as completed in 10027f8 Apr 13, 2018
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

1 participant