Skip to content

Commit 8eb7b89

Browse files
authored
[CLI-323] Added Supplier<T> defaults for getParsedOptionValue (#229)
* Added Supplier<T> defaults for getParsedOptionValue * added tests * Fixed test issues * fixed style issues and added test for null supplier * added additional supplier testing
1 parent 6b86f40 commit 8eb7b89

2 files changed

Lines changed: 87 additions & 8 deletions

File tree

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

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,21 @@ public <T> T getParsedOptionValue(final char opt, final T defaultValue) throws P
412412
return getParsedOptionValue(String.valueOf(opt), defaultValue);
413413
}
414414

415+
/**
416+
* Gets a version of this {@code Option} converted to a particular type.
417+
*
418+
* @param opt the name of the option.
419+
* @param defaultValue the default value to return if opt is not set.
420+
* @param <T> The return type for the method.
421+
* @return the value parsed into a particular object.
422+
* @throws ParseException if there are problems turning the option value into the desired type
423+
* @see PatternOptionBuilder
424+
* @since 1.7.0
425+
*/
426+
public <T> T getParsedOptionValue(final char opt, final Supplier<T> defaultValue) throws ParseException {
427+
return getParsedOptionValue(String.valueOf(opt), defaultValue);
428+
}
429+
415430
/**
416431
* Gets a version of this {@code Option} converted to a particular type.
417432
*
@@ -423,7 +438,7 @@ public <T> T getParsedOptionValue(final char opt, final T defaultValue) throws P
423438
* @since 1.5.0
424439
*/
425440
public <T> T getParsedOptionValue(final Option option) throws ParseException {
426-
return getParsedOptionValue(option, null);
441+
return getParsedOptionValue(option, () -> null);
427442
}
428443

429444
/**
@@ -437,15 +452,30 @@ public <T> T getParsedOptionValue(final Option option) throws ParseException {
437452
* @see PatternOptionBuilder
438453
* @since 1.7.0
439454
*/
440-
@SuppressWarnings("unchecked")
441455
public <T> T getParsedOptionValue(final Option option, final T defaultValue) throws ParseException {
442-
if (option == null) {
443-
return null;
444-
}
445-
final String res = getOptionValue(option);
456+
return getParsedOptionValue(option, () -> defaultValue);
457+
}
458+
459+
/**
460+
* Gets a version of this {@code Option} converted to a particular type.
461+
*
462+
* @param option the name of the option.
463+
* @param defaultValue the default value to return if opt is not set.
464+
* @param <T> The return type for the method.
465+
* @return the value parsed into a particular object.
466+
* @throws ParseException if there are problems turning the option value into the desired type
467+
* @see PatternOptionBuilder
468+
* @since 1.7.0
469+
*/
470+
@SuppressWarnings("unchecked")
471+
public <T> T getParsedOptionValue(final Option option, final Supplier<T> defaultValue) throws ParseException {
472+
final String res = option == null ? null : getOptionValue(option);
446473

447474
try {
448-
return res == null ? defaultValue : (T) option.getConverter().apply(res);
475+
if (res == null) {
476+
return defaultValue == null ? null : defaultValue.get();
477+
}
478+
return (T) option.getConverter().apply(res);
449479
} catch (final Throwable e) {
450480
throw ParseException.wrap(e);
451481
}
@@ -480,6 +510,21 @@ public <T> T getParsedOptionValue(final String opt, final T defaultValue) throws
480510
return getParsedOptionValue(resolveOption(opt), defaultValue);
481511
}
482512

513+
/**
514+
* Gets a version of this {@code Option} converted to a particular type.
515+
*
516+
* @param opt the name of the option.
517+
* @param defaultValue the default value to return if opt is not set.
518+
* @param <T> The return type for the method.
519+
* @return the value parsed into a particular object.
520+
* @throws ParseException if there are problems turning the option value into the desired type
521+
* @see PatternOptionBuilder
522+
* @since 1.7.0
523+
*/
524+
public <T> T getParsedOptionValue(final String opt, final Supplier<T> defaultValue) throws ParseException {
525+
return getParsedOptionValue(resolveOption(opt), defaultValue);
526+
}
527+
483528
/**
484529
* jkeyes - commented out until it is implemented properly
485530
* <p>

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

Lines changed: 35 additions & 1 deletion
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

@@ -166,7 +167,40 @@ public void testGetParsedOptionValueWithOption() throws Exception {
166167
}
167168

168169
@Test
169-
public void testNullhOption() throws Exception {
170+
public void testGetParsedOptionValueUsingDefault() throws Exception {
171+
final Options options = new Options();
172+
final Option optI = Option.builder("i").hasArg().type(Number.class).build();
173+
final Option optF = Option.builder("f").hasArg().build();
174+
options.addOption(optI);
175+
options.addOption(optF);
176+
177+
final CommandLineParser parser = new DefaultParser();
178+
final CommandLine cmd = parser.parse(options, new String[] {"-i", "123"});
179+
final Supplier<String> nullSupplier = null;
180+
181+
assertEquals(123, ((Number) cmd.getParsedOptionValue(optI)).intValue());
182+
assertEquals("foo", cmd.getParsedOptionValue(optF, "foo"));
183+
assertEquals("foo", cmd.getParsedOptionValue(optF, () -> "foo"));
184+
assertNull(cmd.getParsedOptionValue(optF, null));
185+
assertNull(cmd.getParsedOptionValue(optF, nullSupplier));
186+
assertNull(cmd.getParsedOptionValue(optF, () -> null));
187+
188+
assertEquals("foo", cmd.getParsedOptionValue("f", "foo"));
189+
assertEquals("foo", cmd.getParsedOptionValue("f", () -> "foo"));
190+
assertNull(cmd.getParsedOptionValue("f", null));
191+
assertNull(cmd.getParsedOptionValue("f", nullSupplier));
192+
assertNull(cmd.getParsedOptionValue("f", () -> null));
193+
194+
assertEquals("foo", cmd.getParsedOptionValue('f', "foo"));
195+
assertEquals("foo", cmd.getParsedOptionValue('f', () -> "foo"));
196+
assertNull(cmd.getParsedOptionValue('f', null));
197+
assertNull(cmd.getParsedOptionValue('f', nullSupplier));
198+
assertNull(cmd.getParsedOptionValue('f', () -> null));
199+
200+
}
201+
202+
@Test
203+
public void testNullOption() throws Exception {
170204
final Options options = new Options();
171205
final Option optI = Option.builder("i").hasArg().type(Number.class).build();
172206
final Option optF = Option.builder("f").hasArg().build();

0 commit comments

Comments
 (0)