Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixed style issues and added test for null supplier
  • Loading branch information
Claudenw committed Feb 16, 2024
commit bbd14e6f9beebbeeaa5008fabcfbc91725ba60a7
7 changes: 5 additions & 2 deletions src/main/java/org/apache/commons/cli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ public <T> T getParsedOptionValue(final char opt, final Supplier<T> defaultValue
* @since 1.5.0
*/
public <T> T getParsedOptionValue(final Option option) throws ParseException {
return getParsedOptionValue(option, ()-> null);
return getParsedOptionValue(option, () -> null);
}

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

try {
return res == null ? defaultValue.get() : (T) option.getConverter().apply(res);
if (res == null) {
return defaultValue == null ? null : defaultValue.get();
}
return (T) option.getConverter().apply(res);
} catch (final Throwable e) {
throw ParseException.wrap(e);
}
Expand Down
19 changes: 16 additions & 3 deletions src/test/java/org/apache/commons/cli/CommandLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import static org.junit.Assert.assertNull;

import java.util.Properties;
import java.util.function.Supplier;

import org.junit.Test;

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

@Test
public void testGetParsedOptionValueWithNullSupplier() throws Exception {
final Options options = new Options();
options.addOption(Option.builder("i").hasArg().type(Number.class).build());

final CommandLineParser parser = new DefaultParser();
final CommandLine cmd = parser.parse(options, new String[0]);
final Supplier<Number> supplier = null;

assertNull(cmd.getParsedOptionValue('i', supplier));
}

@Test
public void testGetParsedOptionValueWithOption() throws Exception {
final Options options = new Options();
Expand Down Expand Up @@ -178,11 +191,11 @@ public void testGetParsedOptionValueUsingDefault() throws Exception {

assertEquals(123, ((Number) cmd.getParsedOptionValue(optI)).intValue());
assertEquals("foo", cmd.getParsedOptionValue(optF, "foo"));
assertEquals("foo", cmd.getParsedOptionValue(optF, ()-> "foo"));
assertEquals("foo", cmd.getParsedOptionValue(optF, () -> "foo"));
assertEquals("foo", cmd.getParsedOptionValue("f", "foo"));
assertEquals("foo", cmd.getParsedOptionValue("f", ()-> "foo"));
assertEquals("foo", cmd.getParsedOptionValue("f", () -> "foo"));
assertEquals("foo", cmd.getParsedOptionValue('f', "foo"));
assertEquals("foo", cmd.getParsedOptionValue('f', ()-> "foo"));
assertEquals("foo", cmd.getParsedOptionValue('f', () -> "foo"));
Comment thread
garydgregory marked this conversation as resolved.
}

@Test
Expand Down