1

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.

marinier
  • 387
  • 3
  • 13

1 Answers1

0

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.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • 1
    I'm not sure exactly what the problem was, but I had made another change (setting a default value of empty string instead of null for some fields), and when I reverted that, everything worked. It just happened to manifest itself in a way that looked like a reset wasn't happening. Thanks for the explanation! – marinier Apr 01 '20 at 12:26
  • 1
    Glad it’s working for you. Don’t forget to upgrade to 4.2.0 though!! :-) I’d be happy to help if there’s any issue with that. – Remko Popma Apr 01 '20 at 12:28
  • The only issue at this point is time :) But when I get to it, I'll let you know if I run into trouble! – marinier Apr 01 '20 at 18:06
  • I doubt it would break anything if you upgrade. Most changes are backwards compatible. Potential breaking changes in 4.0 are listed here: https://github.com/remkop/picocli/releases?after=v4.0.1#4.0.0-breaking-changes If those don’t impact you then you can safely upgrade to 4.2. – Remko Popma Apr 01 '20 at 20:14