diff --git a/src/main/java/org/apache/commons/cli/CommandLine.java b/src/main/java/org/apache/commons/cli/CommandLine.java index 7f1bf4820..b2f3a3d5c 100644 --- a/src/main/java/org/apache/commons/cli/CommandLine.java +++ b/src/main/java/org/apache/commons/cli/CommandLine.java @@ -412,6 +412,21 @@ public T getParsedOptionValue(final char opt, final T defaultValue) throws P return getParsedOptionValue(String.valueOf(opt), defaultValue); } + /** + * Gets a version of this {@code Option} converted to a particular type. + * + * @param opt the name of the option. + * @param defaultValue the default value to return if opt is not set. + * @param The return type for the method. + * @return the value parsed into a particular object. + * @throws ParseException if there are problems turning the option value into the desired type + * @see PatternOptionBuilder + * @since 1.7.0 + */ + public T getParsedOptionValue(final char opt, final Supplier defaultValue) throws ParseException { + return getParsedOptionValue(String.valueOf(opt), defaultValue); + } + /** * Gets a version of this {@code Option} converted to a particular type. * @@ -423,7 +438,7 @@ public T getParsedOptionValue(final char opt, final T defaultValue) throws P * @since 1.5.0 */ public T getParsedOptionValue(final Option option) throws ParseException { - return getParsedOptionValue(option, null); + return getParsedOptionValue(option, () -> null); } /** @@ -437,15 +452,30 @@ public T getParsedOptionValue(final Option option) throws ParseException { * @see PatternOptionBuilder * @since 1.7.0 */ - @SuppressWarnings("unchecked") public T getParsedOptionValue(final Option option, final T defaultValue) throws ParseException { - if (option == null) { - return null; - } - final String res = getOptionValue(option); + return getParsedOptionValue(option, () -> defaultValue); + } + + /** + * Gets a version of this {@code Option} converted to a particular type. + * + * @param option the name of the option. + * @param defaultValue the default value to return if opt is not set. + * @param The return type for the method. + * @return the value parsed into a particular object. + * @throws ParseException if there are problems turning the option value into the desired type + * @see PatternOptionBuilder + * @since 1.7.0 + */ + @SuppressWarnings("unchecked") + public T getParsedOptionValue(final Option option, final Supplier defaultValue) throws ParseException { + final String res = option == null ? null : getOptionValue(option); try { - return res == null ? defaultValue : (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); } @@ -480,6 +510,21 @@ public T getParsedOptionValue(final String opt, final T defaultValue) throws return getParsedOptionValue(resolveOption(opt), defaultValue); } + /** + * Gets a version of this {@code Option} converted to a particular type. + * + * @param opt the name of the option. + * @param defaultValue the default value to return if opt is not set. + * @param The return type for the method. + * @return the value parsed into a particular object. + * @throws ParseException if there are problems turning the option value into the desired type + * @see PatternOptionBuilder + * @since 1.7.0 + */ + public T getParsedOptionValue(final String opt, final Supplier defaultValue) throws ParseException { + return getParsedOptionValue(resolveOption(opt), defaultValue); + } + /** * jkeyes - commented out until it is implemented properly *

diff --git a/src/test/java/org/apache/commons/cli/CommandLineTest.java b/src/test/java/org/apache/commons/cli/CommandLineTest.java index 4522fb030..98c02ca8c 100644 --- a/src/test/java/org/apache/commons/cli/CommandLineTest.java +++ b/src/test/java/org/apache/commons/cli/CommandLineTest.java @@ -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; @@ -166,7 +167,40 @@ public void testGetParsedOptionValueWithOption() throws Exception { } @Test - public void testNullhOption() throws Exception { + public void testGetParsedOptionValueUsingDefault() throws Exception { + final Options options = new Options(); + final Option optI = Option.builder("i").hasArg().type(Number.class).build(); + final Option optF = Option.builder("f").hasArg().build(); + options.addOption(optI); + options.addOption(optF); + + final CommandLineParser parser = new DefaultParser(); + final CommandLine cmd = parser.parse(options, new String[] {"-i", "123"}); + final Supplier nullSupplier = null; + + assertEquals(123, ((Number) cmd.getParsedOptionValue(optI)).intValue()); + assertEquals("foo", cmd.getParsedOptionValue(optF, "foo")); + assertEquals("foo", cmd.getParsedOptionValue(optF, () -> "foo")); + assertNull(cmd.getParsedOptionValue(optF, null)); + assertNull(cmd.getParsedOptionValue(optF, nullSupplier)); + assertNull(cmd.getParsedOptionValue(optF, () -> null)); + + assertEquals("foo", cmd.getParsedOptionValue("f", "foo")); + assertEquals("foo", cmd.getParsedOptionValue("f", () -> "foo")); + assertNull(cmd.getParsedOptionValue("f", null)); + assertNull(cmd.getParsedOptionValue("f", nullSupplier)); + assertNull(cmd.getParsedOptionValue("f", () -> null)); + + assertEquals("foo", cmd.getParsedOptionValue('f', "foo")); + assertEquals("foo", cmd.getParsedOptionValue('f', () -> "foo")); + assertNull(cmd.getParsedOptionValue('f', null)); + assertNull(cmd.getParsedOptionValue('f', nullSupplier)); + assertNull(cmd.getParsedOptionValue('f', () -> null)); + + } + + @Test + public void testNullOption() throws Exception { final Options options = new Options(); final Option optI = Option.builder("i").hasArg().type(Number.class).build(); final Option optF = Option.builder("f").hasArg().build();