Skip to content
Closed
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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>

<properties>
Expand Down
63 changes: 56 additions & 7 deletions src/main/java/org/apache/commons/cli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,9 @@ public String[] getOptionValues(final String opt) {
* @throws ParseException if there are problems turning the option value into the desired type
* @see PatternOptionBuilder
* @since 1.5.0
* @param <T> The return type for the method.
*/
public Object getParsedOptionValue(final char opt) throws ParseException {
public <T> T getParsedOptionValue(final char opt) throws ParseException {
return getParsedOptionValue(String.valueOf(opt));
}

Expand All @@ -364,29 +365,77 @@ public Object getParsedOptionValue(final char opt) throws ParseException {
* @throws ParseException if there are problems turning the option value into the desired type
* @see PatternOptionBuilder
* @since 1.5.0
* @param <T> The return type for the method.
*/
public Object getParsedOptionValue(final Option option) throws ParseException {
public <T> T getParsedOptionValue(final Option option) throws ParseException {
return getParsedOptionValue(option, null);
}

/**
* Gets a version of this {@code Option} converted to a particular type.
*
* @param opt the name of the option.
* @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.2
* @param <T> The return type for the method.
*/
public <T> T getParsedOptionValue(final String opt) throws ParseException {
return getParsedOptionValue(resolveOption(opt));
}

/**
* 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.
* @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
* @param <T> The return type for the method.
*/
public <T> T getParsedOptionValue(final char opt, T defaultValue) throws ParseException {
return getParsedOptionValue(String.valueOf(opt), 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.
* @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
* @param <T> The return type for the method.
*/
@SuppressWarnings("unchecked")
public <T> T getParsedOptionValue(final Option option, T defaultValue) throws ParseException {
if (option == null) {
return null;
}
final String res = getOptionValue(option);
if (res == null) {
return null;
return defaultValue;
}
return TypeHandler.createValue(res, option.getType());
return (T) TypeHandler.createValue(res, (Class<?>) option.getType());
}

/**
* 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.
* @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.2
* @since 1.7
* @param <T> The return type for the method.
*/
public Object getParsedOptionValue(final String opt) throws ParseException {
return getParsedOptionValue(resolveOption(opt));
public <T> T getParsedOptionValue(final String opt, T defaultValue) throws ParseException {
return getParsedOptionValue(resolveOption(opt), defaultValue);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/apache/commons/cli/ParseException.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ public class ParseException extends Exception {
public ParseException(final String message) {
super(message);
}

public ParseException(final Exception e) {
super(e);
}
}
Loading