@Test public void testCompleteAllowsNormalValues() { List<CharSequence> candidates = new ArrayList<CharSequence>(); AutoComplete.complete(CommandSpec.create().addOption(OptionSpec.builder("-x").build()), new String[] {"-x"}, 0, 0, 0, candidates); assertFalse(candidates.isEmpty()); }
@Test public void testUnmatchedExceptionIsUnknownOption() { CommandLine cmd = new CommandLine(CommandSpec.create()); assertFalse("unmatch list is null", new UnmatchedArgumentException(cmd, "").isUnknownOption()); assertFalse("unmatch list is empty", new UnmatchedArgumentException(cmd, new ArrayList<String>()).isUnknownOption()); List<String> likeAnOption = Arrays.asList("-x"); assertTrue("first unmatched resembles option", new UnmatchedArgumentException(cmd, likeAnOption).isUnknownOption()); List<String> unlikeOption = Arrays.asList("xxx"); assertFalse("first unmatched doesn't resembles option", new UnmatchedArgumentException(cmd, unlikeOption).isUnknownOption()); }
@Test public void testVersionHelp_basic() { CommandSpec spec = CommandSpec.create().version("1.0", "copyright etc"); CommandLine commandLine = new CommandLine(spec); String actual = versionString(commandLine, Ansi.OFF); String expected = String.format("" + "1.0%n" + "copyright etc%n"); assertEquals(expected, actual); }
@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 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 testMultipleUsageHelpOptions() { setTraceLevel("WARN"); CommandSpec cmd = CommandSpec.create() .add(OptionSpec.builder("-x").type(boolean.class).usageHelp(true).build()) .add(OptionSpec.builder("-h").type(boolean.class).usageHelp(true).build()); assertEquals("", systemErrRule.getLog()); systemErrRule.clearLog(); new CommandLine(cmd); assertEquals("", systemOutRule.getLog()); assertEquals(String.format("[picocli WARN] Multiple options [-x, -h] are marked as 'usageHelp=true'. Usually a command has only one --help option that triggers display of the usage help message. Alternatively, consider using @Command(mixinStandardHelpOptions = true) on your command instead.%n"), systemErrRule.getLog()); }
@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 testMultiValuePositionalParamWithListAndAuxTypes() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(List.class).auxiliaryTypes(Integer.class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1", "2", "3"); assertEquals(Arrays.asList(1, 2, 3), spec.positionalParameters().get(0).getValue()); }
@Test public void testModelParse() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-h", "--help").usageHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-V", "--version").versionHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT").arity("1").type(int.class).description("number of times to execute").build()); CommandLine commandLine = new CommandLine(spec); commandLine.parse("-c", "33"); assertEquals(Integer.valueOf(33), spec.optionsMap().get("-c").getValue()); } // TODO parse method should return an object offering only the options/positionals that were matched
@Test public void testAbbreviatedSynopsis() { CommandSpec spec = CommandSpec.create(); spec.addPositional(PositionalParamSpec.builder().paramLabel("a").hidden(true).build()); spec.addPositional(PositionalParamSpec.builder().paramLabel("b").build()); Help help = new Help(spec, new Help.ColorScheme(Help.Ansi.OFF)); String actual = help.abbreviatedSynopsis(); assertEquals(String.format("<main class> b...%n"), actual); }
@Test public void testHelpCreateDetailedSynopsisOptionsText() { Help help = new Help(CommandSpec.create().addOption(OptionSpec.builder("xx").build()), new ColorScheme(Help.Ansi.OFF)); Text text = help.createDetailedSynopsisOptionsText(null, true); assertEquals(" [xx]", text.toString()); }
@Test public void testMessagesGetStringNullKey() { String def = "abc"; assertSame(def, new Messages(CommandSpec.create(), (ResourceBundle) null).getString(null, def)); assertSame(def, new Messages(CommandSpec.create(), (ResourceBundle) null).getString("help", def)); ResourceBundle rb = ResourceBundle.getBundle("picocli.SharedMessages"); assertSame(def, new Messages(CommandSpec.create(), rb).getString(null, def)); assertNotEquals(def, new Messages(CommandSpec.create(), rb).getString("help", def)); }
@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 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 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 testMultipleVersionHelpOptions() { setTraceLevel("WARN"); CommandSpec cmd = CommandSpec.create() .add(OptionSpec.builder("-x").type(boolean.class).versionHelp(true).build()) .add(OptionSpec.builder("-V").type(boolean.class).versionHelp(true).build()); assertEquals("", systemErrRule.getLog()); systemErrRule.clearLog(); new CommandLine(cmd); assertEquals("", systemOutRule.getLog()); assertEquals(String.format("[picocli WARN] Multiple options [-x, -V] are marked as 'versionHelp=true'. Usually a command has only one --version option that triggers display of the version information. Alternatively, consider using @Command(mixinStandardHelpOptions = true) on your command instead.%n"), systemErrRule.getLog()); }
@Test public void testBashify() { CommandSpec cmd = CommandSpec.create().addOption( OptionSpec.builder("-x") .type(String.class) .paramLabel("_A\tB C") .completionCandidates(Arrays.asList("1")).build()); String actual = AutoComplete.bash("bashify", new CommandLine(cmd)); String expected = format(loadTextFromClasspath("/bashify_completion.bash"), CommandLine.VERSION); assertEquals(expected, actual); }
@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)); }
@SuppressWarnings("deprecation") @Test public void testSynopsis() { Help help = new Help(CommandSpec.create(), new Help.ColorScheme(Help.Ansi.OFF)); String actual = help.synopsis(); assertEquals(String.format("<main class>%n"), actual); }