-
Notifications
You must be signed in to change notification settings - Fork 191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix for CLI-340 #334
base: master
Are you sure you want to change the base?
Fix for CLI-340 #334
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more | |
package org.apache.commons.cli; | ||
|
||
import java.io.Serializable; | ||
import java.lang.reflect.Array; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
|
@@ -505,7 +506,7 @@ public String[] getOptionValues(final String opt) { | |
* | ||
* @param opt the name of the option. | ||
* @param <T> The return type for the method. | ||
* @return the value parsed into a particular object. | ||
* @return the value parsed into a particular object or null if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.5.0 | ||
|
@@ -520,7 +521,7 @@ public <T> T getParsedOptionValue(final char opt) throws ParseException { | |
* @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. | ||
* @return the value parsed into a particular object or the defaultValue if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.7.0 | ||
|
@@ -535,7 +536,7 @@ public <T> T getParsedOptionValue(final char opt, final Supplier<T> defaultValue | |
* @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. | ||
* @return the value parsed into a particular object or the defaultValue if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.7.0 | ||
|
@@ -549,7 +550,7 @@ public <T> T getParsedOptionValue(final char opt, final T defaultValue) throws P | |
* | ||
* @param option the option. | ||
* @param <T> The return type for the method. | ||
* @return the value parsed into a particular object. | ||
* @return the value parsed into a particular object or null if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.5.0 | ||
|
@@ -564,7 +565,7 @@ public <T> T getParsedOptionValue(final Option option) throws ParseException { | |
* @param option 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. | ||
* @return the value parsed into a particular object or the defaultValue if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.7.0 | ||
|
@@ -591,7 +592,7 @@ public <T> T getParsedOptionValue(final Option option, final Supplier<T> default | |
* @param option 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. | ||
* @return the value parsed into a particular object or the defaultValue if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.7.0 | ||
|
@@ -605,7 +606,7 @@ public <T> T getParsedOptionValue(final Option option, final T defaultValue) thr | |
* | ||
* @param optionGroup the option group. | ||
* @param <T> The return type for the method. | ||
* @return the value parsed into a particular object. | ||
* @return the value parsed into a particular object or null if no option in the OptionGroup is set. | ||
* @throws ParseException if there are problems turning the selected option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.9.0 | ||
|
@@ -620,7 +621,7 @@ public <T> T getParsedOptionValue(final OptionGroup optionGroup) throws ParseExc | |
* @param optionGroup the option group. | ||
* @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. | ||
* @return the value parsed into a particular object or the defaultValue if no option in the OptionGroup is set. | ||
* @throws ParseException if there are problems turning the selected option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.9.0 | ||
|
@@ -638,7 +639,7 @@ public <T> T getParsedOptionValue(final OptionGroup optionGroup, final Supplier< | |
* @param optionGroup the option group. | ||
* @param defaultValue the default value to return if an option is not selected. | ||
* @param <T> The return type for the method. | ||
* @return the value parsed into a particular object. | ||
* @return the value parsed into a particular object or the defaultValue if no option in the OptionGroup is set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.9.0 | ||
|
@@ -652,7 +653,7 @@ public <T> T getParsedOptionValue(final OptionGroup optionGroup, final T default | |
* | ||
* @param opt the name of the option. | ||
* @param <T> The return type for the method. | ||
* @return the value parsed into a particular object. | ||
* @return the value parsed into a particular object or null if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.2 | ||
|
@@ -667,7 +668,7 @@ public <T> T getParsedOptionValue(final String opt) throws ParseException { | |
* @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. | ||
* @return the value parsed into a particular object or the defaultValue if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.7.0 | ||
|
@@ -682,7 +683,7 @@ public <T> T getParsedOptionValue(final String opt, final Supplier<T> defaultVal | |
* @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. | ||
* @return the value parsed into a particular object or the defaultValue if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.7.0 | ||
|
@@ -691,6 +692,202 @@ 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 an array of a particular type. | ||
* | ||
* @param opt the name of the option. | ||
* @param <T> The array type for the return value. | ||
* @return the values parsed into an array of objects or null if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.10.0 | ||
*/ | ||
public <T> T[] getParsedOptionValues(final char opt) throws ParseException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to abbreviate IMO: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am following the pattern used in the class. Methods from earlier versions used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, it's slightly awkward (to me) to read "@param opt the name of the option." If 'opt' is the name, then why not call it 'name' or 'optionName'? Might as well do the best we can for new code. We can fix the rest separately if you want or do it all in this PR since. |
||
return getParsedOptionValues(String.valueOf(opt)); | ||
} | ||
|
||
/** | ||
* Gets a version of this {@code Option} converted to an array of 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 array type for the return value. | ||
* @return the values parsed into an array of objects or the defaultValue if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.10.0 | ||
*/ | ||
public <T> T[] getParsedOptionValues(final char opt, final Supplier<T[]> defaultValue) throws ParseException { | ||
return getParsedOptionValues(String.valueOf(opt), defaultValue); | ||
} | ||
|
||
/** | ||
* Gets a version of this {@code Option} converted to an array of 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 array type for the return value. | ||
* @return the values parsed into an array of objects or the defaultValue if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.10.0 | ||
*/ | ||
public <T> T[] getParsedOptionValues(final char opt, final T[] defaultValue) throws ParseException { | ||
return getParsedOptionValues(String.valueOf(opt), defaultValue); | ||
} | ||
|
||
/** | ||
* Gets a version of this {@code Option} converted to an array of a particular type. | ||
* | ||
* @param option the option. | ||
* @param <T> The array type for the return value. | ||
* @return the values parsed into an array of objects or null if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.10.0 | ||
*/ | ||
public <T> T[] getParsedOptionValues(final Option option) throws ParseException { | ||
return getParsedOptionValues(option, () -> null); | ||
} | ||
|
||
/** | ||
* Gets a version of this {@code Option} converted to an array of a particular type. | ||
* | ||
* @param option the option. | ||
* @param defaultValue the default value to return if opt is not set. | ||
* @param <T> The array type for the return value. | ||
* @return the values parsed into an array of objects or the defaultValue if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.10.0 | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public <T> T[] getParsedOptionValues(final Option option, final Supplier<T[]> defaultValue) throws ParseException { | ||
if (option == null) { | ||
return get(defaultValue); | ||
} | ||
Class<? extends T> clazz = (Class<? extends T>) option.getType(); | ||
String[] values = getOptionValues(option); | ||
if (values == null) { | ||
return get(defaultValue); | ||
} | ||
T[] result = (T[]) Array.newInstance(clazz, values.length); | ||
try { | ||
for (int i = 0; i < values.length; i++) { | ||
result[i] = clazz.cast(option.getConverter().apply(values[i])); | ||
} | ||
return result; | ||
} catch (final Exception t) { | ||
throw ParseException.wrap(t); | ||
} | ||
} | ||
|
||
/** | ||
* Gets a version of this {@code Option} converted to an array of a particular type. | ||
* | ||
* @param option the option. | ||
* @param defaultValue the default value to return if opt is not set. | ||
* @param <T> The array type for the return value. | ||
* @return the values parsed into an array of objects or the defaultValue if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.10.0 | ||
*/ | ||
public <T> T[] getParsedOptionValues(final Option option, final T[] defaultValue) throws ParseException { | ||
return getParsedOptionValues(option, () -> defaultValue); | ||
} | ||
|
||
/** | ||
* Gets a version of this {@code OptionGroup} converted to an array of a particular type. | ||
* | ||
* @param optionGroup the option group. | ||
* @param <T> The array type for the return value. | ||
* @return the values parsed into an array of objects or null if no option in the OptionGroup is set. | ||
* @throws ParseException if there are problems turning the selected option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.10.0 | ||
*/ | ||
public <T> T[] getParsedOptionValues(final OptionGroup optionGroup) throws ParseException { | ||
return getParsedOptionValues(optionGroup, () -> null); | ||
} | ||
|
||
/** | ||
* Gets a version of this {@code OptionGroup} converted to an array of a particular type. | ||
* | ||
* @param optionGroup the option group. | ||
* @param defaultValue the default value to return if opt is not set. | ||
* @param <T> The array type for the return value. | ||
* @return the values parsed into an array of objects or null if no option in the OptionGroup is set. | ||
* @throws ParseException if there are problems turning the selected option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.10.0 | ||
*/ | ||
public <T> T[] getParsedOptionValues(final OptionGroup optionGroup, final Supplier<T[]> defaultValue) throws ParseException { | ||
if (optionGroup == null || !optionGroup.isSelected()) { | ||
return get(defaultValue); | ||
} | ||
return getParsedOptionValues(optionGroup.getSelected(), defaultValue); | ||
} | ||
|
||
/** | ||
* Gets a version of this {@code OptionGroup} converted to an array of a particular type. | ||
* | ||
* @param optionGroup the option group. | ||
* @param defaultValue the default value to return if an option is not selected. | ||
* @param <T> The array type for the return value. | ||
* @return the values parsed into an array of objects or null if no option in the OptionGroup is set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.10.0 | ||
*/ | ||
public <T> T[] getParsedOptionValues(final OptionGroup optionGroup, final T[] defaultValue) throws ParseException { | ||
return getParsedOptionValues(optionGroup, () -> defaultValue); | ||
} | ||
|
||
/** | ||
* Gets a version of this {@code Option} converted to an array of a particular type. | ||
* | ||
* @param opt the name of the option. | ||
* @param <T> The array type for the return value. | ||
* @return the values parsed into an array of objects or null if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.10.0 | ||
*/ | ||
public <T> T[] getParsedOptionValues(final String opt) throws ParseException { | ||
return getParsedOptionValues(resolveOption(opt)); | ||
} | ||
|
||
/** | ||
* Gets a version of this {@code Option} converted to an array of 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 array type for the return value. | ||
* @return the values parsed into an array of objects or defaultValues if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.10.0 | ||
*/ | ||
public <T> T[] getParsedOptionValues(final String opt, final Supplier<T[]> defaultValue) throws ParseException { | ||
return getParsedOptionValues(resolveOption(opt), defaultValue); | ||
} | ||
|
||
/** | ||
* Gets a version of this {@code Option} converted to an array of 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 array type for the return value. | ||
* @return the values parsed into an array of objects or defaultValues if the option is not set. | ||
* @throws ParseException if there are problems turning the option value into the desired type | ||
* @see PatternOptionBuilder | ||
* @since 1.10.0 | ||
*/ | ||
public <T> T[] getParsedOptionValues(final String opt, final T[] defaultValue) throws ParseException { | ||
return getParsedOptionValues(resolveOption(opt), defaultValue); | ||
} | ||
|
||
/** | ||
* Handles deprecated options. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the method return null? If so, when? What happens if the option does not exist?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As with all the value getter methods if the option is not in the command line
null
is returned unless the default value is defined.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, then please update the Javadoc instead of writing it up here ;-)