List<File> expected = Arrays.asList(new File("file1"), new File("file2")); assert expected.equals(pr.matchedOptionValue('f', defaultValue)); assert expected.equals(pr.matchedOptionValue("--file", defaultValue)); assert Arrays.equals(new int[]{1,2,3}, pr.matchedOptionValue('n', new int[0]));
@Test public void testOptionSpec_DefaultValue_map_replacedByCommandLineValue() { CommandSpec cmd = CommandSpec.create().add(OptionSpec .builder("-x").defaultValue("1=A,2=B,3=C").splitRegex(",").type(Map.class).auxiliaryTypes(Integer.class, String.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs("-x", "4=X,5=Y,6=Z"); Map<Integer, String> expected = new HashMap<Integer, String>(); expected.put(4, "X"); expected.put(5, "Y"); expected.put(6, "Z"); assertEquals(expected, parseResult.matchedOptionValue('x', Collections.emptyMap())); }
@Test public void testOptionValueReturnsAllValuesConvertedToType() { 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); List<File> expected = Arrays.asList(new File("file1"), new File("file2")); assertEquals(expected, pr.matchedOptionValue('f', Collections.<File>emptyList())); assertEquals(expected, pr.matchedOptionValue("--file", Collections.<File>emptyList())); // for examples in Programmatic API wiki page assert expected.equals(pr.matchedOptionValue('f', Collections.<File>emptyList())); assert expected.equals(pr.matchedOptionValue("--file", Collections.<File>emptyList())); }
assertEquals("xval", parseResult.matchedOptionValue("-x", "xval")); assertFalse(parseResult.hasMatchedPositional(0));
if (CommandLine.printHelpIfRequested(pr)) { return; } int count = pr.matchedOptionValue('c', 1); List<File> files = pr.matchedPositionalValue(0, Collections.<File>emptyList()); for (File f : files) {
@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 testParserUnmatchedOptionsArePositionalParams_True_unmatchedOptionIsPositionalParam() { class App { @Option(names = "-a") String alpha; @Parameters String[] remainder; } App app = new App(); CommandLine cmd = new CommandLine(app); cmd.setUnmatchedOptionsArePositionalParams(true); ParseResult parseResult = cmd.parseArgs("-x", "-a", "AAA"); assertTrue(parseResult.hasMatchedPositional(0)); assertArrayEquals(new String[]{"-x"}, parseResult.matchedPositionalValue(0, new String[0])); assertTrue(parseResult.hasMatchedOption("a")); assertEquals("AAA", parseResult.matchedOptionValue("a", null)); assertArrayEquals(new String[]{"-x"}, app.remainder); assertEquals("AAA", app.alpha); }
@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 testOptionSpec_defaultValue_overwritesInitialValue() { class Params { @Option(names = "-x") int num = 12345; } CommandLine cmd = new CommandLine(new Params()); OptionSpec x = cmd.getCommandSpec().posixOptionsMap().get('x').toBuilder().defaultValue("54321").build(); cmd = new CommandLine(CommandSpec.create().addOption(x)); ParseResult parseResult = cmd.parseArgs(); assertFalse(parseResult.hasMatchedOption('x')); // TODO this method should be renamed to matchedOptionValue assertEquals(Integer.valueOf(-1), parseResult.matchedOptionValue('x', -1)); // TODO optionValue should return the value of the option, matched or not //assertEquals(Integer.valueOf(54321), parseResult.optionValue('x')); assertEquals(Integer.valueOf(54321), parseResult.commandSpec().findOption('x').getValue()); }
/** 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}. */
/** 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)); }
@Test public void testOptionSpec_DefaultValue_list_replacedByCommandLineValue() { CommandSpec cmd = CommandSpec.create().addOption(OptionSpec .builder("-x").defaultValue("1,2,3").splitRegex(",").type(List.class).auxiliaryTypes(Integer.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs("-x", "4,5,6"); assertEquals(Arrays.asList(4, 5, 6), parseResult.matchedOptionValue('x', Collections.emptyList())); }
@Test public void testOptionSpec_DefaultValue_array_replacedByCommandLineValue() { CommandSpec cmd = CommandSpec.create().addOption(OptionSpec .builder("-x").defaultValue("1,2,3").splitRegex(",").type(int[].class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs("-x", "4,5,6"); assertArrayEquals(new int[]{4, 5, 6}, parseResult.matchedOptionValue('x', new int[0])); }
@Test public void testOptionSpec_DefaultValue_single_replacedByCommandLineValue() { CommandSpec cmd = CommandSpec.create().addOption(OptionSpec.builder("-x").defaultValue("123").type(int.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs("-x", "456"); assertEquals(Integer.valueOf(456), parseResult.matchedOptionValue('x', -1)); }
@Test(expected = ClassCastException.class) public void testOptionValueWrongType() { class App { @Option(names = "-x") int[] x; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("-x", "123", "-x", "456"); long[] wrongType = {123L, 456L}; assertArrayEquals(wrongType, parseResult.matchedOptionValue("x", wrongType)); }
@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 testOptionValue_ByShortName() { class App { @Option(names = {"-x", "++XX", "/XXX"}) int[] x; @Option(names = "-y") double y; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("-x", "123", "-x", "456", "-y", "3.14"); int[] expected = {123, 456}; assertArrayEquals(expected,parseResult.matchedOptionValue('x', expected)); assertEquals(Double.valueOf(3.14), parseResult.matchedOptionValue('y', 3.14)); assertNull(parseResult.matchedOptionValue('%', null)); // non-existing option }
@Test public void testOptionSpec_setsDefaultValue_ifNotMatched() { CommandSpec cmd = CommandSpec.create().addOption(OptionSpec.builder("-x").defaultValue("123").type(int.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs(); assertFalse(parseResult.hasMatchedOption('x')); // TODO this method should be renamed to matchedOptionValue assertEquals(Integer.valueOf(-1), parseResult.matchedOptionValue('x', -1)); // TODO optionValue should return the value of the option, matched or not //assertEquals(Integer.valueOf(123), parseResult.optionValue('x')); assertEquals(Integer.valueOf(123), parseResult.commandSpec().findOption('x').getValue()); }
@Test public void testOptionValue_NullIfNotMatched() { class App { @Option(names = "-y") String y = "initial"; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs(); assertNull(parseResult.matchedOptionValue("y", null)); assertNull(parseResult.matchedOptionValue("-y", null)); }