问题
I'm trying to improve performance across many commands as described here. Part of that solution is to reuse the same Commandline
object, which is wrapping an object whose fields contain picocli annotations. In some cases, this object needs to be "reset" between uses, as not every field is set by every command, and if some fields have old values from the previous instance, then the wrong behavior results. I tried to use the defaultValue
attribute of the @Option
annotation, but it did not seem to reset the field value on each call.
Do I have to write my own reset()
method, or is there a way to accomplish this with picocli?
Note I'm still on picocli 3.9.6, but I do plan to upgrade to 4.x, so if that's part of the solution, that's fine.
回答1:
Picocli automatically resets all @Option
and @Parameter
-annotated fields to their default value (which may be null
) immediately before parsing command line arguments. (This is what allows CommandLine
objects to be reused.) There is no need for a user-defined reset
method.
I tried to use the defaultValue attribute of the @Option annotation, but it did not seem to reset the field value on each call.
Can you provide an example that reproduces this issue?
Alternatively, when you reproduce the issue in your application, can you run it with system property -Dpicocli.trace=DEBUG
and post the output?
Note I'm still on picocli 3.9.6, but I do plan to upgrade to 4.x, so if that's part of the solution, that's fine.
Please do upgrade to 4.x, the latest version of picocli has many bug fixes and new features, and is overall a lot better. However, the "reset" behaviour has been part of picocli for a long time, certainly in the 3.x version, probably even earlier.
来源:https://stackoverflow.com/questions/60959846/reset-picocli-option-field