assert "1".equals(pr.matchedOption('n').stringValues().get(0)); assert "2".equals(pr.matchedOption('n').stringValues().get(1)); assert "3".equals(pr.matchedOption('n').stringValues().get(2));
/** see <a href="https://github.com/remkop/picocli/issues/279">issue #279</a> */ @Test public void testSingleValueFieldWithOptionalParameter_279() { @Command(name="sample") class Sample { @Option(names="--foo", arity="0..1") String foo; } List<CommandLine> parsed1 = new CommandLine(new Sample()).parse();// not specified OptionSpec option1 = parsed1.get(0).getCommandSpec().optionsMap().get("--foo"); assertNull("optional option is null when option not specified", option1.getValue()); assertTrue("optional option has no string value when option not specified", option1.stringValues().isEmpty()); assertTrue("optional option has no typed value when option not specified", option1.typedValues().isEmpty()); List<CommandLine> parsed2 = new CommandLine(new Sample()).parse("--foo");// specified without value OptionSpec option2 = parsed2.get(0).getCommandSpec().optionsMap().get("--foo"); assertEquals("optional option is empty string when specified without args", "", option2.getValue()); assertEquals("optional option string value when specified without args", "", option2.stringValues().get(0)); assertEquals("optional option typed value when specified without args", "", option2.typedValues().get(0)); List<CommandLine> parsed3 = new CommandLine(new Sample()).parse("--foo", "value");// specified with value OptionSpec option3 = parsed3.get(0).getCommandSpec().optionsMap().get("--foo"); assertEquals("optional option is empty string when specified without args", "value", option3.getValue()); assertEquals("optional option string value when specified without args", "value", option3.stringValues().get(0)); assertEquals("optional option typed value when specified without args", "value", option3.typedValues().get(0)); }
/** see <a href="https://github.com/remkop/picocli/issues/279">issue #279</a> */ @Test public void testSingleValueFieldWithOptionalParameterFollowedByOption_279() { @Command(name="sample") class Sample { @Option(names = "-x") boolean x; @Option(names="--foo", arity="0..1") String foo; } Sample sample = new Sample(); List<CommandLine> parsed3 = new CommandLine(sample).parse("--foo", "-x");// specified without value OptionSpec option3 = parsed3.get(0).getCommandSpec().optionsMap().get("--foo"); assertEquals("optional option is empty string when specified without args", "", option3.getValue()); assertEquals("optional option string value when specified without args", "", option3.stringValues().get(0)); assertEquals("optional option typed value when specified without args", "", option3.typedValues().get(0)); assertEquals("", sample.foo); assertEquals(true, sample.x); }
option -> Arrays.stream(option.names()).anyMatch(dependentOptionsNames::contains) && !option.stringValues().isEmpty()) .map(option -> option.names()[0]) .collect(
assertEquals("xval", parseResult.matchedOption("-x").stringValues().get(0)); assertEquals("xval", parseResult.matchedOptionValue("-x", "xval")); assertFalse(parseResult.hasMatchedPositional(0));
@Test public void testRawOptionValueReturnsFirstValue() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-V", "--verbose").build()); spec.addOption(OptionSpec.builder("-f", "--file") .paramLabel("FILES") .type(List.class) .auxiliaryTypes(File.class) // List<File> .description("The files to process").build()); CommandLine commandLine = new CommandLine(spec); String[] args = { "--verbose", "-f", "file1", "--file=file2" }; ParseResult pr = commandLine.parseArgs(args); assertEquals(Arrays.asList(args), pr.originalArgs()); assertEquals("file1", pr.matchedOption('f').stringValues().get(0)); assertEquals("file1", pr.matchedOption("-f").stringValues().get(0)); assertEquals("file1", pr.matchedOption("--file").stringValues().get(0)); List<String> expected = Arrays.asList("file1", "file2"); assertEquals(expected, pr.matchedOption('f').stringValues()); assertEquals(expected, pr.matchedOption("file").stringValues()); // for examples in Programmatic API wiki page assert expected.equals(pr.matchedOption('f').stringValues()); assert expected.equals(pr.matchedOption("file").stringValues()); assertSame(pr.commandSpec().findOption("-V"), pr.tentativeMatch.get(0)); assertSame(pr.commandSpec().findOption("-f"), pr.tentativeMatch.get(1)); assertSame(pr.originalArgs().get(2), pr.tentativeMatch.get(2)); assertSame(pr.commandSpec().findOption("-f"), pr.tentativeMatch.get(3)); assertEquals(4, pr.tentativeMatch.size()); }
/** see <a href="https://github.com/remkop/picocli/issues/279">issue #279</a> */ @Test public void testSingleValueFieldWithOptionalParameter_279() { @Command(name="sample") class Sample { @Option(names="--foo", arity="0..1") String foo; } List<CommandLine> parsed1 = new CommandLine(new Sample()).parse();// not specified OptionSpec option1 = parsed1.get(0).getCommandSpec().optionsMap().get("--foo"); assertNull("optional option is null when option not specified", option1.getValue()); assertTrue("optional option has no string value when option not specified", option1.stringValues().isEmpty()); assertTrue("optional option has no typed value when option not specified", option1.typedValues().isEmpty()); List<CommandLine> parsed2 = new CommandLine(new Sample()).parse("--foo");// specified without value OptionSpec option2 = parsed2.get(0).getCommandSpec().optionsMap().get("--foo"); assertEquals("optional option is empty string when specified without args", "", option2.getValue()); assertEquals("optional option string value when specified without args", "", option2.stringValues().get(0)); assertEquals("optional option typed value when specified without args", "", option2.typedValues().get(0)); List<CommandLine> parsed3 = new CommandLine(new Sample()).parse("--foo", "value");// specified with value OptionSpec option3 = parsed3.get(0).getCommandSpec().optionsMap().get("--foo"); assertEquals("optional option is empty string when specified without args", "value", option3.getValue()); assertEquals("optional option string value when specified without args", "value", option3.stringValues().get(0)); assertEquals("optional option typed value when specified without args", "value", option3.typedValues().get(0)); }
@Test public void testParseResetsRawAndOriginalStringValues() { CommandSpec spec = CommandSpec.create() .addOption(OptionSpec.builder("-x").type(String.class).build()) .addPositional(PositionalParamSpec.builder().build()); CommandLine cmd = new CommandLine(spec); ParseResult parseResult = cmd.parseArgs("-x", "XVAL", "POSITIONAL"); assertEquals("XVAL", parseResult.matchedOption('x').getValue()); assertEquals(Arrays.asList("XVAL"), parseResult.matchedOption('x').stringValues()); assertEquals(Arrays.asList("XVAL"), parseResult.matchedOption('x').originalStringValues()); assertEquals("POSITIONAL", parseResult.matchedPositional(0).getValue()); assertEquals(Arrays.asList("POSITIONAL"), parseResult.matchedPositional(0).stringValues()); assertEquals(Arrays.asList("POSITIONAL"), parseResult.matchedPositional(0).originalStringValues()); ParseResult parseResult2 = cmd.parseArgs("-x", "222", "$$$$"); assertEquals("222", parseResult2.matchedOption('x').getValue()); assertEquals(Arrays.asList("222"), parseResult2.matchedOption('x').stringValues()); assertEquals(Arrays.asList("222"), parseResult2.matchedOption('x').originalStringValues()); assertEquals("$$$$", parseResult2.matchedPositional(0).getValue()); assertEquals(Arrays.asList("$$$$"), parseResult2.matchedPositional(0).stringValues()); assertEquals(Arrays.asList("$$$$"), parseResult2.matchedPositional(0).originalStringValues()); }
@Test public void testTypedValues() { class App { @Option(names="-x") int x; } ParseResult result1 = new CommandLine(new App()).parseArgs();// not specified assertFalse(result1.hasMatchedOption('x')); assertTrue(result1.commandSpec().findOption('x').typedValues().isEmpty()); ParseResult result2 = new CommandLine(new App()).parseArgs("-x", "123"); assertTrue(result2.hasMatchedOption('x')); assertEquals(Integer.valueOf(123), result2.matchedOptionValue('x', 0)); ParseResult result3 = new CommandLine(new App()) .setOverwrittenOptionsAllowed(true) .parseArgs("-x", "1", "-x", "2", "-x", "3"); assertTrue(result3.hasMatchedOption('x')); assertEquals(Integer.valueOf(3), result3.matchedOptionValue('x', 0)); assertEquals(Arrays.asList("1", "2", "3"), result3.matchedOption('x').stringValues()); assertEquals(Arrays.asList(1, 2, 3), result3.matchedOption('x').typedValues()); }
/** see <a href="https://github.com/remkop/picocli/issues/279">issue #279</a> */ @Test public void testSingleValueFieldWithOptionalParameterFollowedByOption_279() { @Command(name="sample") class Sample { @Option(names = "-x") boolean x; @Option(names="--foo", arity="0..1") String foo; } Sample sample = new Sample(); List<CommandLine> parsed3 = new CommandLine(sample).parse("--foo", "-x");// specified without value OptionSpec option3 = parsed3.get(0).getCommandSpec().optionsMap().get("--foo"); assertEquals("optional option is empty string when specified without args", "", option3.getValue()); assertEquals("optional option string value when specified without args", "", option3.stringValues().get(0)); assertEquals("optional option typed value when specified without args", "", option3.typedValues().get(0)); assertEquals("", sample.foo); assertEquals(true, sample.x); }
@Test public void testRawOptionValue() { class App { @Option(names = {"-x", "++XX", "/XXX"}) String[] x; @Option(names = "-y") String y; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("-x", "value1", "-x", "value2"); assertEquals("value1", parseResult.matchedOption("x").stringValues().get(0)); assertEquals("value1", parseResult.matchedOption("-x").stringValues().get(0)); assertEquals("value1", parseResult.matchedOption("XX").stringValues().get(0)); assertEquals("value1", parseResult.matchedOption("++XX").stringValues().get(0)); assertEquals("value1", parseResult.matchedOption("XXX").stringValues().get(0)); assertEquals("value1", parseResult.matchedOption("/XXX").stringValues().get(0)); assertEquals(null, parseResult.matchedOption("y")); assertEquals(null, parseResult.matchedOption("-y")); }
@Test public void testRawOptionValueForBooleanOptions_ReturnsStringTrue() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-V", "--verbose").build()); CommandLine commandLine = new CommandLine(spec); ParseResult pr = commandLine.parseArgs("--verbose"); assertTrue(pr.hasMatchedOption("--verbose")); // as specified on command line assertTrue(pr.hasMatchedOption('V')); // single-character alias works too assertTrue(pr.hasMatchedOption("verbose")); // command name without hyphens assertTrue(pr.matchedOptionValue("verbose", Boolean.FALSE)); assertEquals("true", pr.matchedOption("verbose").stringValues().get(0)); }
@Test public void testOptionWithNonJavaIdentifierName() { class App { @Option(names = "-") String dash; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("-", "val"); assertEquals("val", parseResult.matchedOption('-').stringValues().get(0)); assertEquals("val", parseResult.matchedOption("-").stringValues().get(0)); assertEquals("val", parseResult.matchedOptionValue('-', "val")); assertEquals("val", parseResult.matchedOptionValue("-", "val")); assertNull("empty string should not match", parseResult.matchedOption("")); assertNull("empty string should not match", parseResult.matchedOptionValue("", null)); }
assertEquals("xval", parseResult.matchedOption("-x").stringValues().get(0)); assertEquals("xval", parseResult.matchedOptionValue("-x", "xval")); assertFalse(parseResult.hasMatchedPositional(0));
@Test public void testRawOptionValueReturnsFirstValue() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-V", "--verbose").build()); spec.addOption(OptionSpec.builder("-f", "--file") .paramLabel("FILES") .type(List.class) .auxiliaryTypes(File.class) // List<File> .description("The files to process").build()); CommandLine commandLine = new CommandLine(spec); String[] args = { "--verbose", "-f", "file1", "--file=file2" }; ParseResult pr = commandLine.parseArgs(args); assertEquals(Arrays.asList(args), pr.originalArgs()); assertEquals("file1", pr.matchedOption('f').stringValues().get(0)); assertEquals("file1", pr.matchedOption("-f").stringValues().get(0)); assertEquals("file1", pr.matchedOption("--file").stringValues().get(0)); List<String> expected = Arrays.asList("file1", "file2"); assertEquals(expected, pr.matchedOption('f').stringValues()); assertEquals(expected, pr.matchedOption("file").stringValues()); // for examples in Programmatic API wiki page assert expected.equals(pr.matchedOption('f').stringValues()); assert expected.equals(pr.matchedOption("file").stringValues()); assertSame(pr.commandSpec().findOption("-V"), pr.tentativeMatch.get(0)); assertSame(pr.commandSpec().findOption("-f"), pr.tentativeMatch.get(1)); assertSame(pr.originalArgs().get(2), pr.tentativeMatch.get(2)); assertSame(pr.commandSpec().findOption("-f"), pr.tentativeMatch.get(3)); assertEquals(4, pr.tentativeMatch.size()); }
@Test public void testParseResetsRawAndOriginalStringValues() { CommandSpec spec = CommandSpec.create() .addOption(OptionSpec.builder("-x").type(String.class).build()) .addPositional(PositionalParamSpec.builder().build()); CommandLine cmd = new CommandLine(spec); ParseResult parseResult = cmd.parseArgs("-x", "XVAL", "POSITIONAL"); assertEquals("XVAL", parseResult.matchedOption('x').getValue()); assertEquals(Arrays.asList("XVAL"), parseResult.matchedOption('x').stringValues()); assertEquals(Arrays.asList("XVAL"), parseResult.matchedOption('x').originalStringValues()); assertEquals("POSITIONAL", parseResult.matchedPositional(0).getValue()); assertEquals(Arrays.asList("POSITIONAL"), parseResult.matchedPositional(0).stringValues()); assertEquals(Arrays.asList("POSITIONAL"), parseResult.matchedPositional(0).originalStringValues()); ParseResult parseResult2 = cmd.parseArgs("-x", "222", "$$$$"); assertEquals("222", parseResult2.matchedOption('x').getValue()); assertEquals(Arrays.asList("222"), parseResult2.matchedOption('x').stringValues()); assertEquals(Arrays.asList("222"), parseResult2.matchedOption('x').originalStringValues()); assertEquals("$$$$", parseResult2.matchedPositional(0).getValue()); assertEquals(Arrays.asList("$$$$"), parseResult2.matchedPositional(0).stringValues()); assertEquals(Arrays.asList("$$$$"), parseResult2.matchedPositional(0).originalStringValues()); }
@Test public void testTypedValues() { class App { @Option(names="-x") int x; } ParseResult result1 = new CommandLine(new App()).parseArgs();// not specified assertFalse(result1.hasMatchedOption('x')); assertTrue(result1.commandSpec().findOption('x').typedValues().isEmpty()); ParseResult result2 = new CommandLine(new App()).parseArgs("-x", "123"); assertTrue(result2.hasMatchedOption('x')); assertEquals(Integer.valueOf(123), result2.matchedOptionValue('x', 0)); ParseResult result3 = new CommandLine(new App()) .setOverwrittenOptionsAllowed(true) .parseArgs("-x", "1", "-x", "2", "-x", "3"); assertTrue(result3.hasMatchedOption('x')); assertEquals(Integer.valueOf(3), result3.matchedOptionValue('x', 0)); assertEquals(Arrays.asList("1", "2", "3"), result3.matchedOption('x').stringValues()); assertEquals(Arrays.asList(1, 2, 3), result3.matchedOption('x').typedValues()); }
@Test public void testRawOptionValue() { class App { @Option(names = {"-x", "++XX", "/XXX"}) String[] x; @Option(names = "-y") String y; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("-x", "value1", "-x", "value2"); assertEquals("value1", parseResult.matchedOption("x").stringValues().get(0)); assertEquals("value1", parseResult.matchedOption("-x").stringValues().get(0)); assertEquals("value1", parseResult.matchedOption("XX").stringValues().get(0)); assertEquals("value1", parseResult.matchedOption("++XX").stringValues().get(0)); assertEquals("value1", parseResult.matchedOption("XXX").stringValues().get(0)); assertEquals("value1", parseResult.matchedOption("/XXX").stringValues().get(0)); assertEquals(null, parseResult.matchedOption("y")); assertEquals(null, parseResult.matchedOption("-y")); }
@Test public void testOptionWithNonJavaIdentifierName() { class App { @Option(names = "-") String dash; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("-", "val"); assertEquals("val", parseResult.matchedOption('-').stringValues().get(0)); assertEquals("val", parseResult.matchedOption("-").stringValues().get(0)); assertEquals("val", parseResult.matchedOptionValue('-', "val")); assertEquals("val", parseResult.matchedOptionValue("-", "val")); assertNull("empty string should not match", parseResult.matchedOption("")); assertNull("empty string should not match", parseResult.matchedOptionValue("", null)); }
@Test public void testRawOptionValueForBooleanOptions_ReturnsStringTrue() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-V", "--verbose").build()); CommandLine commandLine = new CommandLine(spec); ParseResult pr = commandLine.parseArgs("--verbose"); assertTrue(pr.hasMatchedOption("--verbose")); // as specified on command line assertTrue(pr.hasMatchedOption('V')); // single-character alias works too assertTrue(pr.hasMatchedOption("verbose")); // command name without hyphens assertTrue(pr.matchedOptionValue("verbose", Boolean.FALSE)); assertEquals("true", pr.matchedOption("verbose").stringValues().get(0)); }