Skip to content

Commit

Permalink
#147 Toggle boolean flags instead of setting to true
Browse files Browse the repository at this point in the history
Closes #147
  • Loading branch information
remkop committed Jul 4, 2017
1 parent 2c4d909 commit 1bdbae7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions RELEASE-NOTES.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
= picocli Release Notes

== 0.9.8 - Bugfix and enhancements release for public review. API may change.

* #147 Toggle boolean flags instead of setting to `true`

== 0.9.7 - Bugfix and enhancements release for public review. API may change.

* #127 Added support for nested sub-subcommands
Expand Down
4 changes: 3 additions & 1 deletion docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ The below example shows options with one or more names, options that take an opt
----
class Tar {
@Option(names = "-c", description = "create a new archive")
boolean extract;
boolean create;
@Option(names = { "-f", "--file" }, paramLabel = "ARCHIVE", description = "the archive file")
File archive;
Expand Down Expand Up @@ -1110,6 +1110,8 @@ Note that the `CommandLine.run` convenience method cannot be used with subcomman


=== Boolean Parameters
By default the value of a boolean field is toggled to its logical negative when the field's option is specified on the command line.

It is possible to let end users explicitly specify "true" or "false" as a parameter for a boolean option by defining an explicit <<Arity>> attribute. A boolean option with `arity = "0..1"` accepts zero to one parameters, `arity = "1"` means the option _must_ have one parameter. For example:

[source, java]
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,8 @@ private int applyValueToSingleValuedField(Field field,
if (value != null) {
args.push(value); // we don't consume the value
}
value = "true"; // just specifying the option name sets the boolean to true
Boolean currentValue = (Boolean) field.get(command);
value = String.valueOf(currentValue == null ? true : !currentValue); // #147 toggle existing boolean value
}
}
if (noMoreValues && value == null) {
Expand Down
19 changes: 18 additions & 1 deletion src/test/java/picocli/CommandLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2705,7 +2705,7 @@ class App {
assertEquals("2", ((App) commandLine.getCommand()).string);

commandLine = new CommandLine(new App()).setOverwrittenOptionsAllowed(true);
commandLine.parse("-v", "--verbose");
commandLine.parse("-v", "--verbose", "-v"); // F -> T -> F -> T
assertEquals(true, ((App) commandLine.getCommand()).bool);
}

Expand All @@ -2728,4 +2728,21 @@ class A {
}
commandLine.parse("-u", "foo", "-p", "abc");
}

@Test
public void testToggleBooleanValue() {
class App {
@Option(names = "-a") boolean primitiveFalse = false;
@Option(names = "-b") boolean primitiveTrue = true;
@Option(names = "-c") Boolean objectFalse = false;
@Option(names = "-d") Boolean objectTrue = true;
@Option(names = "-e") Boolean objectNull = null;
}
App app = CommandLine.populateCommand(new App(), "-a -b -c -d -e".split(" "));
assertTrue(app.primitiveFalse);
assertFalse(app.primitiveTrue);
assertTrue(app.objectFalse);
assertFalse(app.objectTrue);
assertTrue(app.objectNull);
}
}

0 comments on commit 1bdbae7

Please sign in to comment.