Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
59 changes: 52 additions & 7 deletions src/main/java/org/apache/commons/cli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,21 @@ public <T> 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 <T> 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> T getParsedOptionValue(final char opt, final Supplier<T> defaultValue) throws ParseException {
return getParsedOptionValue(String.valueOf(opt), defaultValue);
}

/**
* Gets a version of this {@code Option} converted to a particular type.
*
Expand All @@ -423,7 +438,7 @@ public <T> T getParsedOptionValue(final char opt, final T defaultValue) throws P
* @since 1.5.0
*/
public <T> T getParsedOptionValue(final Option option) throws ParseException {
return getParsedOptionValue(option, null);
return getParsedOptionValue(option, () -> null);
}

/**
Expand All @@ -437,15 +452,30 @@ public <T> T getParsedOptionValue(final Option option) throws ParseException {
* @see PatternOptionBuilder
* @since 1.7.0
*/
@SuppressWarnings("unchecked")
public <T> 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 <T> 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> T getParsedOptionValue(final Option option, final Supplier<T> 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);
}
Expand Down Expand Up @@ -480,6 +510,21 @@ public <T> 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 <T> 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> T getParsedOptionValue(final String opt, final Supplier<T> defaultValue) throws ParseException {
return getParsedOptionValue(resolveOption(opt), defaultValue);
}

/**
* jkeyes - commented out until it is implemented properly
* <p>
Expand Down
36 changes: 35 additions & 1 deletion 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 @@ -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<String> 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"));
Comment thread
garydgregory marked this conversation as resolved.
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();
Expand Down