Skip to content
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

Fixes #2035 - include mapFallbackValue for inherited opts #2036

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -9526,6 +9526,7 @@ abstract static class Builder<T extends Builder<T>> {
setter = original.setter;
scope = original.scope;
scopeType = original.scopeType;
mapFallbackValue = original.mapFallbackValue;
originalDefaultValue = original.originalDefaultValue;
originalMapFallbackValue = original.originalMapFallbackValue;
}
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/picocli/MapOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import org.junit.contrib.java.lang.system.ProvideSystemProperty;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import org.junit.rules.TestRule;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.ParameterException;
import picocli.CommandLine.Parameters;

import java.util.Map;

import static org.junit.Assert.*;
import static picocli.CommandLine.ScopeType.INHERIT;

/**
* Verifies https://github.com/remkop/picocli/issues/1214
Expand Down Expand Up @@ -75,6 +77,40 @@ class App {
assertEquals(null, app.map.get("key"));
}

@Test
public void testInheritedOptionMapFallbackValueEmptyString() {
class App {
@Option(names = "-D", mapFallbackValue = "", scope = INHERIT) Map<String, String> map;
}
@Command(name = "sub")
class Sub {
}
App app = new App();
Sub sub = new Sub();
new CommandLine(app)
.addSubcommand(sub)
.parseArgs("sub", "-Dkey");
assertEquals(1, app.map.size());
assertEquals("", app.map.get("key"));
}

@Test
public void testInheritedOptionMapFallbackValueNull() {
class App {
@Option(names = "-D", mapFallbackValue = Option.NULL_VALUE, scope = INHERIT) Map<String, String> map;
}
@Command(name = "sub")
class Sub {
}
App app = new App();
Sub sub = new Sub();
new CommandLine(app)
.addSubcommand(sub)
.parseArgs("sub", "-Dkey");
assertEquals(1, app.map.size());
assertEquals(null, app.map.get("key"));
}

@Test
public void testPositionalMapFallbackValueNull() {
class App {
Expand All @@ -85,6 +121,23 @@ class App {
assertEquals(null, app.map.get("key"));
}

@Test
public void testInheritedPositionalMapFallbackValueNull() {
class App {
@Parameters(mapFallbackValue = Option.NULL_VALUE, scope = INHERIT) Map<String, String> map;
}
@Command(name = "sub")
class Sub {
}
App app = new App();
Sub sub = new Sub();
new CommandLine(app)
.addSubcommand(sub)
.parseArgs("sub", "key");
assertEquals(1, app.map.size());
assertEquals(null, app.map.get("key"));
}

@Test
public void testMapFallbackValueEmptyStringMultiple() {
class App {
Expand Down
Loading