This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-cli.git


The following commit(s) were added to refs/heads/master by this push:
     new 764c1487 Fix for CLI-340 (#334)
764c1487 is described below

commit 764c1487067383f4fb72dbde8913fba2d6c422ab
Author: Claude Warren <[email protected]>
AuthorDate: Fri Nov 29 13:17:10 2024 +0000

    Fix for CLI-340 (#334)
    
    * Fix for CLI-340
    
    * removed unused import
    
    * fixed javadocs and catch(Throwable) issues
    
    * rebased and fixed catch issue
    
    * Remove extra whitespace
    
    ---------
    
    Co-authored-by: Gary Gregory <[email protected]>
---
 .../java/org/apache/commons/cli/CommandLine.java   | 221 +++++++++++++++++++--
 src/main/java/org/apache/commons/cli/Option.java   |   8 +-
 .../org/apache/commons/cli/help/HelpFormatter.java |   2 +-
 .../org/apache/commons/cli/CommandLineTest.java    | 134 ++++++++++++-
 4 files changed, 340 insertions(+), 25 deletions(-)

diff --git a/src/main/java/org/apache/commons/cli/CommandLine.java 
b/src/main/java/org/apache/commons/cli/CommandLine.java
index 3a310b39..155aaab2 100644
--- a/src/main/java/org/apache/commons/cli/CommandLine.java
+++ b/src/main/java/org/apache/commons/cli/CommandLine.java
@@ -18,6 +18,7 @@
 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;
@@ -503,7 +504,7 @@ public class CommandLine implements Serializable {
      *
      * @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
@@ -518,7 +519,7 @@ public class CommandLine implements Serializable {
      * @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
@@ -533,7 +534,7 @@ public class CommandLine implements Serializable {
      * @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
@@ -547,7 +548,7 @@ public class CommandLine implements Serializable {
      *
      * @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
@@ -562,7 +563,7 @@ public class CommandLine implements Serializable {
      * @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
@@ -589,7 +590,7 @@ public class CommandLine implements Serializable {
      * @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
@@ -603,7 +604,7 @@ public class CommandLine implements Serializable {
      *
      * @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
@@ -618,7 +619,7 @@ public class CommandLine implements Serializable {
      * @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
@@ -636,7 +637,7 @@ public class CommandLine implements Serializable {
      * @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
@@ -650,7 +651,7 @@ public class CommandLine implements Serializable {
      *
      * @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
@@ -665,7 +666,7 @@ public class CommandLine implements Serializable {
      * @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
@@ -680,7 +681,7 @@ public class CommandLine implements Serializable {
      * @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
@@ -689,6 +690,202 @@ public class CommandLine implements Serializable {
         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 
{
+        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.
      *
diff --git a/src/main/java/org/apache/commons/cli/Option.java 
b/src/main/java/org/apache/commons/cli/Option.java
index b3f7f7f2..c82f1ec1 100644
--- a/src/main/java/org/apache/commons/cli/Option.java
+++ b/src/main/java/org/apache/commons/cli/Option.java
@@ -707,9 +707,9 @@ public class Option implements Cloneable, Serializable {
     }
 
     /**
-     * Gets the values of this Option as a String array or null if there are 
no values.
+     * Gets the values of this Option as a String array or an empty array if 
there are no values.
      *
-     * @return the values of this Option as a String array or null if there 
are no values.
+     * @return the values of this Option as a String array or an empty array 
if there are no values.
      */
     public String[] getValues() {
         return hasNoValues() ? null : values.toArray(EMPTY_STRING_ARRAY);
@@ -725,9 +725,9 @@ public class Option implements Cloneable, Serializable {
     }
 
     /**
-     * Gets the values of this Option as a List or null if there are no values.
+     * Gets the values of this Option as a List.  Will return an empty list if 
there are no values.
      *
-     * @return the values of this Option as a List or null if there are no 
values.
+     * @return the values of this Option as a List or an empty List if there 
are no values.
      */
     public List<String> getValuesList() {
         return values;
diff --git a/src/main/java/org/apache/commons/cli/help/HelpFormatter.java 
b/src/main/java/org/apache/commons/cli/help/HelpFormatter.java
index 63cf4059..149a3345 100644
--- a/src/main/java/org/apache/commons/cli/help/HelpFormatter.java
+++ b/src/main/java/org/apache/commons/cli/help/HelpFormatter.java
@@ -147,7 +147,7 @@ public class HelpFormatter extends AbstractHelpFormatter {
      */
     @Override
     public TableDefinition getTableDefinition(final Iterable<Option> options) {
-        // set up the base TextStyle for the columns configured for the Option 
opt and arg values..
+        // set up the base TextStyle for the columns configured for the Option 
opt and arg values.
         final TextStyle.Builder builder = 
TextStyle.builder().setAlignment(TextStyle.Alignment.LEFT).setIndent(DEFAULT_LEFT_PAD).setScalable(false);
         final List<TextStyle> styles = new ArrayList<>();
         styles.add(builder.get());
diff --git a/src/test/java/org/apache/commons/cli/CommandLineTest.java 
b/src/test/java/org/apache/commons/cli/CommandLineTest.java
index 67a1bb6e..b6064106 100644
--- a/src/test/java/org/apache/commons/cli/CommandLineTest.java
+++ b/src/test/java/org/apache/commons/cli/CommandLineTest.java
@@ -59,7 +59,6 @@ public class CommandLineTest {
         lst.add(Arguments.of(new String[] {"--you"}, optT, optionGroup, false, 
false, false, true, optU));
         lst.add(Arguments.of(new String[] {"--you", "foo", "bar"}, optT, 
optionGroup, false, false, false, true, optU));
 
-
         // U set
         lst.add(Arguments.of(new String[] {"-T"}, optU, optionGroup, false, 
false, true, true, optT));
         lst.add(Arguments.of(new String[] {"-T", "foo", "bar"}, optU, 
optionGroup, false, false, true, true, optT));
@@ -157,7 +156,6 @@ public class CommandLineTest {
         lst.add(Arguments.of(new String[] {"--you"}, optT, optionGroup, false, 
null, false, null, optU));
         lst.add(Arguments.of(new String[] {"--you", "1"}, optT, optionGroup, 
false, null, false, expected, optU));
 
-
         // U set
         lst.add(Arguments.of(new String[] {"-T"}, optU, optionGroup, false, 
null, true, null, optT));
         lst.add(Arguments.of(new String[] {"-T", "1"}, optU, optionGroup, 
false, null, true, expected, optT));
@@ -491,7 +489,6 @@ public class CommandLineTest {
         assertArrayEquals(optValue, commandLine.getOptionValues(opt));
         checkHandler(optDep, handler, opt);
 
-
         // test OptionGroup arg
         assertArrayEquals(grpValue, commandLine.getOptionValues(optionGroup));
         checkHandler(grpDep, handler, grpOpt);
@@ -552,7 +549,6 @@ public class CommandLineTest {
         assertEquals(optValue == null ? thing : optValue, 
commandLine.getParsedOptionValue(opt.getLongOpt(), thinger));
         checkHandler(optDep, handler, opt);
 
-
         // test Option arg
         assertEquals(optValue, commandLine.getParsedOptionValue(opt));
         checkHandler(optDep, handler, opt);
@@ -593,7 +589,6 @@ public class CommandLineTest {
         assertEquals(thing, commandLine.getParsedOptionValue(nullGroup, 
thinger));
         checkHandler(false, handler, grpOpt);
 
-
         // test not an option
         assertNull(commandLine.getParsedOptionValue("Nope"));
         checkHandler(false, handler, opt);
@@ -652,7 +647,6 @@ public class CommandLineTest {
         assertFalse(commandLine.hasOption(otherGroup));
         checkHandler(false, handler, grpOpt);
 
-
         // test null group arg
         assertFalse(commandLine.hasOption(nullGroup));
         checkHandler(false, handler, grpOpt);
@@ -689,7 +683,6 @@ public class CommandLineTest {
             assertEquals(has, commandLine.hasOption(asChar(opt)));
             assertWritten(optDep, baos);
 
-
             // test short option arg
             assertEquals(has, commandLine.hasOption(opt.getOpt()));
             assertWritten(optDep, baos);
@@ -741,7 +734,6 @@ public class CommandLineTest {
             assertEquals(has, commandLine.hasOption(asChar(opt)));
             assertWritten(false, baos);
 
-
             // test short option arg
             assertEquals(has, commandLine.hasOption(opt.getOpt()));
             assertWritten(false, baos);
@@ -905,4 +897,130 @@ public class CommandLineTest {
         assertNull(cmd.getOptionValue((OptionGroup) null));
         assertNull(cmd.getParsedOptionValue((OptionGroup) null));
     }
+
+    @ParameterizedTest(name = "{0}, {1}")
+    @MethodSource("createParsedOptionValuesParameters")
+    public void testGetParsedOptionValues(final String[] args, final Option 
opt, final OptionGroup optionGroup, final boolean optDep,
+                                         final Integer[] optValue, final 
boolean grpDep, final Integer[] grpValue, final Option grpOpt) throws 
ParseException {
+        final Options options = new Options().addOptionGroup(optionGroup);
+        final List<Option> handler = new ArrayList<>();
+        final CommandLine commandLine = 
DefaultParser.builder().setDeprecatedHandler(handler::add).get().parse(options, 
args);
+        final Supplier<Integer[]> thinger = () -> new Integer[]{2, 3};
+        final OptionGroup otherGroup = new 
OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().build())
+                
.addOption(Option.builder().option("p").longOpt("part").hasArg().build());
+        final OptionGroup nullGroup = null;
+        final Integer[] thing = {2, 3};
+
+        // test char option arg
+        assertArrayEquals(optValue, 
commandLine.getParsedOptionValues(asChar(opt)));
+        checkHandler(optDep, handler, opt);
+
+        assertArrayEquals(optValue == null ? thing : optValue, 
commandLine.getParsedOptionValues(asChar(opt), thing));
+        checkHandler(optDep, handler, opt);
+
+        assertArrayEquals(optValue == null ? thing : optValue, 
commandLine.getParsedOptionValues(asChar(opt), thinger));
+        checkHandler(optDep, handler, opt);
+
+        // test short option arg
+        assertArrayEquals(optValue, 
commandLine.getParsedOptionValues(opt.getOpt()));
+        checkHandler(optDep, handler, opt);
+
+        assertArrayEquals(optValue == null ? thing : optValue, 
commandLine.getParsedOptionValues(opt.getOpt(), thing));
+        checkHandler(optDep, handler, opt);
+
+        assertArrayEquals(optValue == null ? thing : optValue, 
commandLine.getParsedOptionValues(opt.getOpt(), thinger));
+        checkHandler(optDep, handler, opt);
+
+        // test long option arg
+        assertArrayEquals(optValue, 
commandLine.getParsedOptionValues(opt.getLongOpt()));
+        checkHandler(optDep, handler, opt);
+
+        assertArrayEquals(optValue == null ? thing : optValue, 
commandLine.getParsedOptionValues(opt.getLongOpt(), thing));
+        checkHandler(optDep, handler, opt);
+
+        assertArrayEquals(optValue == null ? thing : optValue, 
commandLine.getParsedOptionValues(opt.getLongOpt(), thinger));
+        checkHandler(optDep, handler, opt);
+
+        // test Option arg
+        assertArrayEquals(optValue, commandLine.getParsedOptionValues(opt));
+        checkHandler(optDep, handler, opt);
+
+        assertArrayEquals(optValue == null ? thing : optValue, 
commandLine.getParsedOptionValues(opt, thing));
+        checkHandler(optDep, handler, opt);
+
+        assertArrayEquals(optValue == null ? thing : optValue, 
commandLine.getParsedOptionValues(opt, thinger));
+        checkHandler(optDep, handler, opt);
+
+        // test OptionGroup arg
+        assertArrayEquals(grpValue, 
commandLine.getParsedOptionValues(optionGroup));
+        checkHandler(grpDep, handler, grpOpt);
+
+        assertArrayEquals(grpValue == null ? thing : grpValue, 
commandLine.getParsedOptionValues(optionGroup, thing));
+        checkHandler(grpDep, handler, grpOpt);
+
+        assertArrayEquals(grpValue == null ? thing : grpValue, 
commandLine.getParsedOptionValues(optionGroup, thinger));
+        checkHandler(grpDep, handler, grpOpt);
+
+        // test other Group arg
+        assertNull(commandLine.getParsedOptionValues(otherGroup));
+        checkHandler(false, handler, grpOpt);
+
+        assertArrayEquals(thing, commandLine.getParsedOptionValues(otherGroup, 
thing));
+        checkHandler(false, handler, grpOpt);
+
+        assertArrayEquals(thing, commandLine.getParsedOptionValues(otherGroup, 
thinger));
+        checkHandler(false, handler, grpOpt);
+
+        // test null Group arg
+        assertNull(commandLine.getParsedOptionValues(nullGroup));
+        checkHandler(false, handler, grpOpt);
+
+        assertArrayEquals(thing, commandLine.getParsedOptionValues(nullGroup, 
thing));
+        checkHandler(false, handler, grpOpt);
+
+        assertArrayEquals(thing, commandLine.getParsedOptionValues(nullGroup, 
thinger));
+        checkHandler(false, handler, grpOpt);
+
+        // test not an option
+        assertNull(commandLine.getParsedOptionValues("Nope"));
+        checkHandler(false, handler, opt);
+
+        assertArrayEquals(thing, commandLine.getParsedOptionValues("Nope", 
thing));
+        checkHandler(false, handler, opt);
+
+        assertArrayEquals(thing, commandLine.getParsedOptionValues("Nope", 
thinger));
+        checkHandler(false, handler, opt);
+    }
+
+    private static Stream<Arguments> createParsedOptionValuesParameters() 
throws ParseException {
+        final List<Arguments> lst = new ArrayList<>();
+        final Option optT = 
Option.builder().option("T").longOpt("tee").deprecated().type(Integer.class).optionalArg(true).hasArgs().build();
+        final Option optU = 
Option.builder("U").longOpt("you").type(Integer.class).optionalArg(true).hasArgs().build();
+        final OptionGroup optionGroup = new 
OptionGroup().addOption(optT).addOption(optU);
+        final Integer[] expected = new Integer[]{1, 2};
+
+        // T set
+        lst.add(Arguments.of(new String[] {"-T"}, optT, optionGroup, true, 
null, true, null, optT));
+        lst.add(Arguments.of(new String[] {"-T", "1", "2"}, optT, optionGroup, 
true, expected, true, expected, optT));
+        lst.add(Arguments.of(new String[] {"--tee"}, optT, optionGroup, true, 
null, true, null, optT));
+        lst.add(Arguments.of(new String[] {"--tee", "1", "2"}, optT, 
optionGroup, true, expected, true, expected, optT));
+
+        lst.add(Arguments.of(new String[] {"-U"}, optT, optionGroup, false, 
null, false, null, optU));
+        lst.add(Arguments.of(new String[] {"-U", "1", "2"}, optT, optionGroup, 
false, null, false, expected, optU));
+        lst.add(Arguments.of(new String[] {"--you"}, optT, optionGroup, false, 
null, false, null, optU));
+        lst.add(Arguments.of(new String[] {"--you", "1", "2"}, optT, 
optionGroup, false, null, false, expected, optU));
+
+        // U set
+        lst.add(Arguments.of(new String[] {"-T"}, optU, optionGroup, false, 
null, true, null, optT));
+        lst.add(Arguments.of(new String[] {"-T", "1", "2"}, optU, optionGroup, 
false, null, true, expected, optT));
+        lst.add(Arguments.of(new String[] {"--tee"}, optU, optionGroup, false, 
null, true, null, optT));
+        lst.add(Arguments.of(new String[] {"--tee", "1", "2"}, optU, 
optionGroup, false, null, true, expected, optT));
+
+        lst.add(Arguments.of(new String[] {"-U"}, optU, optionGroup, false, 
null, false, null, optU));
+        lst.add(Arguments.of(new String[] {"-U", "1", "2"}, optU, optionGroup, 
false, expected, false, expected, optU));
+        lst.add(Arguments.of(new String[] {"--you"}, optU, optionGroup, false, 
null, false, null, optU));
+        lst.add(Arguments.of(new String[] {"--you", "1", "2"},  optU, 
optionGroup, false, expected, false, expected, optU));
+
+        return lst.stream();
+    }
 }

Reply via email to