Skip to content

Commit 2944add

Browse files
committed
[#1993] Bugfix: fallbackValue=Option.NULL_VALUE did not work for Collection or array options
Closes #1993
1 parent 692efe8 commit 2944add

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Artifacts in this release are signed by Remko Popma (6601 E5C0 8DCC BB96).
2121

2222
## <a name="4.7.2-fixes"></a> Fixed issues
2323
* [#1959] API: Add ability to enable loading resource bundles in annotation processor for tests.
24+
* [#1993] Bugfix: `fallbackValue=Option.NULL_VALUE` did not work for `Collection` or array options. Thanks to [Jiri Daněk](https://github.com/jiridanek) for raising this.
2425
* [#1975][#1976] Enhancement: Fixed `isJansiConsoleInstalled` performance issue. Thanks to [ChrisTrenkamp](https://github.com/ChrisTrenkamp) for the pull request.
2526
* [#1932] Enhancement: Move System-Rules tests to Java 5 test module; move System-Lambda tests to Java 8+ test module. Facilitate testing with recent JRE's.
2627
* [#1984] Enhancement (Kotlin): improve `paramLabel` string auto-generated from Kotlin `internal` methods which have mangled names with embedded "$". Thanks to [Ken Yee](https://github.com/kenyee) for raising this.

src/main/java/picocli/CommandLine.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14551,7 +14551,8 @@ private List<Object> consumeArguments(ArgSpec argSpec,
1455114551
String fallback = consumed == 0 && argSpec.isOption() && !OptionSpec.DEFAULT_FALLBACK_VALUE.equals(((OptionSpec) argSpec).fallbackValue())
1455214552
? ((OptionSpec) argSpec).fallbackValue()
1455314553
: null;
14554-
if (fallback != null && (args.isEmpty() || !varargCanConsumeNextValue(argSpec, args.peek())
14554+
boolean hasFallback = fallback != null || (argSpec.isOption() && Option.NULL_VALUE.equals(((OptionSpec) argSpec).originalFallbackValue));
14555+
if (hasFallback && (args.isEmpty() || !varargCanConsumeNextValue(argSpec, args.peek())
1455514556
|| (!canConsumeOneArgument(argSpec, lookBehind, alreadyUnquoted, arity, consumed, args.peek(), argDescription)))) {
1455614557
args.push(fallback);
1455714558
}

src/test/java/picocli/FallbackTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import picocli.CommandLine.ParameterException;
1212
import picocli.CommandLine.Parameters;
1313

14+
import java.util.Arrays;
1415
import java.util.Collection;
1516
import java.util.Collections;
1617
import java.util.List;
@@ -185,4 +186,45 @@ public void testIssue1904CollectionFallback_positional_map() {
185186
assertEquals(Collections.singletonList(Issue1904.DebugFacility.DEFAULT), obj.facilities);
186187
assertEquals(Collections.singletonMap(Issue1904.DebugFacility.FALLBACK, "1"), obj.map);
187188
}
189+
190+
@Command
191+
static class Issue1993 {
192+
@Option(names = {"--list"}, arity = "0..1", fallbackValue = CommandLine.Option.NULL_VALUE)
193+
List<String> list;
194+
195+
@Option(names = {"--array"}, arity = "0..1", fallbackValue = CommandLine.Option.NULL_VALUE)
196+
String[] array;
197+
198+
@Option(names = {"--map"}, arity = "0..1", fallbackValue = "KEY="+CommandLine.Option.NULL_VALUE)
199+
Map<String, String> map;
200+
}
201+
202+
@Test
203+
public void testIssue1993List() {
204+
Issue1993 main = new Issue1993();
205+
CommandLine commandLine = new CommandLine(main);
206+
commandLine.parseArgs("--list", "--list", "pepa");
207+
208+
assertEquals(Arrays.asList(null, "pepa"), main.list);
209+
}
210+
211+
@Test
212+
public void testIssue1993Array() {
213+
Issue1993 main = new Issue1993();
214+
CommandLine commandLine = new CommandLine(main);
215+
commandLine.parseArgs("--array", "--array", "FOO");
216+
217+
assertArrayEquals(new String[]{null, "FOO"}, main.array);
218+
}
219+
220+
@Test
221+
public void testIssue1993Map() {
222+
Issue1993 main = new Issue1993();
223+
CommandLine commandLine = new CommandLine(main);
224+
commandLine.parseArgs("--map", "--map", "FOO=123");
225+
226+
// Should this sentinel value be replaced with Java `null`?
227+
Map<String, String> expected = TestUtil.mapOf("KEY", CommandLine.Option.NULL_VALUE, "FOO", "123");
228+
assertEquals(expected, main.map);
229+
}
188230
}

0 commit comments

Comments
 (0)