Open kaushalkumar opened 2 years ago
Sorry, I am going to give a quick reply (I just skimmed your email and did not look at the code so apologies if I missed the point completely.)
It is possible to do some initialization with a custom ExecutionStrategy. However, resetting values in the application (like you mentioned) may be a better option.
At this point I want to keep picocli's scope limited to just command line options and positional parameters.
The point here is resetting the command-object, not the application.
This is a real problem in interactive applications (not exiting after the first command-execution but offering a sort of interactive shell to execute multiple commands), when a command is executed multiple times. The properties populated at previous executions are still set on any later execution, because picocli reuses the same command-instance. A possible workaround (for the application) is, to reset all the properties of the command-object after each execution, making it clean for the next execution.
It would be nice, if picocli would either support resetting commands (eg. optional interface IResettableCommand with a method reset() to be implemented by the command-objects and called by picocli before populating the instance), or optionally not reuse command-object-instances (but instantiate them before each command-execution).
@bbodnar The intention is for picocli to automatically reset all options and positional parameters, just prior to processing the input and (re)populating these options and positional parameters. It should already be possible to reuse a CommandLine instance.
I thought this was implemented correctly, but looking at #1531, this is apparently not working correctly for @Option
or @Parameters
-annotated setter methods.
This is probably a bug, but I have not had time yet to investigate.
It appears to me that this ticket (#1532) is asking for a mechanism for something that picocli is already supposed to do (but which is not working correctly for some reason). Rather than providing such a mechanism, I would rather fix the bug, if possible. 😅
@bbodnar Are you experiencing this issue also only with @Option
or @Parameters
-annotated setter methods?
Or is there some other additional way to reproduce this issue? (Wondering if I need to look for multiple bugs...)
@remkop Thank you for the quick reply. You are right, this is really not a generic problem, only argument-groups are affected (in my case). I have created #1536, a demo-application is attached. Members are annotated directly, no setter-methods involved.
@kaushalkumar Issue #1531 was fixed in picocli 4.6.3. Does that address your use case?
Picocli reuse command object when used in interactive mode, but doesnt not provide support to optionally reset (or not reset) instance value which are not annotated with @Options, @Parameter etc. This is required in applications where command objects require instance variable (beyond @Option, @Parameter). Although this can be handled by application by resetting the value, but this will be a workaround. An approach could be to reset all the instance fields by default and introduce a new annotation (@NonPicocliInstance - with reset as true/false) which when set on instance fields gets cleared
Example
Execution