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


##########
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:
   OK, then please update the Javadoc instead of writing it up here ;-)



##########
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:
   Not anymore ;-) Please rebase git on master (my comment from the preview 
review).



##########
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:
   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.



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