@Command static class PositionalsMixedWithOptions { @Command public void mixed(int a, @Option(names = "-b") int b, @Option(names = "-c") String c, int[] x, String[] y) {} }
static class UnAnnotatedClassWithAnnotatedField { @Option(names = "-y") int y; @Command public void cmd1(@Option(names = "-x") int x, File f) { } @Command public void cmd2(@Option(names = "-x") int x, File f) { } }
@Command static class AnnotatedClassWithoutAnnotatedFields { @Command public void cmd1(@Option(names = "-x") int x, File f) { } @Command public void cmd2(@Option(names = "-x") int x, File f) { } }
@Command static class AnnotatedParams { @Command public void method(@Parameters int a, @Parameters int b, @Parameters int c, int x, int y, int z) {} }
/** Adds the specified subcommand with the specified name. * If the specified subcommand does not have a ResourceBundle set, it is initialized to the ResourceBundle of this command spec. * @param name subcommand name - when this String is encountered in the command line arguments the subcommand is invoked * @param subcommand describes the subcommand to envoke when the name is encountered on the command line * @return this {@code CommandSpec} object for method chaining */ public CommandSpec addSubcommand(String name, CommandSpec subcommand) { return addSubcommand(name, new CommandLine(subcommand)); }
@Test public void testCommandLine_printVersionInfo_printsArrayOfPlainTextStrings() { @CommandLine.Command(version = {"Versioned Command 1.0", "512-bit superdeluxe", "(c) 2017"}) class Versioned {} ByteArrayOutputStream baos = new ByteArrayOutputStream(); new CommandLine(new Versioned()).printVersionHelp(new PrintStream(baos, true), Ansi.OFF); String result = baos.toString(); assertEquals(String.format("Versioned Command 1.0%n512-bit superdeluxe%n(c) 2017%n"), result); }
@Test public void testCommandLine_printVersionInfo_printsSingleStringWithMarkup() { @CommandLine.Command(version = "@|red 1.0|@") class Versioned {} ByteArrayOutputStream baos = new ByteArrayOutputStream(); new CommandLine(new Versioned()).printVersionHelp(new PrintStream(baos, true), Ansi.ON); String result = baos.toString(); assertEquals(String.format("\u001B[31m1.0\u001B[39m\u001B[0m%n"), result); }
@Test public void testCommandLine_printVersionInfo_printsSinglePlainTextString() { @Command(version = "1.0") class Versioned {} ByteArrayOutputStream baos = new ByteArrayOutputStream(); new CommandLine(new Versioned()).printVersionHelp(new PrintStream(baos, true), Help.Ansi.OFF); String result = baos.toString(); assertEquals(String.format("1.0%n"), result); }
@Test public void testAddMixinMustBeValidCommand_ParametersAnnotatedField() { class ValidMixin { // valid command because it has @Parameters annotation @Parameters int a; } @Command class Receiver {} CommandLine commandLine = new CommandLine(new Receiver()); commandLine.addMixin("valid", new ValidMixin()); // no exception }
@Test public void testMixinAnnotationCommandAttributes() throws Exception { class Receiver { @Mixin CommandAttributes.MixMeIn mixMeIn; } CommandLine commandLine = new CommandLine(new Receiver()); verifyMixinCommandAttributes(commandLine); }
@Test public void testBooleanArgFilter() { @Command(name = "booltest") class App { @Option(names = "-b") boolean primitive; @Option(names = "-B") Boolean object; } String actual = AutoComplete.bash("booltest", new CommandLine(new App())); assertThat(actual, CoreMatchers.containsString("FLAG_OPTS=\"-b -B\"")); }
@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 testMixinAnnotationMustBeValidCommand_OptionAnnotatedField() { class ValidMixin { // valid command because it has @Option annotation @Option(names = "-a") int a; } class Receiver { // valid command because it has @Mixin annotated field @Mixin ValidMixin mixMeIn; } new CommandLine(new Receiver(), new InnerClassFactory(this)); // no exception }
@Test public void testIssue439InjectedSpecInMixinHasNullCommandLineAnnotations() { CommandLine cmd = new CommandLine(new Issue439Command()); assertExceptionThrownFromSetter(cmd); }
@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 testCompletionScript() throws IOException { String actual = AutoComplete.bash("rcmd", new CommandLine(new HyphenatedCommand())); String expected = format(AutoCompleteTest.loadTextFromClasspath("/hyphenated_completion.bash"), CommandLine.VERSION); assertEquals(expected, actual); } }
@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 testAddMixin_CanBeRetrievedByFieldName() { @Command class MixMeIn {} @Command class Receiver {} CommandLine commandLine = new CommandLine(new Receiver(), new InnerClassFactory(this)); commandLine.addMixin("mixin", new MixMeIn()); assertFalse("mixin was registered", commandLine.getMixins().isEmpty()); assertTrue(commandLine.getMixins().get("mixin") instanceof MixMeIn); }
@Test public void testCommandSpecAddSubcommand_SubcommandInheritsResourceBundle() { ResourceBundle rb = ResourceBundle.getBundle("picocli.SharedMessages"); CommandSpec spec = CommandSpec.wrapWithoutInspection(null); spec.resourceBundle(rb); assertSame(rb, spec.resourceBundle()); CommandSpec sub = CommandSpec.wrapWithoutInspection(null); spec.addSubcommand("a", new CommandLine(sub)); assertSame(rb, sub.resourceBundle()); }
@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