@Test public void testQuotedMapKeysAndQuotedMapValuesNeedExtraQuotes() { class App { @Option(names = "-e") Map<String, String> map = new HashMap<String, String>(); } App app = new App(); new CommandLine(app).setTrimQuotes(true).parseArgs("-e", "\"\"a=b=c\"=\"x y z\"\""); assertTrue(app.map.keySet().toString(), app.map.containsKey("a=b=c")); assertEquals("x y z", app.map.get("a=b=c")); } @Ignore("Needs support for nested quoting #595")
@Test public void testUpdateHelpRequested() { class App { @Option(names = "-x", help = true) boolean x; } System.setProperty("picocli.trace", "INFO"); new CommandLine(new App()).parseArgs("-x"); assertTrue(systemErrRule.getLog().contains("App.x has 'help' annotation: not validating required fields")); }
@Test public void testParseQuotedArgumentWithNestedQuotes2() { class Example { @Option(names = "-x", split = ",") String[] parts; } String[] args = {"-x", "\"-Dvalues=a,b,c\",\"-Dother=1,2\""}; Example example = new Example(); new CommandLine(example).parseArgs(args); assertArrayEquals(new String[]{"\"-Dvalues=a,b,c\"", "\"-Dother=1,2\""}, example.parts); }
@Test public void testAssertNoMissingParametersOption() { class App { @Option(names = "-x") int x; } CommandLine cmd = new CommandLine(new App()); cmd.getCommandSpec().parser().collectErrors(true); ParseResult parseResult = cmd.parseArgs("-x"); List<Exception> errors = parseResult.errors(); assertEquals(1, errors.size()); assertEquals("Missing required parameter for option '-x' (<x>)", errors.get(0).getMessage()); }
@Test public void testAssertNoMissingParametersPositional() { class App { @Parameters(arity = "1") int x; } CommandLine cmd = new CommandLine(new App()); cmd.getCommandSpec().parser().collectErrors(true); ParseResult parseResult = cmd.parseArgs(); List<Exception> errors = parseResult.errors(); assertEquals(1, errors.size()); assertEquals("Missing required parameter: <x>", errors.get(0).getMessage()); }
@Test public void testClearListOptionOldValueBeforeParse() { CommandSpec cmd = CommandSpec.create(); cmd.addOption(OptionSpec.builder("-x").type(List.class).initialValue(Arrays.asList("ABC")).build()); CommandLine cl = new CommandLine(cmd); cl.parseArgs("-x", "1", "-x", "2", "-x", "3"); assertEquals(Arrays.asList("1", "2", "3"), cmd.findOption("x").getValue()); cl.parseArgs("-x", "4", "-x", "5"); assertEquals(Arrays.asList("4", "5"), cmd.findOption("x").getValue()); cl.parseArgs(); assertEquals(Arrays.asList("ABC"), cmd.findOption("x").getValue()); }
@Test public void testPositionalParameterQuotesTrimmedIfRequested() { class TextParams { @Parameters() String[] text; } TextParams opt = new TextParams(); new CommandLine(opt).setTrimQuotes(true).parseArgs("\"a text\""); assertEquals("a text", opt.text[0]); }
@Test public void testPositionalMultiParameterQuotesTrimmedIfRequested() { class TextParams { @Parameters() String[] text; } TextParams opt = new TextParams(); new CommandLine(opt).setTrimQuotes(true).parseArgs("\"a text\"", "\"another text\"", "\"x z\""); assertArrayEquals(new String[]{"a text", "another text", "x z"}, opt.text); }
@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 testPositionalParamSpec_DefaultValue_map_replacedByCommandLineValue() { CommandSpec cmd = CommandSpec.create().add(PositionalParamSpec .builder().defaultValue("1=A,2=B,3=C").splitRegex(",").type(Map.class).auxiliaryTypes(Integer.class, String.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs("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.matchedPositionalValue(0, Collections.emptyMap())); }
@Test public void testCommandSpec_IsCommandLineCommandSpec() { class App { @Parameters String[] positional; } CommandLine cmd = new CommandLine(new App()); ParseResult result = cmd.parseArgs("a", "b"); assertSame(cmd.getCommandSpec(), result.commandSpec()); } @Test
@Test public void testClearScalarOptionOldValueBeforeParse() { CommandSpec cmd = CommandSpec.create(); cmd.addOption(OptionSpec.builder("-x").type(String.class).initialValue(null).build()); CommandLine cl = new CommandLine(cmd); cl.parseArgs("-x", "1"); assertEquals("1", cmd.findOption("x").getValue()); cl.parseArgs("-x", "2"); assertEquals("2", cmd.findOption("x").getValue()); cl.parseArgs(); assertNull(cmd.findOption("x").getValue()); }
@Test public void testDontClearArrayOptionOldValueBeforeParse() { CommandSpec cmd = CommandSpec.create(); cmd.addOption(OptionSpec.builder("-x").arity("2..3").initialValue(new String[] {"ABC"}).hasInitialValue(false).build()); CommandLine cl = new CommandLine(cmd); cl.parseArgs("-x", "1", "2", "3"); assertArrayEquals(new String[] {"1", "2", "3"}, (String[]) cmd.findOption("x").getValue()); cl.parseArgs("-x", "4", "5"); assertArrayEquals(new String[] {"4", "5"}, (String[]) cmd.findOption("x").getValue()); cl.parseArgs(); assertArrayEquals(new String[] {"4", "5"}, (String[]) cmd.findOption("x").getValue()); }
@Test public void testPositionalParamSpec_DefaultValue_single_replacedByCommandLineValue() { CommandSpec cmd = CommandSpec.create().add(PositionalParamSpec.builder().defaultValue("123").type(int.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs("456"); assertEquals(Integer.valueOf(456), parseResult.matchedPositionalValue(0, -1)); }
@Test public void testPositionalParamSpec_setsDefaultValue_ifNotMatched() { CommandSpec cmd = CommandSpec.create().add(PositionalParamSpec.builder().defaultValue("123").type(int.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs(); assertFalse(parseResult.hasMatchedPositional(0)); // TODO this method should be renamed to matchedPositionalValue assertEquals(Integer.valueOf(-1), parseResult.matchedPositionalValue(0, -1)); // TODO positionalValue should return the value of the option, matched or not //assertEquals(Integer.valueOf(123), parseResult.positionalValue(0)); assertEquals(Integer.valueOf(123), parseResult.commandSpec().positionalParameters().get(0).getValue()); }
@Test public void testInitializingDefaultsShouldNotAddPositionalToParseResult() { CommandSpec spec = CommandSpec.create() .addPositional(PositionalParamSpec.builder().defaultValue("xyz").build()); CommandLine cmd = new CommandLine(spec); ParseResult parseResult = cmd.parseArgs(); assertFalse(parseResult.hasMatchedPositional(0)); }
@Test public void testBooleanObjectUsageHelpOptions() { CommandSpec cmd = CommandSpec.create().add(OptionSpec.builder("-z").type(Boolean.class).usageHelp(true).build()); assertTrue(new CommandLine(cmd).parseArgs("-z").isUsageHelpRequested()); }
@Test public void testInitializingDefaultsShouldNotAddOptionToParseResult() { CommandSpec spec = CommandSpec.create() .addOption(OptionSpec.builder("-x").type(String.class).defaultValue("xyz").build()); CommandLine cmd = new CommandLine(spec); ParseResult parseResult = cmd.parseArgs(); assertFalse(parseResult.hasMatchedOption('x')); }
@Test public void testBooleanObjectVersionHelpOptions() { CommandSpec cmd = CommandSpec.create().add(OptionSpec.builder("-x").type(Boolean.class).versionHelp(true).build()); assertTrue(new CommandLine(cmd).parseArgs("-x").isVersionHelpRequested()); }
@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)); }