Skip to content

Add method ArgSpec.hasInitialValue #355

@remkop

Description

@remkop

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions