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 12124d4 [CLI-336] Deprecation not always reported (#284)
12124d4 is described below
commit 12124d41bebd799db75815c4696ade73534cb1b7
Author: Claude Warren <[email protected]>
AuthorDate: Sat Jun 22 14:44:36 2024 +0200
[CLI-336] Deprecation not always reported (#284)
* added methods with OptionGroup args + tests
* Updated OptionGroup documentation to indicate where deprecation is no
reported
* Added tests
* added tests + cleaned up checkstyle
* added more tests
---
.../java/org/apache/commons/cli/CommandLine.java | 133 +++-
.../java/org/apache/commons/cli/OptionGroup.java | 3 +
.../org/apache/commons/cli/CommandLineTest.java | 875 +++++++++++++++++----
3 files changed, 856 insertions(+), 155 deletions(-)
diff --git a/src/main/java/org/apache/commons/cli/CommandLine.java
b/src/main/java/org/apache/commons/cli/CommandLine.java
index fdf5e57..8ca7eb8 100644
--- a/src/main/java/org/apache/commons/cli/CommandLine.java
+++ b/src/main/java/org/apache/commons/cli/CommandLine.java
@@ -314,7 +314,7 @@ public class CommandLine implements Serializable {
/**
* Gets the first argument, if any, of this option.
*
- * @param option the name of the option.
+ * @param option the option.
* @return Value of the argument if option is set, and has an argument,
otherwise null.
* @since 1.5.0
*/
@@ -326,7 +326,7 @@ public class CommandLine implements Serializable {
/**
* Gets the first argument, if any, of an option.
*
- * @param option name of the option.
+ * @param option the option.
* @param defaultValue is the default value to be returned if the option
is not specified.
* @return Value of the argument if option is set, and has an argument,
otherwise {@code defaultValue}.
* @since 1.5.0
@@ -338,7 +338,7 @@ public class CommandLine implements Serializable {
/**
* Gets the first argument, if any, of an option.
*
- * @param option name of the option.
+ * @param option the option.
* @param defaultValue is a supplier for the default value to be returned
if the option is not specified.
* @return Value of the argument if option is set, and has an argument,
otherwise {@code defaultValue}.
* @since 1.7.0
@@ -348,6 +348,44 @@ public class CommandLine implements Serializable {
return answer != null ? answer : get(defaultValue);
}
+ /**
+ * Gets the first argument, if any, of this option group.
+ *
+ * @param optionGroup the option group.
+ * @return Value of the argument if option group is selected, and has an
argument, otherwise null.
+ * @since 1.9.0
+ */
+ public String getOptionValue(final OptionGroup optionGroup) {
+ final String[] values = getOptionValues(optionGroup);
+ return values == null ? null : values[0];
+ }
+
+ /**
+ * Gets the first argument, if any, of an option group.
+ *
+ * @param optionGroup the option group.
+ * @param defaultValue is the default value to be returned if the option
group is not selected.
+ * @return Value of the argument if option group is selected, and has an
argument, otherwise {@code defaultValue}.
+ * @since 1.9.0
+ */
+ public String getOptionValue(final OptionGroup optionGroup, final String
defaultValue) {
+ return getOptionValue(optionGroup, () -> defaultValue);
+ }
+
+ /**
+ * Gets the first argument, if any, of an option group.
+ *
+ * @param optionGroup the option group..
+ * @param defaultValue is a supplier for the default value to be returned
if the option group is not selected.
+ * @return Value of the argument if option group is selected, and has an
argument, otherwise {@code defaultValue}.
+ * @since 1.9.0
+ */
+ public String getOptionValue(final OptionGroup optionGroup, final
Supplier<String> defaultValue) {
+ final String answer = getOptionValue(optionGroup);
+ return answer != null ? answer : get(defaultValue);
+ }
+
+
/**
* Gets the first argument, if any, of this option.
*
@@ -395,7 +433,7 @@ public class CommandLine implements Serializable {
/**
* Gets the array of values, if any, of an option.
*
- * @param option string name of the option.
+ * @param option the option.
* @return Values of the argument if option is set, and has an argument,
otherwise null.
* @since 1.5.0
*/
@@ -403,18 +441,32 @@ public class CommandLine implements Serializable {
if (option == null) {
return null;
}
- if (option.isDeprecated()) {
- handleDeprecated(option);
- }
final List<String> values = new ArrayList<>();
for (final Option processedOption : options) {
if (processedOption.equals(option)) {
+ if (option.isDeprecated()) {
+ handleDeprecated(option);
+ }
values.addAll(processedOption.getValuesList());
}
}
return values.isEmpty() ? null :
values.toArray(Util.EMPTY_STRING_ARRAY);
}
+ /**
+ * Gets the array of values, if any, of an option group.
+ *
+ * @param optionGroup the option group.
+ * @return Values of the argument if option group is selected, and has an
argument, otherwise null.
+ * @since 1.9.0
+ */
+ public String[] getOptionValues(final OptionGroup optionGroup) {
+ if (optionGroup == null || !optionGroup.isSelected()) {
+ return null;
+ }
+ return getOptionValues(optionGroup.getSelected());
+ }
+
/**
* Gets the array of values, if any, of an option.
*
@@ -472,7 +524,7 @@ public class CommandLine implements Serializable {
/**
* Gets a version of this {@code Option} converted to a particular type.
*
- * @param option the name of the option.
+ * @param option the option.
* @param <T> The return type for the method.
* @return the value parsed into a particular object.
* @throws ParseException if there are problems turning the option value
into the desired type
@@ -486,7 +538,7 @@ public class CommandLine implements Serializable {
/**
* Gets a version of this {@code Option} converted to a particular type.
*
- * @param option the name of the option.
+ * @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.
@@ -513,7 +565,7 @@ public class CommandLine implements Serializable {
/**
* Gets a version of this {@code Option} converted to a particular type.
*
- * @param option the name of the option.
+ * @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.
@@ -525,6 +577,53 @@ public class CommandLine implements Serializable {
return getParsedOptionValue(option, () -> defaultValue);
}
+ /**
+ * Gets a version of this {@code OptionGroup} converted to a particular
type.
+ *
+ * @param optionGroup the option group.
+ * @param <T> The return type for the method.
+ * @return the value parsed into a particular object.
+ * @throws ParseException if there are problems turning the selected
option value into the desired type
+ * @see PatternOptionBuilder
+ * @since 1.9.0
+ */
+ public <T> T getParsedOptionValue(final OptionGroup optionGroup) throws
ParseException {
+ return getParsedOptionValue(optionGroup, () -> null);
+ }
+
+ /**
+ * Gets a version of this {@code OptionGroup} converted to a particular
type.
+ *
+ * @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.
+ * @throws ParseException if there are problems turning the selected
option value into the desired type
+ * @see PatternOptionBuilder
+ * @since 1.9.0
+ */
+ public <T> T getParsedOptionValue(final OptionGroup optionGroup, final
Supplier<T> defaultValue) throws ParseException {
+ if (optionGroup == null || !optionGroup.isSelected()) {
+ return get(defaultValue);
+ }
+ return getParsedOptionValue(optionGroup.getSelected(), defaultValue);
+ }
+
+ /**
+ * Gets a version of this {@code OptionGroup} converted to a particular
type.
+ *
+ * @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.
+ * @throws ParseException if there are problems turning the option value
into the desired type
+ * @see PatternOptionBuilder
+ * @since 1.9.0
+ */
+ public <T> T getParsedOptionValue(final OptionGroup optionGroup, final T
defaultValue) throws ParseException {
+ return getParsedOptionValue(optionGroup, () -> defaultValue);
+ }
+
/**
* Gets a version of this {@code Option} converted to a particular type.
*
@@ -623,6 +722,20 @@ public class CommandLine implements Serializable {
return result;
}
+ /**
+ * Tests to see if an option has been set.
+ *
+ * @param optionGroup the option group to check.
+ * @return true if set, false if not.
+ * @since 1.9.0
+ */
+ public boolean hasOption(final OptionGroup optionGroup) {
+ if (optionGroup == null || !optionGroup.isSelected()) {
+ return false;
+ }
+ return hasOption(optionGroup.getSelected());
+ }
+
/**
* Tests to see if an option has been set.
*
diff --git a/src/main/java/org/apache/commons/cli/OptionGroup.java
b/src/main/java/org/apache/commons/cli/OptionGroup.java
index 51f75e9..0685179 100644
--- a/src/main/java/org/apache/commons/cli/OptionGroup.java
+++ b/src/main/java/org/apache/commons/cli/OptionGroup.java
@@ -76,6 +76,7 @@ public class OptionGroup implements Serializable {
/**
* Gets the selected option name.
*
+ * If the selected option is deprecated <em>no warning is logged</em>.
* @return the selected option name.
*/
public String getSelected() {
@@ -94,6 +95,7 @@ public class OptionGroup implements Serializable {
/**
* Tests whether an option is selected.
*
+ * If an option is selected and is deprecated <em>no warning is
logged</em>.
* @return whether whether an option is selected.
* @since 1.9.0
*/
@@ -113,6 +115,7 @@ public class OptionGroup implements Serializable {
/**
* Sets the selected option of this group to {@code name}.
*
+ * If the selected option is deprecated <em>no warning is logged</em>.
* @param option the option that is selected
* @throws AlreadySelectedException if an option from this group has
already been selected.
*/
diff --git a/src/test/java/org/apache/commons/cli/CommandLineTest.java
b/src/test/java/org/apache/commons/cli/CommandLineTest.java
index f0fe20f..f0edf21 100644
--- a/src/test/java/org/apache/commons/cli/CommandLineTest.java
+++ b/src/test/java/org/apache/commons/cli/CommandLineTest.java
@@ -17,22 +17,30 @@
package org.apache.commons.cli;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
-import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
+import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
-@SuppressWarnings("deprecation") // tests some deprecated classes
public class CommandLineTest {
+ private enum Count { ONE, TWO, THREE };
+
@Test
public void testBuilder() {
final CommandLine.Builder builder = new CommandLine.Builder();
@@ -70,93 +78,13 @@ public class CommandLineTest {
assertEquals(0, cmd.getOptions().length);
}
- @Test
- public void testDeprecatedDefaultOption() {
- final CommandLine.Builder builder = new CommandLine.Builder();
- builder.addArg("foo").addArg("bar");
- final Option opt = Option.builder().option("T").deprecated().build();
- builder.addOption(opt);
- final AtomicReference<Option> handler = new AtomicReference<>();
- final CommandLine cmd = builder.build();
- cmd.getOptionValue(opt.getOpt());
- handler.set(null);
- cmd.getOptionValue("Nope");
- assertNull(handler.get());
- }
-
- @Test
- public void testDeprecatedOption() {
- final CommandLine.Builder builder = new CommandLine.Builder();
- builder.addArg("foo").addArg("bar");
- final Option opt =
Option.builder().option("T").longOpt("tee").deprecated().build();
- builder.addOption(opt);
- // verify one and only one call
- final List<Option> handler = new ArrayList<>();
- final CommandLine cmd =
builder.setDeprecatedHandler(handler::add).build();
- // test short option arg
- cmd.getOptionValue(opt.getOpt());
- assertEquals(1, handler.size());
- assertSame(opt, handler.get(0));
- handler.clear();
-
- // test long option arg
- cmd.getOptionValue(opt.getLongOpt());
- assertEquals(1, handler.size());
- assertSame(opt, handler.get(0));
- handler.clear();
-
- // test Option arg
- cmd.getOptionValue(opt);
- assertEquals(1, handler.size());
- assertSame(opt, handler.get(0));
- handler.clear();
-
- // test not an option
- cmd.getOptionValue("Nope");
- assertEquals(0, handler.size());
- }
-
- @Test
- public void testDeprecatedParsedOptionValue() throws ParseException {
- final CommandLine.Builder builder = new CommandLine.Builder();
- builder.addArg("foo").addArg("bar");
- final Option opt =
Option.builder().option("T").longOpt("tee").deprecated().build();
- builder.addOption(opt);
- // verify one and only one call
- final List<Option> handler = new ArrayList<>();
- final CommandLine cmd =
builder.setDeprecatedHandler(handler::add).build();
-
- // test short option arg
- cmd.getParsedOptionValue(opt.getOpt());
- assertEquals(1, handler.size());
- assertSame(opt, handler.get(0));
- handler.clear();
-
- // test long option arg
- cmd.getParsedOptionValue(opt.getLongOpt());
- assertEquals(1, handler.size());
- assertSame(opt, handler.get(0));
- handler.clear();
-
- // test option arg
- cmd.getParsedOptionValue(opt);
- assertEquals(1, handler.size());
- assertSame(opt, handler.get(0));
- handler.clear();
-
-
- // test not an option
- cmd.getParsedOptionValue("Nope");
- assertEquals(0, handler.size());
- }
-
@Test
public void testGetOptionProperties() throws Exception {
final String[] args = {"-Dparam1=value1", "-Dparam2=value2",
"-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar"};
final Options options = new Options();
-
options.addOption(OptionBuilder.withValueSeparator().hasOptionalArgs(2).create('D'));
-
options.addOption(OptionBuilder.withValueSeparator().hasArgs(2).withLongOpt("property").create());
+
options.addOption(Option.builder("D").valueSeparator().optionalArg(true).numberOfArgs(2).build());
+
options.addOption(Option.builder().valueSeparator().numberOfArgs(2).longOpt("property").build());
final Parser parser = new GnuParser();
final CommandLine cl = parser.parse(options, args);
@@ -177,8 +105,8 @@ public class CommandLineTest {
final String[] args = {"-Dparam1=value1", "-Dparam2=value2",
"-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar"};
final Options options = new Options();
- final Option optionD =
OptionBuilder.withValueSeparator().hasOptionalArgs(2).create('D');
- final Option optionProperty =
OptionBuilder.withValueSeparator().hasArgs(2).withLongOpt("property").create();
+ final Option optionD =
Option.builder("D").valueSeparator().numberOfArgs(2).optionalArg(true).build();
+ final Option optionProperty =
Option.builder().valueSeparator().numberOfArgs(2).longOpt("property").build();
options.addOption(optionD);
options.addOption(optionProperty);
@@ -225,89 +153,746 @@ public class CommandLineTest {
}
@Test
- public void testGetParsedOptionValue() throws Exception {
+ public void testBadGetParsedOptionValue() throws Exception {
+
final Options options = new Options();
-
options.addOption(OptionBuilder.hasArg().withType(Number.class).create("i"));
- options.addOption(OptionBuilder.hasArg().create("f"));
+
options.addOption(Option.builder("i").hasArg().type(Number.class).build());
+ options.addOption(Option.builder("c").hasArg().converter(s ->
Count.valueOf(s.toUpperCase())).build());
+
final CommandLineParser parser = new DefaultParser();
- final CommandLine cmd = parser.parse(options, new String[] {"-i",
"123", "-f", "foo"});
+ final CommandLine cmd = parser.parse(options, new String[] {"-i",
"foo", "-c", "bar"});
- assertEquals(123, ((Number) cmd.getParsedOptionValue("i")).intValue());
- assertEquals("foo", cmd.getParsedOptionValue("f"));
+ assertEquals(NumberFormatException.class,
assertThrows(ParseException.class, () ->
cmd.getParsedOptionValue("i")).getCause().getClass());
+ assertEquals(IllegalArgumentException.class,
assertThrows(ParseException.class, () ->
cmd.getParsedOptionValue("c")).getCause().getClass());
}
@Test
- public void testGetParsedOptionValueUsingDefault() throws Exception {
+ public void testNullOption() throws Exception {
final Options options = new Options();
final Option optI =
Option.builder("i").hasArg().type(Number.class).build();
final Option optF = Option.builder("f").hasArg().build();
options.addOption(optI);
options.addOption(optF);
-
final CommandLineParser parser = new DefaultParser();
- final CommandLine cmd = parser.parse(options, new String[] {"-i",
"123"});
+ final CommandLine cmd = parser.parse(options, new String[] {"-i",
"123", "-f", "foo"});
+ assertNull(cmd.getOptionValue((Option) null));
+ assertNull(cmd.getParsedOptionValue((Option) null));
+ assertNull(cmd.getOptionValue((OptionGroup) null));
+ assertNull(cmd.getParsedOptionValue((OptionGroup) null));
+ }
+
+ private void assertWritten(final boolean optDep, final
ByteArrayOutputStream baos) {
+ System.out.flush();
+ if (optDep) {
+ assertEquals("Option 'T''tee': Deprecated",
baos.toString().trim());
+ } else {
+ assertEquals("", baos.toString());
+ }
+ baos.reset();
+ }
+
+ @ParameterizedTest(name = "{0}, {1}")
+ @MethodSource("createOptionValueParameters")
+ public void noDeprecationHandlerTest(final String[] args, final Option
opt, final OptionGroup optionGroup, final boolean optDep,
+ final String optValue, final boolean
grpDep, final String grpValue, final Option grpOpt) throws ParseException {
+ final Options options = new Options().addOptionGroup(optionGroup);
+ final CommandLine commandLine =
DefaultParser.builder().build().parse(options, args);
+ final Supplier<String> thinger = () -> {
+ return "thing";
+ };
final Supplier<String> nullSupplier = null;
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ PrintStream ps = System.out;
+ try {
+ System.setOut(new PrintStream(baos));
+
+ OptionGroup otherGroup = new
OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().build())
+
.addOption(Option.builder().option("p").longOpt("part").hasArg().build());
+ OptionGroup nullGroup = null;
+
+ // test char option
+ assertEquals(optValue, commandLine.getOptionValue(asChar(opt)));
+ assertWritten(optDep, baos);
- assertEquals(123, ((Number)
cmd.getParsedOptionValue(optI)).intValue());
- assertEquals("foo", cmd.getParsedOptionValue(optF, "foo"));
- assertEquals("foo", cmd.getParsedOptionValue(optF, () -> "foo"));
- assertNull(cmd.getParsedOptionValue(optF, null));
- assertNull(cmd.getParsedOptionValue(optF, nullSupplier));
- assertNull(cmd.getParsedOptionValue(optF, () -> null));
-
- assertEquals("foo", cmd.getParsedOptionValue("f", "foo"));
- assertEquals("foo", cmd.getParsedOptionValue("f", () -> "foo"));
- assertNull(cmd.getParsedOptionValue("f", null));
- assertNull(cmd.getParsedOptionValue("f", nullSupplier));
- assertNull(cmd.getParsedOptionValue("f", () -> null));
-
- assertEquals("foo", cmd.getParsedOptionValue('f', "foo"));
- assertEquals("foo", cmd.getParsedOptionValue('f', () -> "foo"));
- assertNull(cmd.getParsedOptionValue('f', null));
- assertNull(cmd.getParsedOptionValue('f', nullSupplier));
- assertNull(cmd.getParsedOptionValue('f', () -> null));
+ assertEquals(optValue == null ? "thing" : optValue,
commandLine.getOptionValue(asChar(opt), "thing"));
+ assertWritten(optDep, baos);
+ assertEquals(optValue == null ? "thing" : optValue,
commandLine.getOptionValue(asChar(opt), thinger));
+ assertWritten(optDep, baos);
+
+ assertEquals(optValue, commandLine.getOptionValue(asChar(opt),
nullSupplier));
+ assertWritten(optDep, baos);
+
+ // test short option arg
+ assertEquals(optValue, commandLine.getOptionValue(opt.getOpt()));
+ assertWritten(optDep, baos);
+
+ assertEquals(optValue == null ? "thing" : optValue,
commandLine.getOptionValue(opt.getOpt(), "thing"));
+ assertWritten(optDep, baos);
+
+ assertEquals(optValue == null ? "thing" : optValue,
commandLine.getOptionValue(opt.getOpt(), thinger));
+ assertWritten(optDep, baos);
+
+ assertEquals(optValue, commandLine.getOptionValue(opt.getOpt(),
nullSupplier));
+ assertWritten(optDep, baos);
+
+ // test long option arg
+ assertEquals(optValue,
commandLine.getOptionValue(opt.getLongOpt()));
+ assertWritten(optDep, baos);
+
+ assertEquals(optValue == null ? "thing" : optValue,
commandLine.getOptionValue(opt.getLongOpt(), "thing"));
+ assertWritten(optDep, baos);
+
+ assertEquals(optValue == null ? "thing" : optValue,
commandLine.getOptionValue(opt.getLongOpt(), thinger));
+ assertWritten(optDep, baos);
+
+ assertEquals(optValue,
commandLine.getOptionValue(opt.getLongOpt(), nullSupplier));
+ assertWritten(optDep, baos);
+
+ // test Option arg
+ assertEquals(optValue, commandLine.getOptionValue(opt));
+ assertWritten(optDep, baos);
+
+ assertEquals(optValue == null ? "thing" : optValue,
commandLine.getOptionValue(opt, "thing"));
+ assertWritten(optDep, baos);
+
+ assertEquals(optValue == null ? "thing" : optValue,
commandLine.getOptionValue(opt, thinger));
+ assertWritten(optDep, baos);
+
+ assertEquals(optValue, commandLine.getOptionValue(opt,
nullSupplier));
+ assertWritten(optDep, baos);
+
+ // test optionGroup arg
+ assertEquals(grpValue, commandLine.getOptionValue(optionGroup));
+ assertWritten(grpDep, baos);
+
+ assertEquals(grpValue == null ? "thing" : grpValue,
commandLine.getOptionValue(optionGroup, "thing"));
+ assertWritten(grpDep, baos);
+
+ assertEquals(grpValue == null ? "thing" : grpValue,
commandLine.getOptionValue(optionGroup, thinger));
+ assertWritten(grpDep, baos);
+
+ assertEquals(grpValue, commandLine.getOptionValue(optionGroup,
nullSupplier));
+ assertWritten(grpDep, baos);
+
+ // test other group arg
+ assertNull(commandLine.getOptionValue(otherGroup));
+ assertWritten(false, baos);
+
+ assertEquals("thing", commandLine.getOptionValue(otherGroup,
"thing"));
+ assertWritten(false, baos);
+
+ assertEquals("thing", commandLine.getOptionValue(otherGroup,
thinger));
+ assertWritten(false, baos);
+
+ assertNull(commandLine.getOptionValue(otherGroup, nullSupplier));
+ assertWritten(false, baos);
+
+ // test null Group arg
+ assertNull(commandLine.getOptionValue(nullGroup));
+ assertWritten(false, baos);
+
+ assertEquals("thing", commandLine.getOptionValue(nullGroup,
"thing"));
+ assertWritten(false, baos);
+
+ assertEquals("thing", commandLine.getOptionValue(nullGroup,
thinger));
+ assertWritten(false, baos);
+
+ assertNull(commandLine.getOptionValue(nullGroup, nullSupplier));
+ assertWritten(false, baos);
+
+ // test not an option
+ assertNull(commandLine.getOptionValue("Nope"));
+ assertWritten(false, baos);
+
+ assertEquals("thing", commandLine.getOptionValue("Nope", "thing"));
+ assertWritten(false, baos);
+
+ assertEquals("thing", commandLine.getOptionValue("Nope", thinger));
+ assertWritten(false, baos);
+
+ assertNull(commandLine.getOptionValue("Nope", nullSupplier));
+ assertWritten(false, baos);
+ } finally {
+ System.setOut(ps);
+ }
}
- @Test
- public void testGetParsedOptionValueWithChar() throws Exception {
- final Options options = new Options();
-
options.addOption(Option.builder("i").hasArg().type(Number.class).build());
- options.addOption(Option.builder("f").hasArg().build());
+ /**
+ * verifies that the deprecation handler has been called only once or not
at all.
+ * @param optDep {@code true} if the dependency should have been logged.
+ * @param handler The list that the deprecation is logged to.
+ * @param opt The option that triggered the logging. May be (@code null}
if {@code optDep} is {@code false}.
+ */
+ void checkHandler(final boolean optDep, final List<Option> handler, final
Option opt) {
+ if (optDep) {
+ assertEquals(1, handler.size());
+ assertEquals(opt, handler.get(0));
+ } else {
+ assertEquals(0, handler.size());
+ }
+ handler.clear();
+ }
- final CommandLineParser parser = new DefaultParser();
- final CommandLine cmd = parser.parse(options, new String[] {"-i",
"123", "-f", "foo"});
+ char asChar(final Option opt) {
+ return opt.getOpt().charAt(0);
+ }
+
+ /**
+ * Test for get option value with and without default values. Verifies
that deprecated options only report as
+ * deprecated once.
+ * @param args the argument strings to parse.
+ * @param opt the option to check for values with.
+ * @param optionGroup the option group to check for values with.
+ * @param optDep {@code true} if the opt is deprecated.
+ * @param optValue The value expected from opt.
+ * @param grpDep {@code true} if the group is deprecated.
+ * @param grpValue the value expected from the group.
+ * @param grpOpt the option that is expected to be processed by the group.
+ * @throws ParseException on parse error.
+ */
+ @ParameterizedTest(name = "{0}, {1}")
+ @MethodSource("createOptionValueParameters")
+ public void getOptionValueTest(final String[] args, final Option opt,
final OptionGroup optionGroup, final boolean optDep,
+ final String optValue, final boolean
grpDep, final String 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).build().parse(options,
args);
+ final Supplier<String> thinger = () -> {
+ return "thing";
+ };
+ OptionGroup otherGroup = new
OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().build())
+
.addOption(Option.builder().option("p").longOpt("part").hasArg().build());
+ OptionGroup nullGroup = null;
+
+ // test char option
+ assertEquals(optValue, commandLine.getOptionValue(asChar(opt)));
+ checkHandler(optDep, handler, opt);
+
+ assertEquals(optValue == null ? "thing" : optValue,
commandLine.getOptionValue(asChar(opt), "thing"));
+ checkHandler(optDep, handler, opt);
+
+ assertEquals(optValue == null ? "thing" : optValue,
commandLine.getOptionValue(asChar(opt), thinger));
+ checkHandler(optDep, handler, opt);
+
+ // test short option arg
+ assertEquals(optValue, commandLine.getOptionValue(opt.getOpt()));
+ checkHandler(optDep, handler, opt);
+
+ assertEquals(optValue == null ? "thing" : optValue,
commandLine.getOptionValue(opt.getOpt(), "thing"));
+ checkHandler(optDep, handler, opt);
+
+ assertEquals(optValue == null ? "thing" : optValue,
commandLine.getOptionValue(opt.getOpt(), thinger));
+ checkHandler(optDep, handler, opt);
+
+ // test long option arg
+ assertEquals(optValue, commandLine.getOptionValue(opt.getLongOpt()));
+ checkHandler(optDep, handler, opt);
+
+ assertEquals(optValue == null ? "thing" : optValue,
commandLine.getOptionValue(opt.getLongOpt(), "thing"));
+ checkHandler(optDep, handler, opt);
+
+ assertEquals(optValue == null ? "thing" : optValue,
commandLine.getOptionValue(opt.getLongOpt(), thinger));
+ checkHandler(optDep, handler, opt);
+
+ // test Option arg
+ assertEquals(optValue, commandLine.getOptionValue(opt));
+ checkHandler(optDep, handler, opt);
+
+ assertEquals(optValue == null ? "thing" : optValue,
commandLine.getOptionValue(opt, "thing"));
+ checkHandler(optDep, handler, opt);
+
+ assertEquals(optValue == null ? "thing" : optValue,
commandLine.getOptionValue(opt, thinger));
+ checkHandler(optDep, handler, opt);
- assertEquals(123, ((Number) cmd.getParsedOptionValue('i')).intValue());
- assertEquals("foo", cmd.getParsedOptionValue('f'));
+ // test option group arg
+ assertEquals(grpValue, commandLine.getOptionValue(optionGroup));
+ checkHandler(grpDep, handler, grpOpt);
+
+ assertEquals(grpValue == null ? "thing" : grpValue,
commandLine.getOptionValue(optionGroup, "thing"));
+ checkHandler(grpDep, handler, grpOpt);
+
+ assertEquals(grpValue == null ? "thing" : grpValue,
commandLine.getOptionValue(optionGroup, thinger));
+ checkHandler(grpDep, handler, grpOpt);
+
+ // test other group arg
+ assertNull(commandLine.getOptionValue(otherGroup));
+ checkHandler(false, handler, grpOpt);
+
+ assertEquals("thing", commandLine.getOptionValue(otherGroup, "thing"));
+ checkHandler(false, handler, grpOpt);
+
+ assertEquals("thing", commandLine.getOptionValue(otherGroup, thinger));
+ checkHandler(false, handler, grpOpt);
+
+ // test null Group arg
+ assertNull(commandLine.getOptionValue(nullGroup));
+ checkHandler(false, handler, grpOpt);
+
+ assertEquals("thing", commandLine.getOptionValue(nullGroup, "thing"));
+ checkHandler(false, handler, grpOpt);
+
+ assertEquals("thing", commandLine.getOptionValue(nullGroup, thinger));
+ checkHandler(false, handler, grpOpt);
+
+ // test not an option
+ assertNull(commandLine.getOptionValue("Nope"));
+ checkHandler(false, handler, opt);
+
+ assertEquals("thing", commandLine.getOptionValue("Nope", "thing"));
+ checkHandler(false, handler, opt);
+
+ assertEquals("thing", commandLine.getOptionValue("Nope", thinger));
+ checkHandler(false, handler, opt);
}
- @Test
- public void testGetParsedOptionValueWithOption() throws Exception {
- final Options options = new Options();
- final Option optI =
Option.builder("i").hasArg().type(Number.class).build();
- final Option optF = Option.builder("f").hasArg().build();
- options.addOption(optI);
- options.addOption(optF);
+ private static Stream<Arguments> createOptionValueParameters() throws
ParseException {
+ List<Arguments> lst = new ArrayList<>();
+ final Option optT =
Option.builder().option("T").longOpt("tee").deprecated().optionalArg(true).build();
+ final Option optU =
Option.builder("U").longOpt("you").optionalArg(true).build();
+ OptionGroup optionGroup = new
OptionGroup().addOption(optT).addOption(optU);
- final CommandLineParser parser = new DefaultParser();
- final CommandLine cmd = parser.parse(options, new String[] {"-i",
"123", "-f", "foo"});
+ // T set
+ lst.add(Arguments.of(new String[] {"-T"}, optT, optionGroup, true,
null, true, null, optT));
+ lst.add(Arguments.of(new String[] {"-T", "foo"}, optT, optionGroup,
true, "foo", true, "foo", optT));
+ lst.add(Arguments.of(new String[] {"--tee"}, optT, optionGroup, true,
null, true, null, optT));
+ lst.add(Arguments.of(new String[] {"--tee", "foo"}, optT, optionGroup,
true, "foo", true, "foo", optT));
+
+ lst.add(Arguments.of(new String[] {"-U"}, optT, optionGroup, false,
null, false, null, optU));
+ lst.add(Arguments.of(new String[] {"-U", "foo"}, optT, optionGroup,
false, null, false, "foo", optU));
+ lst.add(Arguments.of(new String[] {"--you"}, optT, optionGroup, false,
null, false, null, optU));
+ lst.add(Arguments.of(new String[] {"--you", "foo"}, optT, optionGroup,
false, null, false, "foo", optU));
- assertEquals(123, ((Number)
cmd.getParsedOptionValue(optI)).intValue());
- assertEquals("foo", cmd.getParsedOptionValue(optF));
+
+ // U set
+ lst.add(Arguments.of(new String[] {"-T"}, optU, optionGroup, false,
null, true, null, optT));
+ lst.add(Arguments.of(new String[] {"-T", "foo"}, optU, optionGroup,
false, null, true, "foo", optT));
+ lst.add(Arguments.of(new String[] {"--tee"}, optU, optionGroup, false,
null, true, null, optT));
+ lst.add(Arguments.of(new String[] {"--tee", "foo"}, optU, optionGroup,
false, null, true, "foo", optT));
+
+ lst.add(Arguments.of(new String[] {"-U"}, optU, optionGroup, false,
null, false, null, optU));
+ lst.add(Arguments.of(new String[] {"-U", "foo"}, optU, optionGroup,
false, "foo", false, "foo", optU));
+ lst.add(Arguments.of(new String[] {"--you"}, optU, optionGroup, false,
null, false, null, optU));
+ lst.add(Arguments.of(new String[] {"--you", "foo"}, optU,
optionGroup, false, "foo", false, "foo", optU));
+
+ return lst.stream();
}
- @Test
- public void testNullOption() throws Exception {
- final Options options = new Options();
- final Option optI =
Option.builder("i").hasArg().type(Number.class).build();
- final Option optF = Option.builder("f").hasArg().build();
- options.addOption(optI);
- options.addOption(optF);
- final CommandLineParser parser = new DefaultParser();
- final CommandLine cmd = parser.parse(options, new String[] {"-i",
"123", "-f", "foo"});
- assertNull(cmd.getOptionValue((Option) null));
- assertNull(cmd.getParsedOptionValue((Option) null));
+
+ /**
+ * Test for get option values with and without default values. Verifies
that deprecated options only report as
+ * deprecated once.
+ * @param args the argument strings to parse.
+ * @param opt the option to check for values with.
+ * @param optionGroup the option group to check for values with.
+ * @param optDep {@code true} if the opt is deprecated.
+ * @param optValue The value expected from opt.
+ * @param grpDep {@code true} if the group is deprecated.
+ * @param grpValue the value expected from the group.
+ * @param grpOpt the option that is expected to be processed by the group.
+ * @throws ParseException on parse error.
+ */
+ @ParameterizedTest(name = "{0}, {1}")
+ @MethodSource("createOptionValuesParameters")
+ public void getOptionValuesTest(final String[] args, final Option opt,
final OptionGroup optionGroup, final boolean optDep,
+ final String[] optValue, final boolean
grpDep, final String[] 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).build().parse(options,
args);
+ OptionGroup otherGroup = new
OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().build())
+
.addOption(Option.builder().option("p").longOpt("part").hasArg().build());
+ OptionGroup nullGroup = null;
+
+ // test char option arg
+ assertArrayEquals(optValue, commandLine.getOptionValues(asChar(opt)));
+ checkHandler(optDep, handler, opt);
+
+ // test short option arg
+ assertArrayEquals(optValue, commandLine.getOptionValues(opt.getOpt()));
+ checkHandler(optDep, handler, opt);
+
+ // test long option arg
+ assertArrayEquals(optValue,
commandLine.getOptionValues(opt.getLongOpt()));
+ checkHandler(optDep, handler, opt);
+
+ // test Option arg
+ assertArrayEquals(optValue, commandLine.getOptionValues(opt));
+ checkHandler(optDep, handler, opt);
+
+
+ // test OptionGroup arg
+ assertArrayEquals(grpValue, commandLine.getOptionValues(optionGroup));
+ checkHandler(grpDep, handler, grpOpt);
+
+ // test not an option
+ assertNull(commandLine.getOptionValues("Nope"));
+ checkHandler(false, handler, opt);
+
+ // test other group arg
+ assertNull(commandLine.getOptionValues(otherGroup));
+ checkHandler(false, handler, grpOpt);
+
+ // test null group arg
+ assertNull(commandLine.getOptionValues(nullGroup));
+ checkHandler(false, handler, grpOpt);
+ }
+
+ private static Stream<Arguments> createOptionValuesParameters() throws
ParseException {
+ List<Arguments> lst = new ArrayList<>();
+ final Option optT =
Option.builder().option("T").longOpt("tee").numberOfArgs(2).deprecated().optionalArg(true).build();
+ final Option optU =
Option.builder("U").longOpt("you").numberOfArgs(2).optionalArg(true).build();
+ final OptionGroup optionGroup = new
OptionGroup().addOption(optT).addOption(optU);
+
+ String[] foobar = { "foo", "bar" };
+ // T set
+ lst.add(Arguments.of(new String[] {"-T"}, optT, optionGroup, true,
null, true, null, optT));
+ lst.add(Arguments.of(new String[] {"-T", "foo", "bar"}, optT,
optionGroup, true, foobar, true, foobar, optT));
+ lst.add(Arguments.of(new String[] {"--tee"}, optT, optionGroup, true,
null, true, null, optT));
+ lst.add(Arguments.of(new String[] {"--tee", "foo", "bar"}, optT,
optionGroup, true, foobar, true, foobar, optT));
+
+ lst.add(Arguments.of(new String[] {"-U"}, optT, optionGroup, false,
null, false, null, optU));
+ lst.add(Arguments.of(new String[] {"-U", "foo", "bar"}, optT,
optionGroup, false, null, false, foobar, optU));
+ lst.add(Arguments.of(new String[] {"--you"}, optT, optionGroup, false,
null, false, null, optU));
+ lst.add(Arguments.of(new String[] {"--you", "foo", "bar"}, optT,
optionGroup, false, null, false, foobar, optU));
+
+
+ // U set
+ lst.add(Arguments.of(new String[] {"-T"}, optU, optionGroup, false,
null, true, null, optT));
+ lst.add(Arguments.of(new String[] {"-T", "foo", "bar"}, optU,
optionGroup, false, null, true, foobar, optT));
+ lst.add(Arguments.of(new String[] {"--tee"}, optU, optionGroup, false,
null, true, null, optT));
+ lst.add(Arguments.of(new String[] {"--tee", "foo", "bar"}, optU,
optionGroup, false, null, true, foobar, optT));
+
+ lst.add(Arguments.of(new String[] {"-U"}, optU, optionGroup, false,
null, false, null, optU));
+ lst.add(Arguments.of(new String[] {"-U", "foo", "bar"}, optU,
optionGroup, false, foobar, false, foobar, optU));
+ lst.add(Arguments.of(new String[] {"--you"}, optU, optionGroup, false,
null, false, null, optU));
+ lst.add(Arguments.of(new String[] {"--you", "foo", "bar"}, optU,
optionGroup, false, foobar, false, foobar, optU));
+
+ return lst.stream();
+ }
+
+ /**
+ * Tests the hasOption calls.
+ * @param args the argument strings to parse.
+ * @param opt the option to check for values with.
+ * @param optionGroup the option group to check for values with.
+ * @param optDep {@code true} if the opt is deprecated.
+ * @param has {@code true} if the opt is present.
+ * @param grpDep {@code true} if the group is deprecated.
+ * @param hasGrp {@code true} if the group is present.
+ * @param grpOpt the option that is expected to be processed by the group.
+ * @throws ParseException on parsing error.
+ */
+ @ParameterizedTest(name = "{0}, {1}")
+ @MethodSource("createHasOptionParameters")
+ public void hasOptionTest(final String[] args, final Option opt, final
OptionGroup optionGroup, final boolean optDep,
+ final boolean has, final boolean grpDep, final
boolean hasGrp, 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).build().parse(options,
args);
+ 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;
+
+ // test char option arg
+ assertEquals(has, commandLine.hasOption(asChar(opt)));
+ checkHandler(optDep, handler, opt);
+
+ // test short option arg
+ assertEquals(has, commandLine.hasOption(opt.getOpt()));
+ checkHandler(optDep, handler, opt);
+
+ // test long option arg
+ assertEquals(has, commandLine.hasOption(opt.getLongOpt()));
+ checkHandler(optDep, handler, opt);
+
+ // test Option arg
+ assertEquals(has, commandLine.hasOption(opt));
+ checkHandler(optDep, handler, opt);
+
+ // test OptionGroup arg
+ assertEquals(hasGrp, commandLine.hasOption(optionGroup));
+ checkHandler(grpDep, handler, grpOpt);
+
+ // test other group arg
+ assertFalse(commandLine.hasOption(otherGroup));
+ checkHandler(false, handler, grpOpt);
+
+
+ // test null group arg
+ assertFalse(commandLine.hasOption(nullGroup));
+ checkHandler(false, handler, grpOpt);
+
+ // test not an option
+ assertFalse(commandLine.hasOption("Nope"));
+ checkHandler(false, handler, opt);
+ }
+
+ /**
+ * Tests the hasOption calls.
+ * @param args the argument strings to parse.
+ * @param opt the option to check for values with.
+ * @param optionGroup the option group to check for values with.
+ * @param optDep {@code true} if the opt is deprecated.
+ * @param has {@code true} if the opt is present.
+ * @param grpDep {@code true} if the group is deprecated.
+ * @param hasGrp {@code true} if the group is present.
+ * @param grpOpt the option that is expected to be processed by the group.
+ * @throws ParseException on parsing error.
+ */
+ @ParameterizedTest(name = "{0}, {1}")
+ @MethodSource("createHasOptionParameters")
+ public void hasOptionNoDeprecationHandlerTest(final String[] args, final
Option opt, final OptionGroup optionGroup, final boolean optDep,
+ final boolean has, final boolean grpDep, final
boolean hasGrp, final Option grpOpt) throws ParseException {
+ final Options options = new Options().addOptionGroup(optionGroup);
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final CommandLine commandLine =
DefaultParser.builder().build().parse(options, args);
+ PrintStream ps = System.out;
+ try {
+ System.setOut(new PrintStream(baos));
+
+ // test char option arg
+ assertEquals(has, commandLine.hasOption(asChar(opt)));
+ assertWritten(optDep, baos);
+
+
+ // test short option arg
+ assertEquals(has, commandLine.hasOption(opt.getOpt()));
+ assertWritten(optDep, baos);
+
+ // test long option arg
+ assertEquals(has, commandLine.hasOption(opt.getLongOpt()));
+ assertWritten(optDep, baos);
+
+ // test Option arg
+ assertEquals(has, commandLine.hasOption(opt));
+ assertWritten(optDep, baos);
+
+ // test OptionGroup arg
+ assertEquals(hasGrp, commandLine.hasOption(optionGroup));
+ assertWritten(grpDep, baos);
+
+ // test not an option
+ assertFalse(commandLine.hasOption("Nope"));
+ assertWritten(false, baos);
+ } finally {
+ System.setOut(ps);
+ }
+ }
+
+ /**
+ * Tests the hasOption calls.
+ * @param args the argument strings to parse.
+ * @param opt the option to check for values with.
+ * @param optionGroup the option group to check for values with.
+ * @param optDep {@code true} if the opt is deprecated.
+ * @param has {@code true} if the opt is present.
+ * @param grpDep {@code true} if the group is deprecated.
+ * @param hasGrp {@code true} if the group is present.
+ * @param grpOpt the option that is expected to be processed by the group.
+ * @throws ParseException on parsing error.
+ */
+ @ParameterizedTest(name = "{0}, {1}")
+ @MethodSource("createHasOptionParameters")
+ public void hasOptionNullDeprecationHandlerTest(final String[] args, final
Option opt, final OptionGroup optionGroup, final boolean optDep,
+ final boolean has, final
boolean grpDep, final boolean hasGrp, final Option grpOpt) throws
ParseException {
+ final Options options = new Options().addOptionGroup(optionGroup);
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final CommandLine commandLine =
DefaultParser.builder().setDeprecatedHandler(null).build().parse(options, args);
+ PrintStream ps = System.out;
+ try {
+ System.setOut(new PrintStream(baos));
+
+ // test char option arg
+ assertEquals(has, commandLine.hasOption(asChar(opt)));
+ assertWritten(false, baos);
+
+
+ // test short option arg
+ assertEquals(has, commandLine.hasOption(opt.getOpt()));
+ assertWritten(false, baos);
+
+ // test long option arg
+ assertEquals(has, commandLine.hasOption(opt.getLongOpt()));
+ assertWritten(false, baos);
+
+ // test Option arg
+ assertEquals(has, commandLine.hasOption(opt));
+ assertWritten(false, baos);
+
+ // test OptionGroup arg
+ assertEquals(hasGrp, commandLine.hasOption(optionGroup));
+ assertWritten(false, baos);
+
+ // test not an option
+ assertFalse(commandLine.hasOption("Nope"));
+ assertWritten(false, baos);
+ } finally {
+ System.setOut(ps);
+ }
+ }
+
+ private static Stream<Arguments> createHasOptionParameters() throws
ParseException {
+ List<Arguments> lst = new ArrayList<>();
+ final Option optT =
Option.builder().option("T").longOpt("tee").deprecated().optionalArg(true).build();
+ final Option optU =
Option.builder("U").longOpt("you").optionalArg(true).build();
+ final OptionGroup optionGroup = new
OptionGroup().addOption(optT).addOption(optU);
+
+ String[] foobar = { "foo", "bar" };
+ // T set
+ lst.add(Arguments.of(new String[] {"-T"}, optT, optionGroup, true,
true, true, true, optT));
+ lst.add(Arguments.of(new String[] {"-T", "foo"}, optT, optionGroup,
true, true, true, true, optT));
+ lst.add(Arguments.of(new String[] {"--tee"}, optT, optionGroup, true,
true, true, true, optT));
+ lst.add(Arguments.of(new String[] {"--tee", "foo"}, optT, optionGroup,
true, true, true, true, optT));
+
+ lst.add(Arguments.of(new String[] {"-U"}, optT, optionGroup, false,
false, false, true, optU));
+ lst.add(Arguments.of(new String[] {"-U", "foo", "bar"}, optT,
optionGroup, false, false, false, true, optU));
+ 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));
+ lst.add(Arguments.of(new String[] {"--tee"}, optU, optionGroup, false,
false, true, true, optT));
+ lst.add(Arguments.of(new String[] {"--tee", "foo", "bar"}, optU,
optionGroup, false, false, true, true, optT));
+
+ lst.add(Arguments.of(new String[] {"-U"}, optU, optionGroup, false,
true, false, true, optU));
+ lst.add(Arguments.of(new String[] {"-U", "foo", "bar"}, optU,
optionGroup, false, true, false, true, optU));
+ lst.add(Arguments.of(new String[] {"--you"}, optU, optionGroup, false,
true, false, true, optU));
+ lst.add(Arguments.of(new String[] {"--you", "foo", "bar"}, optU,
optionGroup, false, true, false, true, optU));
+
+ return lst.stream();
+ }
+
+ @ParameterizedTest(name = "{0}, {1}")
+ @MethodSource("createParsedOptionValueParameters")
+ public void getParsedOptionValueTest(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).build().parse(options,
args);
+ final Supplier<Integer> thinger = () -> {
+ return 2;
+ };
+ OptionGroup otherGroup = new
OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().build())
+
.addOption(Option.builder().option("p").longOpt("part").hasArg().build());
+ OptionGroup nullGroup = null;
+ Integer thing = 2;
+
+ // test char option arg
+ assertEquals(optValue, commandLine.getParsedOptionValue(asChar(opt)));
+ checkHandler(optDep, handler, opt);
+
+ assertEquals(optValue == null ? thing : optValue,
commandLine.getParsedOptionValue(asChar(opt), thing));
+ checkHandler(optDep, handler, opt);
+
+ assertEquals(optValue == null ? thing : optValue,
commandLine.getParsedOptionValue(asChar(opt), thinger));
+ checkHandler(optDep, handler, opt);
+
+ // test short option arg
+ assertEquals(optValue, commandLine.getParsedOptionValue(opt.getOpt()));
+ checkHandler(optDep, handler, opt);
+
+ assertEquals(optValue == null ? thing : optValue,
commandLine.getParsedOptionValue(opt.getOpt(), thing));
+ checkHandler(optDep, handler, opt);
+
+ assertEquals(optValue == null ? thing : optValue,
commandLine.getParsedOptionValue(opt.getOpt(), thinger));
+ checkHandler(optDep, handler, opt);
+
+ // test long option arg
+ assertEquals(optValue,
commandLine.getParsedOptionValue(opt.getLongOpt()));
+ checkHandler(optDep, handler, opt);
+
+ assertEquals(optValue == null ? thing : optValue,
commandLine.getParsedOptionValue(opt.getLongOpt(), thing));
+ checkHandler(optDep, handler, opt);
+
+ 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);
+
+ assertEquals(optValue == null ? thing : optValue,
commandLine.getParsedOptionValue(opt, thing));
+ checkHandler(optDep, handler, opt);
+
+ assertEquals(optValue == null ? thing : optValue,
commandLine.getParsedOptionValue(opt, thinger));
+ checkHandler(optDep, handler, opt);
+
+ // test OptionGroup arg
+ assertEquals(grpValue, commandLine.getParsedOptionValue(optionGroup));
+ checkHandler(grpDep, handler, grpOpt);
+
+ assertEquals(grpValue == null ? thing : grpValue,
commandLine.getParsedOptionValue(optionGroup, thing));
+ checkHandler(grpDep, handler, grpOpt);
+
+ assertEquals(grpValue == null ? thing : grpValue,
commandLine.getParsedOptionValue(optionGroup, thinger));
+ checkHandler(grpDep, handler, grpOpt);
+
+ // test other Group arg
+ assertNull(commandLine.getParsedOptionValue(otherGroup));
+ checkHandler(false, handler, grpOpt);
+
+ assertEquals(thing, commandLine.getParsedOptionValue(otherGroup,
thing));
+ checkHandler(false, handler, grpOpt);
+
+ assertEquals(thing, commandLine.getParsedOptionValue(otherGroup,
thinger));
+ checkHandler(false, handler, grpOpt);
+
+ // test null Group arg
+ assertNull(commandLine.getParsedOptionValue(nullGroup));
+ checkHandler(false, handler, grpOpt);
+
+ assertEquals(thing, commandLine.getParsedOptionValue(nullGroup,
thing));
+ checkHandler(false, handler, grpOpt);
+
+ assertEquals(thing, commandLine.getParsedOptionValue(nullGroup,
thinger));
+ checkHandler(false, handler, grpOpt);
+
+
+ // test not an option
+ assertNull(commandLine.getParsedOptionValue("Nope"));
+ checkHandler(false, handler, opt);
+
+ assertEquals(thing, commandLine.getParsedOptionValue("Nope", thing));
+ checkHandler(false, handler, opt);
+
+ assertEquals(thing, commandLine.getParsedOptionValue("Nope", thinger));
+ checkHandler(false, handler, opt);
+ }
+
+ private static Stream<Arguments> createParsedOptionValueParameters()
throws ParseException {
+ List<Arguments> lst = new ArrayList<>();
+ Option optT =
Option.builder().option("T").longOpt("tee").deprecated().type(Integer.class).optionalArg(true).build();
+ Option optU =
Option.builder("U").longOpt("you").type(Integer.class).optionalArg(true).build();
+ OptionGroup optionGroup = new
OptionGroup().addOption(optT).addOption(optU);
+ Integer expected = Integer.valueOf(1);
+
+ // T set
+ lst.add(Arguments.of(new String[] {"-T"}, optT, optionGroup, true,
null, true, null, optT));
+ lst.add(Arguments.of(new String[] {"-T", "1"}, 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"}, 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"}, 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"}, 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));
+ lst.add(Arguments.of(new String[] {"--tee"}, optU, optionGroup, false,
null, true, null, optT));
+ lst.add(Arguments.of(new String[] {"--tee", "1"}, 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"}, 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"}, optU, optionGroup,
false, expected, false, expected, optU));
+
+ return lst.stream();
}
}