@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()); }
@Deprecated @Test public void testOptionDefaultHelpIsFalse() { assertFalse(OptionSpec.builder("-x").build().help()); } @Test
@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 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 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 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 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 testSortByShortestOptionNameAlphabetically_handlesNulls() throws Exception { Help.SortByShortestOptionNameAlphabetically sort = new Help.SortByShortestOptionNameAlphabetically(); OptionSpec a = OptionSpec.builder("-a").build(); OptionSpec b = OptionSpec.builder("-b").build(); assertEquals(1, sort.compare(null, a)); assertEquals(-1, sort.compare(a, null)); assertEquals(-1, sort.compare(a, b)); assertEquals(0, sort.compare(a, a)); assertEquals(1, sort.compare(b, a)); }
@Test public void testOptionSpecRequiresNonNullName() { try { OptionSpec.builder(null, "-s").build(); fail("Expected exception"); } catch (NullPointerException ex) { assertEquals("name", ex.getMessage()); } }
@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)); }
@Test() public void testOptionSpecRequiresNonNullNameArray() { try { OptionSpec.builder(null).build(); fail("Expected exception"); } catch (InitializationException ex) { assertEquals("OptionSpec names cannot be null. Specify at least one option name.", ex.getMessage()); } }
@Test public void testOptionDefaultVersionHelpIsFalse() { assertFalse(OptionSpec.builder("-x").build().versionHelp()); } @Deprecated