Skip to content

Commit ece63f3

Browse files
committed
added tests
1 parent 915c5bf commit ece63f3

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,7 @@ public <T> T getParsedOptionValue(final Option option, final T defaultValue) thr
469469
*/
470470
@SuppressWarnings("unchecked")
471471
public <T> T getParsedOptionValue(final Option option, final Supplier<T> defaultValue) throws ParseException {
472-
if (option == null) {
473-
return null;
474-
}
475-
final String res = getOptionValue(option);
472+
final String res = option == null ? null : getOptionValue(option);
476473

477474
try {
478475
return res == null ? defaultValue.get() : (T) option.getConverter().apply(res);

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,26 @@ public void testGetParsedOptionValueWithOption() throws Exception {
165165
assertEquals("foo", cmd.getParsedOptionValue(optF));
166166
}
167167

168+
@Test
169+
public void testGetParsedOptionValueUsingDefault() throws Exception {
170+
final Options options = new Options();
171+
final Option optI = Option.builder("i").hasArg().type(Number.class).build();
172+
final Option optF = Option.builder("f").hasArg().build();
173+
options.addOption(optI);
174+
options.addOption(optF);
175+
176+
final CommandLineParser parser = new DefaultParser();
177+
final CommandLine cmd = parser.parse(options, new String[] {"-i", "123"});
178+
179+
assertEquals(123, ((Number) cmd.getParsedOptionValue(optI)).intValue());
180+
assertEquals("foo", cmd.getParsedOptionValue(optF, "foo"));
181+
assertEquals("foo", cmd.getParsedOptionValue(optF, ()->"foo"));
182+
assertEquals("foo", cmd.getParsedOptionValue("f", "foo"));
183+
assertEquals("foo", cmd.getParsedOptionValue("f", ()->"foo"));
184+
assertEquals("foo", cmd.getParsedOptionValue('f', "foo"));
185+
assertEquals("foo", cmd.getParsedOptionValue('f', ()->"foo"));
186+
}
187+
168188
@Test
169189
public void testNullhOption() throws Exception {
170190
final Options options = new Options();

0 commit comments

Comments
 (0)