-
Notifications
You must be signed in to change notification settings - Fork 420
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
Negatable boolean @Option with default value "true" is not negated as expected #813
Comments
You raise a good point. Double negatives are always a source of confusion... If your option is called Inspired by your feedback, I updated the user manual: https://picocli.info/#_negatable_options |
@YannRobert Sorry if I wasn't clear. You can still make the option negatable, even if you give it the name @Option(names = "--no-demote-first", defaultValue = "true", negatable = true)
private boolean demoteFirst; When the user specifies I believe this is how it should work. The key is to name the option Does that make sense? |
@YannRobert I added this test. I believe it is working as expected. @Test
public void testIssue813() {
class App {
@Option(names = "--no-xxx", defaultValue = "true", negatable = true)
boolean xxx;
@Option(names = "--yyy", defaultValue = "false", negatable = true)
boolean yyy;
}
App app = new App();
new CommandLine(app).parseArgs("--xxx", "--yyy");
assertTrue(app.xxx);
assertTrue(app.yyy);
new CommandLine(app).parseArgs("--no-xxx", "--no-yyy");
assertFalse(app.xxx);
assertFalse(app.yyy);
} Is this useful to help resolve the issue? |
Hi @remkop I think your addition to the documentation is helping very much. Also you additional test is helping, but I would complete the test like this, maybe : @Test
public void testIssue813_you_probably_should_not_use_it_this_way() {
// see doc : Negatable options that are true by default
class AppDont {
@CommandLine.Option(names = "--zzz", defaultValue = "true", negatable = true)
boolean zzz;
}
AppDont app;
app = new AppDont();
new CommandLine(app).parseArgs();
assertTrue(app.zzz);
app = new AppDont();
new CommandLine(app).parseArgs("--zzz");
assertFalse(app.zzz); // don't expect true
app = new AppDont();
new CommandLine(app).parseArgs("--no-zzz");
assertTrue(app.zzz); // don't expect false
}
@Test
public void testIssue813_but_you_probably_should_do_this() {
// see doc : Negatable options that are true by default
class AppDo {
@CommandLine.Option(names = "--no-zzz", defaultValue = "true", negatable = true)
boolean zzz;
}
AppDo app;
app = new AppDo();
new CommandLine(app).parseArgs();
assertTrue(app.zzz);
app = new AppDo();
new CommandLine(app).parseArgs("--zzz");
assertTrue(app.zzz);
app = new AppDo();
new CommandLine(app).parseArgs("--no-zzz");
assertFalse(app.zzz);
} |
@YannRobert Excellent, glad to hear that! If you like picocli, please star the project on GitHub and tell your friends! 😅 |
P.S. Your contribution is mentioned in the release notes for picocli 4.0.5. |
Thanks 👍 |
Hi
I have a boolean
Option
that I want to betrue
by default.I also want the user to be able to set this option to
false
.I want the
Option
name to be explicit enough to be self-documenting (actual code provides a description though)The
--(no-)option
modifier seems like a perfect fit at first. Even if--option
is redundant with default value.In order to do this, I use the following code :
Using latest version as of now (version 4.0.4)
When default value is
true
:Option
does not change the value tofalse
.Option
does change the value tofalse
I think this is counter intuitive.
Is this the expected behavior ?
In order to avoid this, I have to change the code to :
make the
Option
notnegatable
(this is my favorite)or
make the
Option
negatable
and alsorequired
(without any default value otherwiserequired
is ignored)For Options with a
false
default value, we do not observe this.This means that usage is not consistant depending on the default value.
The text was updated successfully, but these errors were encountered: