问题描述
我正在尝试此处中所述,改善许多命令的性能一个>.该解决方案的一部分是重用相同的Commandline
对象,该对象包装了其字段包含picocli批注的对象.在某些情况下,需要在每次使用之间重置"该对象,因为不是每个命令都设置了每个字段,并且如果某些字段具有先前实例的旧值,则会导致错误的行为.我尝试使用@Option
批注的defaultValue
属性,但似乎没有在每次调用时重置字段值.
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.
我是否必须编写自己的reset()
方法,还是有办法用picocli完成此操作?
Do I have to write my own reset()
method, or is there a way to accomplish this with picocli?
请注意,我仍在使用picocli 3.9.6,但我确实打算升级到4.x,因此,如果这是解决方案的一部分,那就可以了.
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.
推荐答案
在解析命令行参数之前,Picocli会自动将所有带有@Option
和@Parameter
注释的字段重置为默认值(可能是null
). (这就是允许重新使用CommandLine
对象的原因.)不需要用户定义的reset
方法.
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.
您能否提供一个可以重现此问题的示例?
Can you provide an example that reproduces this issue?
或者,当您在应用程序中重现该问题时,是否可以使用系统属性-Dpicocli.trace=DEBUG
运行该问题并发布输出?
Alternatively, when you reproduce the issue in your application, can you run it with system property -Dpicocli.trace=DEBUG
and post the output?
请升级到4.x,最新版本的picocli具有许多错误修复和新功能,并且总体而言要好得多.但是,很长一段时间以来,重置"行为一直是picocli的一部分,当然在3.x版本中,甚至可能更早.
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.
这篇关于重置picocli选项字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!