List<String> originalArgs = pr.originalArgs(); // lists all command line args assert Arrays.asList(args).equals(originalArgs);
@Test public void testHasMatchedPositionalByPositionalSpec() { class App { @Option(names = "-x") String x; @Parameters(index = "0", arity = "0..1") int index0 = -1; @Parameters(index = "1", arity = "0..1") int index1 = -1; @Parameters(index = "2", arity = "0..1") int index2 = -1; } CommandLine cmd = new CommandLine(new App()); ParseResult result = cmd.parseArgs("-x", "xval", "0", "1"); assertSame(result.commandSpec().findOption("-x"), result.tentativeMatch.get(0)); assertSame(result.originalArgs().get(1), result.tentativeMatch.get(1)); assertSame(result.commandSpec().positionalParameters().get(0), result.tentativeMatch.get(2)); assertSame(result.commandSpec().positionalParameters().get(1), result.tentativeMatch.get(3)); List<PositionalParamSpec> all = cmd.getCommandSpec().positionalParameters(); assertTrue(result.hasMatchedPositional(all.get(0))); assertTrue(result.hasMatchedPositional(all.get(1))); assertFalse(result.hasMatchedPositional(all.get(2))); }
@Test public void testOptionValue() { class App { @Option(names = {"-x", "++XX", "/XXX"}) int[] x; @Option(names = "-y") double y; } CommandLine cmd = new CommandLine(new App()); ParseResult result = cmd.parseArgs("-x", "123", "-x", "456", "-y", "3.14"); int[] expected = {123, 456}; assertArrayEquals(expected, result.matchedOptionValue("x", expected)); assertArrayEquals(expected, result.matchedOptionValue("-x", expected)); assertArrayEquals(expected, result.matchedOptionValue("XX", expected)); assertArrayEquals(expected, result.matchedOptionValue("++XX", expected)); assertArrayEquals(expected, result.matchedOptionValue("XXX", expected)); assertArrayEquals(expected, result.matchedOptionValue("/XXX", expected)); assertEquals(Double.valueOf(3.14), result.matchedOptionValue("y", 3.14)); assertEquals(Double.valueOf(3.14), result.matchedOptionValue("-y", 3.14)); assertSame(result.commandSpec().findOption("-x"), result.tentativeMatch.get(0)); assertSame(result.originalArgs().get(1), result.tentativeMatch.get(1)); assertSame(result.commandSpec().findOption("-x"), result.tentativeMatch.get(2)); assertSame(result.originalArgs().get(3), result.tentativeMatch.get(3)); assertSame(result.commandSpec().findOption("-y"), result.tentativeMatch.get(4)); assertSame(result.originalArgs().get(5), result.tentativeMatch.get(5)); }
cmd.addSubcommand("sub", new Sub()); ParseResult parseResult = cmd.parseArgs("-x", "xval", "sub", "1", "2", "3"); assertEquals(Arrays.asList("-x", "xval", "sub", "1", "2", "3"), parseResult.originalArgs()); assertSame(parseResult.originalArgs().get(1), parseResult.tentativeMatch.get(1)); assertSame(parseResult.subcommand().commandSpec(), parseResult.tentativeMatch.get(2)); assertSame(parseResult.subcommand().commandSpec().positionalParameters().get(0), parseResult.tentativeMatch.get(3)); assertEquals(Arrays.asList("-x", "xval", "sub", "1", "2", "3"), subResult.originalArgs()); // TODO should subresult.originalArgs include the args consumed by the parent?
assertEquals(Arrays.asList(args), result.originalArgs());
@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()); }
@Override protected List<Object> handle(final ParseResult parseResult) throws ExecutionException { final CommandLine commandLine = parseResult.asCommandLineList().get(0); if (parseResult.hasMatchedOption(configFileOptionName)) { final OptionSpec configFileOption = parseResult.matchedOption(configFileOptionName); final File configFile; try { configFile = configFileOption.getter().get(); } catch (final Exception e) { throw new ExecutionException(commandLine, e.getMessage(), e); } final TomlConfigFileDefaultProvider tomlConfigFileDefaultProvider = new TomlConfigFileDefaultProvider(commandLine, configFile); commandLine.setDefaultValueProvider(tomlConfigFileDefaultProvider); } else if (isDocker) { final File configFile = new File(DOCKER_CONFIG_LOCATION); if (configFile.exists()) { final TomlConfigFileDefaultProvider tomlConfigFileDefaultProvider = new TomlConfigFileDefaultProvider(commandLine, configFile); commandLine.setDefaultValueProvider(tomlConfigFileDefaultProvider); } } commandLine.parseWithHandlers( resultHandler, exceptionHandler, parseResult.originalArgs().toArray(new String[0])); return new ArrayList<>(); }
@Test public void testBasicUsage() { class App { @Option(names = {"-t", "-ttt"}) boolean boolVal; @Option(names = {"-i", "-int"}) int intVal; @Parameters String[] positional; } ParseResult result = new CommandLine(new App()).parseArgs("-t", "-i", "1", "a", "b"); assertEquals(Arrays.asList("-t", "-i", "1", "a", "b"), result.originalArgs()); assertSame(result.commandSpec().findOption("-t"), result.tentativeMatch.get(0)); assertSame(result.commandSpec().findOption("-i"), result.tentativeMatch.get(1)); assertSame(result.originalArgs().get(2), result.tentativeMatch.get(2)); assertSame(result.commandSpec().positionalParameters().get(0), result.tentativeMatch.get(3)); assertSame(result.commandSpec().positionalParameters().get(0), result.tentativeMatch.get(4)); assertTrue(result.unmatched().isEmpty()); assertFalse(result.hasSubcommand()); assertFalse(result.isUsageHelpRequested()); assertFalse(result.isVersionHelpRequested()); assertTrue(result.hasMatchedOption("-ttt")); assertTrue(result.hasMatchedOption("-t")); assertTrue(result.hasMatchedOption("-i")); assertTrue(result.hasMatchedOption("-int")); assertFalse(result.hasMatchedOption("-unknown")); assertTrue(result.hasMatchedPositional(0)); assertTrue(result.hasMatchedPositional(1)); } @Test
@Test public void testOptionValue() { class App { @Option(names = {"-x", "++XX", "/XXX"}) int[] x; @Option(names = "-y") double y; } CommandLine cmd = new CommandLine(new App()); ParseResult result = cmd.parseArgs("-x", "123", "-x", "456", "-y", "3.14"); int[] expected = {123, 456}; assertArrayEquals(expected, result.matchedOptionValue("x", expected)); assertArrayEquals(expected, result.matchedOptionValue("-x", expected)); assertArrayEquals(expected, result.matchedOptionValue("XX", expected)); assertArrayEquals(expected, result.matchedOptionValue("++XX", expected)); assertArrayEquals(expected, result.matchedOptionValue("XXX", expected)); assertArrayEquals(expected, result.matchedOptionValue("/XXX", expected)); assertEquals(Double.valueOf(3.14), result.matchedOptionValue("y", 3.14)); assertEquals(Double.valueOf(3.14), result.matchedOptionValue("-y", 3.14)); assertSame(result.commandSpec().findOption("-x"), result.tentativeMatch.get(0)); assertSame(result.originalArgs().get(1), result.tentativeMatch.get(1)); assertSame(result.commandSpec().findOption("-x"), result.tentativeMatch.get(2)); assertSame(result.originalArgs().get(3), result.tentativeMatch.get(3)); assertSame(result.commandSpec().findOption("-y"), result.tentativeMatch.get(4)); assertSame(result.originalArgs().get(5), result.tentativeMatch.get(5)); }
@Test public void testHasMatchedOptionByName_VariousPrefixes() { class App { @Option(names = {"-x", "++XX", "/XXX"}) String[] x; @Option(names = "-y") String y; } CommandLine cmd = new CommandLine(new App()); ParseResult result = cmd.parseArgs("-x", "value1", "-x", "value2"); assertTrue(result.hasMatchedOption("x")); assertTrue(result.hasMatchedOption("-x")); assertTrue(result.hasMatchedOption("XX")); assertTrue(result.hasMatchedOption("++XX")); assertTrue(result.hasMatchedOption("XXX")); assertTrue(result.hasMatchedOption("/XXX")); assertFalse(result.hasMatchedOption("y")); assertFalse(result.hasMatchedOption("-y")); assertSame(result.commandSpec().findOption("-x"), result.tentativeMatch.get(0)); assertSame(result.originalArgs().get(1), result.tentativeMatch.get(1)); assertSame(result.commandSpec().findOption("-x"), result.tentativeMatch.get(2)); assertSame(result.originalArgs().get(3), result.tentativeMatch.get(3)); }
@Test public void testHasMatchedPositionalByPositionalSpec() { class App { @Option(names = "-x") String x; @Parameters(index = "0", arity = "0..1") int index0 = -1; @Parameters(index = "1", arity = "0..1") int index1 = -1; @Parameters(index = "2", arity = "0..1") int index2 = -1; } CommandLine cmd = new CommandLine(new App()); ParseResult result = cmd.parseArgs("-x", "xval", "0", "1"); assertSame(result.commandSpec().findOption("-x"), result.tentativeMatch.get(0)); assertSame(result.originalArgs().get(1), result.tentativeMatch.get(1)); assertSame(result.commandSpec().positionalParameters().get(0), result.tentativeMatch.get(2)); assertSame(result.commandSpec().positionalParameters().get(1), result.tentativeMatch.get(3)); List<PositionalParamSpec> all = cmd.getCommandSpec().positionalParameters(); assertTrue(result.hasMatchedPositional(all.get(0))); assertTrue(result.hasMatchedPositional(all.get(1))); assertFalse(result.hasMatchedPositional(all.get(2))); }
@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); }
cmd.addSubcommand("sub", new Sub()); ParseResult parseResult = cmd.parseArgs("-x", "xval", "sub", "1", "2", "3"); assertEquals(Arrays.asList("-x", "xval", "sub", "1", "2", "3"), parseResult.originalArgs()); assertSame(parseResult.originalArgs().get(1), parseResult.tentativeMatch.get(1)); assertSame(parseResult.subcommand().commandSpec(), parseResult.tentativeMatch.get(2)); assertSame(parseResult.subcommand().commandSpec().positionalParameters().get(0), parseResult.tentativeMatch.get(3)); assertEquals(Arrays.asList("-x", "xval", "sub", "1", "2", "3"), subResult.originalArgs()); // TODO should subresult.originalArgs include the args consumed by the parent?
assertEquals(Arrays.asList(args), result.originalArgs());
@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 testBasicUsage() { class App { @Option(names = {"-t", "-ttt"}) boolean boolVal; @Option(names = {"-i", "-int"}) int intVal; @Parameters String[] positional; } ParseResult result = new CommandLine(new App()).parseArgs("-t", "-i", "1", "a", "b"); assertEquals(Arrays.asList("-t", "-i", "1", "a", "b"), result.originalArgs()); assertSame(result.commandSpec().findOption("-t"), result.tentativeMatch.get(0)); assertSame(result.commandSpec().findOption("-i"), result.tentativeMatch.get(1)); assertSame(result.originalArgs().get(2), result.tentativeMatch.get(2)); assertSame(result.commandSpec().positionalParameters().get(0), result.tentativeMatch.get(3)); assertSame(result.commandSpec().positionalParameters().get(0), result.tentativeMatch.get(4)); assertTrue(result.unmatched().isEmpty()); assertFalse(result.hasSubcommand()); assertFalse(result.isUsageHelpRequested()); assertFalse(result.isVersionHelpRequested()); assertTrue(result.hasMatchedOption("-ttt")); assertTrue(result.hasMatchedOption("-t")); assertTrue(result.hasMatchedOption("-i")); assertTrue(result.hasMatchedOption("-int")); assertFalse(result.hasMatchedOption("-unknown")); assertTrue(result.hasMatchedPositional(0)); assertTrue(result.hasMatchedPositional(1)); } @Test
@Test public void testHasMatchedOptionByName_VariousPrefixes() { class App { @Option(names = {"-x", "++XX", "/XXX"}) String[] x; @Option(names = "-y") String y; } CommandLine cmd = new CommandLine(new App()); ParseResult result = cmd.parseArgs("-x", "value1", "-x", "value2"); assertTrue(result.hasMatchedOption("x")); assertTrue(result.hasMatchedOption("-x")); assertTrue(result.hasMatchedOption("XX")); assertTrue(result.hasMatchedOption("++XX")); assertTrue(result.hasMatchedOption("XXX")); assertTrue(result.hasMatchedOption("/XXX")); assertFalse(result.hasMatchedOption("y")); assertFalse(result.hasMatchedOption("-y")); assertSame(result.commandSpec().findOption("-x"), result.tentativeMatch.get(0)); assertSame(result.originalArgs().get(1), result.tentativeMatch.get(1)); assertSame(result.commandSpec().findOption("-x"), result.tentativeMatch.get(2)); assertSame(result.originalArgs().get(3), result.tentativeMatch.get(3)); }