@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()); }
/** Similar to {@link #usage(PrintStream, Help.ColorScheme)}, but with the specified {@code PrintWriter} instead of a {@code PrintStream}. * @since 3.0 */ public void usage(PrintWriter writer, Help.ColorScheme colorScheme) { writer.print(usage(new StringBuilder(), getHelpFactory().create(getCommandSpec(), colorScheme))); } /** Similar to {@link #usage(PrintStream)}, but returns the usage help message as a String instead of printing it to the {@code PrintStream}.
/** * Main method. * * @param args Arguments. */ public static void main(String... args) { CommandLine.run(new RootCommand(), System.out, args); } }
/** @deprecated use {@link #parseWithHandler(IParseResultHandler2, String[])} instead * @since 2.0 */ @Deprecated public List<Object> parseWithHandler(IParseResultHandler handler, PrintStream out, String... args) { return parseWithHandlers(handler, out, Help.Ansi.AUTO, defaultExceptionHandler(), args); } /**
/** * Equivalent to {@code new CommandLine(command).usage(out, colorScheme)}. * See {@link #usage(PrintStream, Help.ColorScheme)} for details. * @param command the object annotated with {@link Command}, {@link Option} and {@link Parameters} * @param out the print stream to print the help message to * @param colorScheme the {@code ColorScheme} defining the styles for options, parameters and commands when ANSI is enabled * @throws IllegalArgumentException if the specified command object does not have a {@link Command}, {@link Option} or {@link Parameters} annotation */ public static void usage(Object command, PrintStream out, Help.ColorScheme colorScheme) { toCommandLine(command, new DefaultFactory()).usage(out, colorScheme); }
@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 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 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 testInject_AnnotatedFieldInjected() { class Injected { @Spec CommandSpec commandSpec; @Parameters String[] params; } Injected injected = new Injected(); assertNull(injected.commandSpec); CommandLine cmd = new CommandLine(injected); assertSame(cmd.getCommandSpec(), injected.commandSpec); }
@Test public void testDeclarativelyAddSubcommandsSucceedsWithDefaultConstructorForDefaultFactory() { @Command(subcommands = {SubSub1_testDeclarativelyAddSubcommands.class}) class MainCommand {} CommandLine cmdLine = new CommandLine(new MainCommand()); assertEquals(SubSub1_testDeclarativelyAddSubcommands.class.getName(), cmdLine.getSubcommands().get("subsub1").getCommand().getClass().getName()); } @Test
@Test public void testDefaultProviderPropagatedToSubCommand() { CommandLine cmd = new CommandLine(App.class); cmd.addSubcommand("sub", new CommandLine(Sub.class)); CommandLine subCommandLine = cmd.getSubcommands().get("sub"); cmd.setDefaultValueProvider(new TestDefaultProvider()); assertNotNull(subCommandLine.getCommandSpec().defaultValueProvider()); assertEquals(TestDefaultProvider.class, subCommandLine.getCommandSpec().defaultValueProvider().getClass()); }
@Test public void testBooleanOptionsArity0_nFailsIfAttachedParamNotABoolean() { // ignores varargs CommandLine cmd = new CommandLine(new CommandLineArityTest.BooleanOptionsArity0_nAndParameters()); cmd.getCommandSpec().parser().collectErrors(true); cmd.parse("-bool=123 -other".split(" ")); assertEquals(1, cmd.getParseResult().errors().size()); assertEquals("Invalid value for option '-bool': '123' is not a boolean", cmd.getParseResult().errors().get(0).getMessage()); } @Test
@Test public void testIssue444SubcommandWithDuplicateAliases() { Issue443TopLevelCommand top = new Issue443TopLevelCommand(); SubCommandWithAlias sub = new SubCommandWithAlias(); CommandLine cmd = new CommandLine(top).addSubcommand("task", sub, "t", "t"); CommandSpec subSpec = cmd.getSubcommands().get("task").getCommandSpec(); String expected = String.format("" + "Usage: cb [COMMAND]%n" + "Commands:%n" + " task, t subcommand with alias%n"); assertEquals(expected, cmd.getUsageMessage()); assertArrayEquals(new String[]{"t"}, subSpec.aliases()); }
public static void main(String[] args) { CommandLine cmd = new CommandLine(new ParentCommand()); cmd.parseWithHandler(new CommandLine.RunAll(), args); if (args.length == 0) { cmd.usage(System.out); } } }
public static void main(String[] args) { CommandLine cmd = new CommandLine(new ShowAll()); cmd.getHelpSectionMap().put(SECTION_KEY_COMMAND_LIST, new MyCommandListRenderer()); cmd.usage(System.out); } }
@Test public void testCommandSpecQualifiedName_topLevelCommand() { CommandLine cmd = new CommandLine(new I18nCommand()); assertEquals("i18n-top", cmd.getCommandSpec().qualifiedName(".")); Map<String, CommandLine> subcommands = cmd.getSubcommands(); assertEquals("i18n-top.help", subcommands.get("help").getCommandSpec().qualifiedName(".")); CommandLine sub = subcommands.get("i18n-sub"); assertEquals("i18n-top.i18n-sub", sub.getCommandSpec().qualifiedName(".")); assertEquals("i18n-top.i18n-sub.help", sub.getSubcommands().get("help").getCommandSpec().qualifiedName(".")); }
@Test public void testCommandListReturnsRegisteredCommands() { @Command class MainCommand {} @Command class Command1 {} @Command class Command2 {} CommandLine commandLine = new CommandLine(new MainCommand()); commandLine.addSubcommand("cmd1", new Command1()).addSubcommand("cmd2", new Command2()); Map<String, CommandLine> commandMap = commandLine.getSubcommands(); assertEquals(2, commandMap.size()); assertTrue("cmd1", commandMap.get("cmd1").getCommand() instanceof Command1); assertTrue("cmd2", commandMap.get("cmd2").getCommand() instanceof Command2); }
@Test public void testCommandAliasAnnotationSubcommandUsageHelp() { CommandLine commandLine = new CommandLine(new TopLevelCommand()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); commandLine.getSubcommands().get("sub").usage(new PrintStream(baos), CommandLine.Help.defaultColorScheme(Help.Ansi.ON).commands(Help.Ansi.Style.underline)); // add underline String expected = Help.Ansi.ON.new Text(String.format("" + "Usage: @|bold,underline top sub|@ [COMMAND]%n" + "I'm subcommand No. 1!%n" + "Commands:%n" + " @|bold,underline subsub|@, @|bold,underline ss|@, @|bold,underline sbsb|@ I'm like a 3rd rate command but great bang for your buck%n")).toString(); assertEquals(expected, baos.toString()); } }
/** {@inheritDoc} */ @Override public void run() { CommandLine.usage(this, System.out); } }
@Test public void testAnnotateMethod_mixinParameter() { Method m = CommandLine.getCommandMethods(UnannotatedClassWithMixinParameters.class, "withMixin").get(0); CommandLine cmd = new CommandLine(m); CommandSpec spec = cmd.getCommandSpec(); assertEquals(1, spec.mixins().size()); spec = spec.mixins().get("arg0"); assertEquals(SomeMixin.class, spec.userObject().getClass()); }