Claudenw commented on code in PR #334:
URL: https://github.com/apache/commons-cli/pull/334#discussion_r1838995804


##########
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:
   True, I was following the pattern from `getParsedValue()`.  I will fix it 
there as well, but since the Converter throws Throwable we have to deal with it 
as a Throwable.  



-- 
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]

Reply via email to