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
commit 7ecbc0ce89af46b513169e149fba8657430303a0 Author: Gary Gregory <[email protected]> AuthorDate: Sat Aug 10 08:53:36 2024 -0400 Sort members --- .../org/apache/commons/cli/CommandLineTest.java | 1064 ++++++++++---------- 1 file changed, 532 insertions(+), 532 deletions(-) diff --git a/src/test/java/org/apache/commons/cli/CommandLineTest.java b/src/test/java/org/apache/commons/cli/CommandLineTest.java index 31998da..6fefd89 100644 --- a/src/test/java/org/apache/commons/cli/CommandLineTest.java +++ b/src/test/java/org/apache/commons/cli/CommandLineTest.java @@ -41,280 +41,149 @@ public class CommandLineTest { private enum Count { ONE, TWO, THREE } - @Test - public void testBuilder() { - final CommandLine.Builder builder = new CommandLine.Builder(); - builder.addArg("foo").addArg("bar"); - builder.addOption(Option.builder("T").build()); - final CommandLine cmd = builder.build(); - - assertEquals("foo", cmd.getArgs()[0]); - assertEquals("bar", cmd.getArgList().get(1)); - assertEquals("T", cmd.getOptions()[0].getOpt()); - } - - @Test - public void testBuilderNullArgs() { - final CommandLine.Builder builder = new CommandLine.Builder(); - builder.addArg(null).addArg(null); - builder.addOption(Option.builder("T").build()); - final CommandLine cmd = builder.build(); - - assertEquals(0, cmd.getArgs().length); - assertEquals("T", cmd.getOptions()[0].getOpt()); - } - - @Test - public void testBuilderNullOption() { - final CommandLine.Builder builder = new CommandLine.Builder(); - builder.addArg("foo").addArg("bar"); - builder.addOption(null); - builder.addOption(null); - builder.addOption(null); - final CommandLine cmd = builder.build(); + private static Stream<Arguments> createHasOptionParameters() throws ParseException { + final 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); - assertEquals("foo", cmd.getArgs()[0]); - assertEquals("bar", cmd.getArgList().get(1)); - assertEquals(0, cmd.getOptions().length); - } + final 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)); - @Test - public void testGetOptionProperties() throws Exception { - final String[] args = {"-Dparam1=value1", "-Dparam2=value2", "-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar"}; + 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)); - final Options options = new Options(); - 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); + // 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)); - final Properties props = cl.getOptionProperties("D"); - assertNotNull(props, "null properties"); - assertEquals(4, props.size(), "number of properties in " + props); - assertEquals("value1", props.getProperty("param1"), "property 1"); - assertEquals("value2", props.getProperty("param2"), "property 2"); - assertEquals("true", props.getProperty("param3"), "property 3"); - assertEquals("value4", props.getProperty("param4"), "property 4"); + 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)); - assertEquals("bar", cl.getOptionProperties("property").getProperty("foo"), "property with long format"); + return lst.stream(); } - @Test - public void testGetOptionPropertiesWithOption() throws Exception { - final String[] args = {"-Dparam1=value1", "-Dparam2=value2", "-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar"}; - - final Options options = new Options(); - 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); - - final Parser parser = new GnuParser(); - final CommandLine cl = parser.parse(options, args); - - final Properties props = cl.getOptionProperties(optionD); - assertNotNull(props, "null properties"); - assertEquals(4, props.size(), "number of properties in " + props); - assertEquals("value1", props.getProperty("param1"), "property 1"); - assertEquals("value2", props.getProperty("param2"), "property 2"); - assertEquals("true", props.getProperty("param3"), "property 3"); - assertEquals("value4", props.getProperty("param4"), "property 4"); - - assertEquals("bar", cl.getOptionProperties(optionProperty).getProperty("foo"), "property with long format"); - } + private static Stream<Arguments> createOptionValueParameters() throws ParseException { + final 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); - @Test - public void testGetOptionsBuilder() { - final CommandLine cmd = CommandLine.builder().build(); - assertNotNull(cmd.getOptions()); - assertEquals(0, cmd.getOptions().length); + // 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)); - cmd.addOption(null); - cmd.addOption(new Option("a", null)); - cmd.addOption(new Option("b", null)); - cmd.addOption(new Option("c", null)); + 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(3, cmd.getOptions().length); - } - @Test - public void testGetOptionsCtor() { - final CommandLine cmd = new CommandLine(); - assertNotNull(cmd.getOptions()); - assertEquals(0, cmd.getOptions().length); + // 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)); - cmd.addOption(new Option("a", null)); - cmd.addOption(new Option("b", null)); - cmd.addOption(new Option("c", null)); - cmd.addOption(null); + 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)); - assertEquals(3, cmd.getOptions().length); + return lst.stream(); } - @Test - public void testBadGetParsedOptionValue() throws Exception { + private static Stream<Arguments> createOptionValuesParameters() throws ParseException { + final 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); - final Options options = new Options(); - options.addOption(Option.builder("i").hasArg().type(Number.class).build()); - options.addOption(Option.builder("c").hasArg().converter(s -> Count.valueOf(s.toUpperCase())).build()); + final 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)); - final CommandLineParser parser = new DefaultParser(); - final CommandLine cmd = parser.parse(options, new String[] {"-i", "foo", "-c", "bar"}); - assertEquals(NumberFormatException.class, assertThrows(ParseException.class, () -> cmd.getParsedOptionValue("i")).getCause().getClass()); - assertEquals(IllegalArgumentException.class, assertThrows(ParseException.class, () -> cmd.getParsedOptionValue("c")).getCause().getClass()); - } + // 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)); - @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)); - assertNull(cmd.getOptionValue((OptionGroup) null)); - assertNull(cmd.getParsedOptionValue((OptionGroup) null)); - } + 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)); - 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(); + return lst.stream(); } - @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 = () -> "thing"; - final Supplier<String> nullSupplier = null; - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final PrintStream ps = System.out; - try { - System.setOut(new PrintStream(baos)); - - 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 - assertEquals(optValue, commandLine.getOptionValue(asChar(opt))); - assertWritten(optDep, baos); - - 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); + private static Stream<Arguments> createParsedOptionValueParameters() throws ParseException { + final List<Arguments> lst = new ArrayList<>(); + final Option optT = Option.builder().option("T").longOpt("tee").deprecated().type(Integer.class).optionalArg(true).build(); + final Option optU = Option.builder("U").longOpt("you").type(Integer.class).optionalArg(true).build(); + final OptionGroup optionGroup = new OptionGroup().addOption(optT).addOption(optU); + final Integer expected = Integer.valueOf(1); - // test null Group arg - assertNull(commandLine.getOptionValue(nullGroup)); - assertWritten(false, baos); + // 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)); - assertEquals("thing", commandLine.getOptionValue(nullGroup, "thing")); - assertWritten(false, baos); + 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)); - assertEquals("thing", commandLine.getOptionValue(nullGroup, thinger)); - assertWritten(false, baos); - assertNull(commandLine.getOptionValue(nullGroup, nullSupplier)); - assertWritten(false, baos); + // 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)); - // test not an option - assertNull(commandLine.getOptionValue("Nope")); - assertWritten(false, baos); + 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)); - assertEquals("thing", commandLine.getOptionValue("Nope", "thing")); - assertWritten(false, baos); + return lst.stream(); + } - assertEquals("thing", commandLine.getOptionValue("Nope", thinger)); - assertWritten(false, baos); + char asChar(final Option opt) { + return opt.getOpt().charAt(0); + } - assertNull(commandLine.getOptionValue("Nope", nullSupplier)); - assertWritten(false, baos); - } finally { - System.setOut(ps); + 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(); } /** @@ -333,8 +202,62 @@ public class CommandLineTest { handler.clear(); } - char asChar(final Option opt) { - return opt.getOpt().charAt(0); + /** + * 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); + 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 + 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); } /** @@ -443,184 +366,99 @@ public class CommandLineTest { checkHandler(false, handler, opt); } - private static Stream<Arguments> createOptionValueParameters() throws ParseException { - final 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); - - // 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)); - - - // 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 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); - 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 - 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 { - final 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); - - final 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 { + @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 = () -> 2; 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; // test char option arg - assertEquals(has, commandLine.hasOption(asChar(opt))); + 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(has, commandLine.hasOption(opt.getOpt())); + 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(has, commandLine.hasOption(opt.getLongOpt())); + 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(has, commandLine.hasOption(opt)); + 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(hasGrp, commandLine.hasOption(optionGroup)); + assertEquals(grpValue, commandLine.getParsedOptionValue(optionGroup)); checkHandler(grpDep, handler, grpOpt); - // test other group arg - assertFalse(commandLine.hasOption(otherGroup)); + 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 - assertFalse(commandLine.hasOption(nullGroup)); + // 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 - assertFalse(commandLine.hasOption("Nope")); + 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); } @@ -704,189 +542,351 @@ public class CommandLineTest { assertWritten(false, baos); - // test short option arg - assertEquals(has, commandLine.hasOption(opt.getOpt())); + // 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); + } + } + + /** + * 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); + } + + @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 = () -> "thing"; + final Supplier<String> nullSupplier = null; + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final PrintStream ps = System.out; + try { + System.setOut(new PrintStream(baos)); + + 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 + assertEquals(optValue, commandLine.getOptionValue(asChar(opt))); + assertWritten(optDep, baos); + + 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 long option arg - assertEquals(has, commandLine.hasOption(opt.getLongOpt())); + // test not an option + assertNull(commandLine.getOptionValue("Nope")); assertWritten(false, baos); - // test Option arg - assertEquals(has, commandLine.hasOption(opt)); + assertEquals("thing", commandLine.getOptionValue("Nope", "thing")); assertWritten(false, baos); - // test OptionGroup arg - assertEquals(hasGrp, commandLine.hasOption(optionGroup)); + assertEquals("thing", commandLine.getOptionValue("Nope", thinger)); assertWritten(false, baos); - // test not an option - assertFalse(commandLine.hasOption("Nope")); + assertNull(commandLine.getOptionValue("Nope", nullSupplier)); assertWritten(false, baos); } finally { System.setOut(ps); } } - private static Stream<Arguments> createHasOptionParameters() throws ParseException { - final 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); - - final 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)); + @Test + public void testBadGetParsedOptionValue() throws Exception { + final Options options = new Options(); + options.addOption(Option.builder("i").hasArg().type(Number.class).build()); + options.addOption(Option.builder("c").hasArg().converter(s -> Count.valueOf(s.toUpperCase())).build()); - // 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)); + final CommandLineParser parser = new DefaultParser(); + final CommandLine cmd = parser.parse(options, new String[] {"-i", "foo", "-c", "bar"}); - return lst.stream(); + assertEquals(NumberFormatException.class, assertThrows(ParseException.class, () -> cmd.getParsedOptionValue("i")).getCause().getClass()); + assertEquals(IllegalArgumentException.class, assertThrows(ParseException.class, () -> cmd.getParsedOptionValue("c")).getCause().getClass()); } - @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 = () -> 2; - 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; - - // 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 + public void testBuilder() { + final CommandLine.Builder builder = new CommandLine.Builder(); + builder.addArg("foo").addArg("bar"); + builder.addOption(Option.builder("T").build()); + final CommandLine cmd = builder.build(); - // 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("foo", cmd.getArgs()[0]); + assertEquals("bar", cmd.getArgList().get(1)); + assertEquals("T", cmd.getOptions()[0].getOpt()); + } - assertEquals(optValue == null ? thing : optValue, commandLine.getParsedOptionValue(opt, thinger)); - checkHandler(optDep, handler, opt); + @Test + public void testBuilderNullArgs() { + final CommandLine.Builder builder = new CommandLine.Builder(); + builder.addArg(null).addArg(null); + builder.addOption(Option.builder("T").build()); + final CommandLine cmd = builder.build(); - // test OptionGroup arg - assertEquals(grpValue, commandLine.getParsedOptionValue(optionGroup)); - checkHandler(grpDep, handler, grpOpt); + assertEquals(0, cmd.getArgs().length); + assertEquals("T", cmd.getOptions()[0].getOpt()); + } - assertEquals(grpValue == null ? thing : grpValue, commandLine.getParsedOptionValue(optionGroup, thing)); - checkHandler(grpDep, handler, grpOpt); + @Test + public void testBuilderNullOption() { + final CommandLine.Builder builder = new CommandLine.Builder(); + builder.addArg("foo").addArg("bar"); + builder.addOption(null); + builder.addOption(null); + builder.addOption(null); + final CommandLine cmd = builder.build(); - assertEquals(grpValue == null ? thing : grpValue, commandLine.getParsedOptionValue(optionGroup, thinger)); - checkHandler(grpDep, handler, grpOpt); + assertEquals("foo", cmd.getArgs()[0]); + assertEquals("bar", cmd.getArgList().get(1)); + assertEquals(0, cmd.getOptions().length); + } - // test other Group arg - assertNull(commandLine.getParsedOptionValue(otherGroup)); - checkHandler(false, handler, grpOpt); + @Test + public void testGetOptionProperties() throws Exception { + final String[] args = {"-Dparam1=value1", "-Dparam2=value2", "-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar"}; - assertEquals(thing, commandLine.getParsedOptionValue(otherGroup, thing)); - checkHandler(false, handler, grpOpt); + final Options options = new Options(); + options.addOption(Option.builder("D").valueSeparator().optionalArg(true).numberOfArgs(2).build()); + options.addOption(Option.builder().valueSeparator().numberOfArgs(2).longOpt("property").build()); - assertEquals(thing, commandLine.getParsedOptionValue(otherGroup, thinger)); - checkHandler(false, handler, grpOpt); + final Parser parser = new GnuParser(); + final CommandLine cl = parser.parse(options, args); - // test null Group arg - assertNull(commandLine.getParsedOptionValue(nullGroup)); - checkHandler(false, handler, grpOpt); + final Properties props = cl.getOptionProperties("D"); + assertNotNull(props, "null properties"); + assertEquals(4, props.size(), "number of properties in " + props); + assertEquals("value1", props.getProperty("param1"), "property 1"); + assertEquals("value2", props.getProperty("param2"), "property 2"); + assertEquals("true", props.getProperty("param3"), "property 3"); + assertEquals("value4", props.getProperty("param4"), "property 4"); - assertEquals(thing, commandLine.getParsedOptionValue(nullGroup, thing)); - checkHandler(false, handler, grpOpt); + assertEquals("bar", cl.getOptionProperties("property").getProperty("foo"), "property with long format"); + } - assertEquals(thing, commandLine.getParsedOptionValue(nullGroup, thinger)); - checkHandler(false, handler, grpOpt); + @Test + public void testGetOptionPropertiesWithOption() throws Exception { + final String[] args = {"-Dparam1=value1", "-Dparam2=value2", "-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar"}; + final Options options = new Options(); + 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); - // test not an option - assertNull(commandLine.getParsedOptionValue("Nope")); - checkHandler(false, handler, opt); + final Parser parser = new GnuParser(); + final CommandLine cl = parser.parse(options, args); - assertEquals(thing, commandLine.getParsedOptionValue("Nope", thing)); - checkHandler(false, handler, opt); + final Properties props = cl.getOptionProperties(optionD); + assertNotNull(props, "null properties"); + assertEquals(4, props.size(), "number of properties in " + props); + assertEquals("value1", props.getProperty("param1"), "property 1"); + assertEquals("value2", props.getProperty("param2"), "property 2"); + assertEquals("true", props.getProperty("param3"), "property 3"); + assertEquals("value4", props.getProperty("param4"), "property 4"); - assertEquals(thing, commandLine.getParsedOptionValue("Nope", thinger)); - checkHandler(false, handler, opt); + assertEquals("bar", cl.getOptionProperties(optionProperty).getProperty("foo"), "property with long format"); } - private static Stream<Arguments> createParsedOptionValueParameters() throws ParseException { - final List<Arguments> lst = new ArrayList<>(); - final Option optT = Option.builder().option("T").longOpt("tee").deprecated().type(Integer.class).optionalArg(true).build(); - final Option optU = Option.builder("U").longOpt("you").type(Integer.class).optionalArg(true).build(); - final OptionGroup optionGroup = new OptionGroup().addOption(optT).addOption(optU); - final Integer expected = Integer.valueOf(1); + @Test + public void testGetOptionsBuilder() { + final CommandLine cmd = CommandLine.builder().build(); + assertNotNull(cmd.getOptions()); + assertEquals(0, cmd.getOptions().length); - // 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)); + cmd.addOption(null); + cmd.addOption(new Option("a", null)); + cmd.addOption(new Option("b", null)); + cmd.addOption(new Option("c", null)); - 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)); + assertEquals(3, cmd.getOptions().length); + } + @Test + public void testGetOptionsCtor() { + final CommandLine cmd = new CommandLine(); + assertNotNull(cmd.getOptions()); + assertEquals(0, cmd.getOptions().length); - // 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)); + cmd.addOption(new Option("a", null)); + cmd.addOption(new Option("b", null)); + cmd.addOption(new Option("c", null)); + cmd.addOption(null); - 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)); + assertEquals(3, cmd.getOptions().length); + } - 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)); + assertNull(cmd.getOptionValue((OptionGroup) null)); + assertNull(cmd.getParsedOptionValue((OptionGroup) null)); } }
