-
Notifications
You must be signed in to change notification settings - Fork 424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Options specified at method level is not reset in shell mode (via jline3). #1531
Comments
Thank you for raising this. That said, I am currently extremely busy with Log4j, so it may take me a while to get to this. |
I did some analysis, here are my observations: With tracing on, I see the following output for the last run (when
Note that the This is the cause of the issue: in the absence of an explicit For annotated fields, it is clear what their "initial value" is. See this example: class C {
@Option(names = "-a") int a; // initial value is primitive int 0 (zero)
@Option(names = "-b") int b = 123; // initial value is primitive 123
@Option(names = "-c") String c; // initial value is null
@Option(names = "-d") String d = "abc"; // initial value is "abc"
} For annotated methods, it is unknowable what the initial value should be (picocli does not know where the value is stored), so at the time that the annotated setter method options feature was introduced, I decided to do nothing. I need to think a bit on what to do. Maybe setting to |
The solution I am thinking of now requires annotated setter method options to explicitly set class M {
String x;
@Option(names = "-x", defaultValue = Option.NULL) // reset to null between parseArgs invocations
void setX(String x) { this.x = x; }
} Unfortunately this does not work correctly with picocli 4.6.2, this will be fixed in the next release. @kaushalkumar Does that meet your requirements? |
@kaushalkumar I pushed a fix for this to the master branch, can you verify? |
This has been fixed in the main branch and the fix will be included in the upcoming picocli 4.6.3 release. note to self: [WARNING]
====
Applications that reuse a single `CommandLine` instance for multiple `parseArgs` invocations should be aware of the following:
Annotated setter method options that explicitly set `defaultValue = Option.NULL_VALUE` will get a call with a `null` parameter when the command line did not include that option.
If the setter method annotation does _not_ explicitly set `defaultValue = Option.NULL_VALUE`, then this method will not be called when the command line did not include that option. This means then the value will retain the value of the previous `parseArgs` invocation, which may not be desirable. (This only happens for applications that reuse a single `CommandLine` instance for multiple `parseArgs` invocations.)
.Java
[source,java,role="primary"]
----
class App {
String x;
@Option(names = "-x", defaultValue = Option.NULL) // reset to null when option not specified
void setX(String x) { this.x = x; }
String y;
@Option(names = "-y") // retains value of previous parseArgs invocation when option not specified
void setY(String y) { this.y = y; }
}
----
.Kotlin
[source,kotlin,role="secondary"]
----
class App {
var x
@Option(names = ["-x"], defaultValue = Option.NULL) // reset to null when option not specified
fun setX(x: String) { this.x = x }
var y
@Option(names = ["-y"]) // retains value of previous parseArgs invocation when option not specified
fun setY(y: String) { this.y = y }
}
----
==== |
Closing this ticket, the documentation has been updated. |
Picocli reuse command object when used in interactive mode. While reusing it clears all the instance variable annotated with @Option/@Paramters, but does not reset the options specified at method level. It looks like Picocli does not invokes method annotated with @option unless the matching option is specified in command line. Due to this, when the command is executed with the option, then instance variable is initialized and next the command is executed without the option, then same value gets visible.
We thought of using default value attribute to get it reset, but unfortunately, in our application, we cannot predefine the default value, thus could not take this approach. Kindly check it.
Example code
Execution
The text was updated successfully, but these errors were encountered: