Skip to content

Commit a9e6110

Browse files
committed
fixed style issues and added test for null supplier
1 parent a8fe1af commit a9e6110

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

src/main/java/org/apache/commons/cli/CommandLine.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ public <T> T getParsedOptionValue(final char opt, final Supplier<T> defaultValue
438438
* @since 1.5.0
439439
*/
440440
public <T> T getParsedOptionValue(final Option option) throws ParseException {
441-
return getParsedOptionValue(option, ()-> null);
441+
return getParsedOptionValue(option, () -> null);
442442
}
443443

444444
/**
@@ -472,7 +472,10 @@ public <T> T getParsedOptionValue(final Option option, final Supplier<T> default
472472
final String res = option == null ? null : getOptionValue(option);
473473

474474
try {
475-
return res == null ? defaultValue.get() : (T) option.getConverter().apply(res);
475+
if (res == null) {
476+
return defaultValue == null ? null : defaultValue.get();
477+
}
478+
return (T) option.getConverter().apply(res);
476479
} catch (final Throwable e) {
477480
throw ParseException.wrap(e);
478481
}

src/test/java/org/apache/commons/cli/CommandLineTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2222
import static org.junit.Assert.assertNull;
2323

2424
import java.util.Properties;
25+
import java.util.function.Supplier;
2526

2627
import org.junit.Test;
2728

@@ -150,6 +151,18 @@ public void testGetParsedOptionValueWithChar() throws Exception {
150151
assertEquals("foo", cmd.getParsedOptionValue('f'));
151152
}
152153

154+
@Test
155+
public void testGetParsedOptionValueWithNullSupplier() throws Exception {
156+
final Options options = new Options();
157+
options.addOption(Option.builder("i").hasArg().type(Number.class).build());
158+
159+
final CommandLineParser parser = new DefaultParser();
160+
final CommandLine cmd = parser.parse(options, new String[0]);
161+
final Supplier<Number> supplier = null;
162+
163+
assertNull(cmd.getParsedOptionValue('i', supplier));
164+
}
165+
153166
@Test
154167
public void testGetParsedOptionValueWithOption() throws Exception {
155168
final Options options = new Options();
@@ -178,11 +191,11 @@ public void testGetParsedOptionValueUsingDefault() throws Exception {
178191

179192
assertEquals(123, ((Number) cmd.getParsedOptionValue(optI)).intValue());
180193
assertEquals("foo", cmd.getParsedOptionValue(optF, "foo"));
181-
assertEquals("foo", cmd.getParsedOptionValue(optF, ()-> "foo"));
194+
assertEquals("foo", cmd.getParsedOptionValue(optF, () -> "foo"));
182195
assertEquals("foo", cmd.getParsedOptionValue("f", "foo"));
183-
assertEquals("foo", cmd.getParsedOptionValue("f", ()-> "foo"));
196+
assertEquals("foo", cmd.getParsedOptionValue("f", () -> "foo"));
184197
assertEquals("foo", cmd.getParsedOptionValue('f', "foo"));
185-
assertEquals("foo", cmd.getParsedOptionValue('f', ()-> "foo"));
198+
assertEquals("foo", cmd.getParsedOptionValue('f', () -> "foo"));
186199
}
187200

188201
@Test

0 commit comments

Comments
 (0)