garydgregory commented on code in PR #334:
URL: https://github.com/apache/commons-cli/pull/334#discussion_r1838621644
##########
src/main/java/org/apache/commons/cli/CommandLine.java:
##########
@@ -691,6 +692,203 @@ 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.
+ *
Review Comment:
Can the method return null? If so, when? What happens if the option does not
exist?
##########
src/main/java/org/apache/commons/cli/CommandLine.java:
##########
@@ -691,6 +692,203 @@ 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.
+ * @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
{
Review Comment:
No need to abbreviate IMO: `opt` -> `option`. Also, using `opt` in Javadoc
makes for awkward reading.
##########
src/main/java/org/apache/commons/cli/CommandLine.java:
##########
@@ -691,6 +692,203 @@ 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.
+ * @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
{
+ 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.
+ * @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.
+ * @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.
+ * @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.
+ * @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 (Throwable t) {
Review Comment:
Catching `Throwable` is a bit of an anti-pattern here IMO because you
certainly don't want to demote `Error`s as `ParseException`s
You should catch more specific exceptions, even if means catching all of
`RuntimeException`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]