@Test public void testSortedSetPositionalParametersAreReusedIfNonNull() { SortedSetPositionalParams params = new SortedSetPositionalParams(); params.sortedSet = new TreeSet<Integer>(); SortedSet<Integer> list = params.sortedSet; new CommandLine(params).parse("3", "2", "1"); assertSame(list, params.sortedSet); assertEquals(Arrays.asList(1, 2, 3), new ArrayList<Integer>(params.sortedSet)); } @Test
@Test public void testSetPositionalParametersAreReusedIfNonNull() { SetPositionalParams params = new SetPositionalParams(); params.set = new TreeSet<Integer>(); Set<Integer> list = params.set; new CommandLine(params).parse("3", "2", "1"); assertSame(list, params.set); assertEquals(new HashSet<Integer>(Arrays.asList(1, 2, 3)), params.set); } @Test
@Test public void testQueuePositionalParametersAreReusedIfNonNull() { QueuePositionalParams params = new QueuePositionalParams(); params.queue = new LinkedList<Integer>(); Queue<Integer> list = params.queue; new CommandLine(params).parse("3", "2", "1"); assertSame(list, params.queue); assertEquals(new LinkedList<Integer>(Arrays.asList(3, 2, 1)), params.queue); } @Test
@Test public void testPrintHelpIfRequestedReturnsFalseForNoHelp() throws IOException { class App { @Option(names = "-v") boolean verbose; } List<CommandLine> list = new CommandLine(new App()).parse("-v"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); final PrintStream out = new PrintStream(baos); assertFalse(CommandLine.printHelpIfRequested(list, out, out, Help.Ansi.OFF)); String expected = ""; assertEquals(expected, baos.toString()); }
@Test public void testListOptionParametersAreReusedIfNonNull() { EnumParams params = new EnumParams(); List<TimeUnit> list = new ArrayList<TimeUnit>(); params.timeUnitList = list; new CommandLine(params).parse("-timeUnitList", "SECONDS", "MICROSECONDS", "SECONDS"); assertEquals(Arrays.asList(SECONDS, MICROSECONDS, SECONDS), params.timeUnitList); assertSame(list, params.timeUnitList); } @Test
@Test public void testCollectionPositionalParametersAreReusedIfNonNull() { CollectionPositionalParams params = new CollectionPositionalParams(); params.collection = new ArrayList<Integer>(); Collection<Integer> list = params.collection; new CommandLine(params).parse("3", "2", "1"); assertSame(list, params.collection); assertEquals(Arrays.asList(3, 2, 1), params.collection); } @Test
@Test public void testSortedSetPositionalParametersAreInstantiatedIfNull() { SortedSetPositionalParams params = new SortedSetPositionalParams(); assertNull(params.sortedSet); new CommandLine(params).parse("3", "2", "1"); assertNotNull(params.sortedSet); assertEquals(Arrays.asList(1, 2, 3), new ArrayList<Integer>(params.sortedSet)); } @Test
@Test public void testShortOptionsWithColonSeparatorButNoValueAssignsEmptyStringEvenIfNotLast() { CompactFields compact = new CompactFields(); CommandLine cmd = new CommandLine(compact); cmd.setSeparator(":"); cmd.parse("-ro: -v".split(" ")); verifyCompact(compact, false, true, "-v", null); } @Test
@Test public void testBuiltInHelpCommandMakesRequiredOptionsOptional() { @Command(subcommands = HelpCommand.class) class Parent { @Option(names = "-m", required = true) String mandatory; } CommandLine commandLine = new CommandLine(new Parent(), new InnerClassFactory(this)); commandLine.parse("help"); }
@Test public void testParentInjectedOnParseWhenConfiguredAsSubcommand() { List<CommandLine> result = new CommandLine(new Top()).parse("-d/tmp/blah", "sub", "3"); Top top = result.get(0).getCommand(); assertEquals(new File("/tmp/blah"), top.baseDirectory); Sub sub = result.get(1).getCommand(); assertEquals(3, sub.count); assertSame(top, sub.parent); } @Test
@Test public void testCommandLine_isVersionHelpRequested_falseWhenNotSpecified() { List<CommandLine> parsedCommands = new CommandLine(new RequiredField()).parse("--help"); assertFalse("version info requested", parsedCommands.get(0).isVersionHelpRequested()); } @Test
@Test public void test244SubcommandsNotParsed() { List<CommandLine> list = new CommandLine(new Top()).parse("-h", "sub"); assertEquals(2, list.size()); assertTrue(list.get(0).getCommand() instanceof Top); assertTrue(list.get(1).getCommand() instanceof Sub); assertTrue(((Top) list.get(0).getCommand()).isUsageHelpRequested); }
@Test public void testCompactFieldsWithUnmatchedArguments() { setTraceLevel("OFF"); CommandLine cmd = new CommandLine(new CompactFields()).setUnmatchedArgumentsAllowed(true); cmd.parse("-oout -r -vp1 p2".split(" ")); assertEquals(Arrays.asList("-p1"), cmd.getUnmatchedArguments()); }
@Test public void testAnnotatedCustomConverterFactory() { class App { @Parameters(converter = CustomConverter.class) int converted; } App app = new App(); CommandLine commandLine = new CommandLine(app, new Plus23ConverterFactory()); commandLine.parse("100"); assertEquals(123, app.converted); } static class EnumParams {
@Test public void testParentNotInjectedWhenConfiguredAsTopLevelCommand() { List<CommandLine> result = new CommandLine(new Sub()).parse("3"); Sub sub = result.get(0).getCommand(); assertNull(sub.parent); assertEquals(3, sub.count); assertEquals(0, sub.result); } @Test
@Test public void testParserCaseInsensitiveEnumValuesAllowed_invalidInput() throws Exception { PojoWithEnumOptions fields = new PojoWithEnumOptions(); CommandLine cmd = new CommandLine(fields).setCaseInsensitiveEnumValuesAllowed(true); try { cmd.parse("-u=millisecondINVALID"); fail("Expected exception"); } catch (ParameterException ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Invalid value for option '-u': expected one of ")); } }
@Test public void testPositionalParamsUnknownArgumentMultiValueWithUnmatchedArgsAllowed() throws Exception { class SingleValue { @Parameters(index = "0..2") String[] str; } setTraceLevel("OFF"); CommandLine cmd = new CommandLine(new SingleValue()).setUnmatchedArgumentsAllowed(true); cmd.parse("val0", "val1", "val2", "val3"); assertArrayEquals(new String[]{"val0", "val1", "val2"}, ((SingleValue)cmd.getCommand()).str); assertEquals(Arrays.asList("val3"), cmd.getUnmatchedArguments()); }
@Test public void testMultiValueOptionArityAloneIsInsufficient() throws Exception { CommandLine.Model.CommandSpec spec = CommandLine.Model.CommandSpec.create(); CommandLine.Model.OptionSpec option = CommandLine.Model.OptionSpec.builder("-c", "--count").arity("3").type(int.class).build(); assertFalse(option.isMultiValue()); spec.addOption(option); spec.parser().collectErrors(true); CommandLine commandLine = new CommandLine(spec); commandLine.parse("-c", "1", "2", "3"); assertEquals(1, commandLine.getParseResult().errors().size()); assertEquals("Unmatched arguments: 2, 3", commandLine.getParseResult().errors().get(0).getMessage()); }
@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