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)); assert "1,2,3".equals(pr.matchedOption("--num").originalStringValues().get(0));
@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 testIsUsageHelpRequested() { @Command(mixinStandardHelpOptions = true) class App { @Option(names = "-x") String x; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("-h"); assertTrue(parseResult.isUsageHelpRequested()); assertFalse(parseResult.isVersionHelpRequested()); assertSame(cmd.getCommandSpec().optionsMap().get("-h"), parseResult.matchedOption('h')); assertTrue(parseResult.unmatched().isEmpty()); assertTrue(parseResult.matchedPositionals().isEmpty()); }
@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)); }
/** Returns whether an option whose aliases include the specified name was matched on the command line. * @param name used to search the matched options. May be an alias of the option name that was actually specified on the command line. * The specified name may include option name prefix characters or not. */ public boolean hasMatchedOption(String name) { return matchedOption(name) != null; } /** Returns whether the specified option was matched on the command line. */
@Before public void initMocks() { final List<CommandLine> commandLines = new ArrayList<>(); commandLines.add(mockCommandLine); when(mockParseResult.asCommandLineList()).thenReturn(commandLines); final List<String> originalArgs = new ArrayList<>(); originalArgs.add(CONFIG_FILE_OPTION_NAME); when(mockParseResult.originalArgs()).thenReturn(originalArgs); when(mockParseResult.matchedOption(CONFIG_FILE_OPTION_NAME)).thenReturn(mockConfigOptionSpec); when(mockParseResult.hasMatchedOption(CONFIG_FILE_OPTION_NAME)).thenReturn(true); when(mockConfigOptionSpec.getter()).thenReturn(mockConfigOptionGetter); }
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 testMatchedOption_returnsOnlyMatchedOptions() { 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"); OptionSpec x = cmd.getCommandSpec().posixOptionsMap().get('x'); assertSame(x, parseResult.matchedOption('x')); assertSame(x, parseResult.matchedOption("x")); assertSame(x, parseResult.matchedOption("-x")); assertSame(x, parseResult.matchedOption("XX")); assertSame(x, parseResult.matchedOption("++XX")); assertSame(x, parseResult.matchedOption("XXX")); assertSame(x, parseResult.matchedOption("/XXX")); }
/** Returns the command line argument value of the option with the specified name, converted to the {@linkplain OptionSpec#type() type} of the option, or the specified default value if no option with the specified name was matched. */ public <T> T matchedOptionValue(String name, T defaultValue) { return matchedOptionValue(matchedOption(name), defaultValue); } /** Returns the command line argument value of the specified option, converted to the {@linkplain OptionSpec#type() type} of the option, or the specified default value if the specified option is {@code null}. */
@Test public void testMatchedOption_ReturnsOnlyMatchedOptions() { class App { @Option(names = "-a", arity = "0..1") String a; @Option(names = "-b", arity = "0..1") String b; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("-a"); assertNotNull(parseResult.matchedOption('a')); assertNotNull(parseResult.matchedOption("a")); assertNotNull(parseResult.matchedOption("-a")); assertNull(parseResult.matchedOption('b')); assertNull(parseResult.matchedOption("b")); assertNull(parseResult.matchedOption("-b")); } @Test
/** Returns the command line argument value of the option with the specified name, converted to the {@linkplain OptionSpec#type() type} of the option, or the specified default value if no option with the specified name was matched. */ public <T> T matchedOptionValue(char shortName, T defaultValue) { return matchedOptionValue(matchedOption(shortName), defaultValue); } /** Returns the command line argument value of the option with the specified name, converted to the {@linkplain OptionSpec#type() type} of the option, or the specified default value if no option with the specified name was matched. */
@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)); }
/** Returns whether an option whose aliases include the specified short name was matched on the command line. * @param shortName used to search the matched options. May be an alias of the option name that was actually specified on the command line. */ public boolean hasMatchedOption(char shortName) { return matchedOption(shortName) != null; } /** Returns whether an option whose aliases include the specified name was matched on the command line.
@Test public void testIsVersionHelpRequested() { @Command(mixinStandardHelpOptions = true) class App { @Option(names = "-x") String x; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("--version"); assertFalse(parseResult.isUsageHelpRequested()); assertTrue(parseResult.isVersionHelpRequested()); assertSame(cmd.getCommandSpec().optionsMap().get("--version"), parseResult.matchedOption('V')); }
/** Returns whether an option whose aliases include the specified name was matched on the command line. * @param name used to search the matched options. May be an alias of the option name that was actually specified on the command line. * The specified name may include option name prefix characters or not. */ public boolean hasMatchedOption(String name) { return matchedOption(name) != null; } /** Returns whether the specified option was matched on the command line. */
@Test public void testMatchedOption_returnsNullForNonMatchedOption() { 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"); assertNull(parseResult.matchedOption('y')); assertNull(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)); }