public static void main(String[] args) { final MetaManagementService service = new MetaManagementService(); final CommandLine cmd = new CommandLine(new Tools()); cmd.addSubcommand("AddBroker", new AddBrokerCommand(service)); cmd.addSubcommand("AddDb", new AddDbCommand(service)); cmd.addSubcommand("ReplaceBroker", new ReplaceBrokerCommand(service)); cmd.addSubcommand("ListBrokers", new ListBrokersCommand(service)); cmd.addSubcommand("ListBrokerGroups", new ListBrokerGroupsCommand(service)); cmd.addSubcommand("ListSubjectRoutes", new ListSubjectRoutesCommand(service)); cmd.addSubcommand("AddSubjectBrokerGroup", new AddSubjectBrokerGroupCommand(service)); cmd.addSubcommand("RemoveSubjectBrokerGroup", new RemoveSubjectBrokerGroupCommand(service)); cmd.addSubcommand("AddNewSubject", new AddNewSubjectCommand(service)); cmd.addSubcommand("ExtendSubjectRoute", new ExtendSubjectRouteCommand(service)); cmd.addSubcommand("MarkReadonly", new MarkBrokerReadonlyCommand(service)); cmd.addSubcommand("UnMarkReadonly", new UnMarkBrokerReadonlyCommand(service)); cmd.parseWithHandler(new CommandLine.RunLast(), args); }
public CommandLine create() {return new CommandLine(new App()).addSubcommand("sub", new Sub());} };
public CommandLine create() {return new CommandLine(new App()).addSubcommand("sub", new Sub());} };
static CommandLine mainCommand() { CommandLine commandLine = new CommandLine(new Git()); commandLine.addSubcommand("help", new CommandLine.HelpCommand()); commandLine.addSubcommand("status", new GitStatus()); commandLine.addSubcommand("commit", new GitCommit()); commandLine.addSubcommand("add", new GitAdd()); commandLine.addSubcommand("branch", new GitBranch()); commandLine.addSubcommand("checkout", new GitCheckout()); commandLine.addSubcommand("clone", new GitClone()); commandLine.addSubcommand("diff", new GitDiff()); commandLine.addSubcommand("merge", new GitMerge()); commandLine.addSubcommand("push", new GitPush()); commandLine.addSubcommand("rebase", new GitRebase()); commandLine.addSubcommand("tag", new GitTag()); return commandLine; }
@Test public void testUsage_NoHeaderIfAllSubcommandHidden() { @Command(name = "foo", description = "This is a foo sub-command", hidden = true) class Foo { } @Command(name = "bar", description = "This is a foo sub-command", hidden = true) class Bar { } @Command(name = "app", abbreviateSynopsis = true) class App { } CommandLine app = new CommandLine(new App()) .addSubcommand("foo", new Foo()) .addSubcommand("bar", new Bar()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); app.usage(new PrintStream(baos)); String expected = format("" + "Usage: app [COMMAND]%n"); assertEquals(expected, baos.toString()); }
private static CommandLine createNestedCommandWithAliases() { CommandLine commandLine = new CommandLine(new MainCommand()); commandLine .addSubcommand("cmd1", new CommandLine(new ChildCommand1()) .addSubcommand("sub11", new GrandChild1Command1(), "sub11alias1", "sub11alias2") .addSubcommand("sub12", new GrandChild1Command2(), "sub12alias1", "sub12alias2") , "cmd1alias1", "cmd1alias2") .addSubcommand("cmd2", new CommandLine(new ChildCommand2()) .addSubcommand("sub21", new GrandChild2Command1(), "sub21alias1", "sub21alias2") .addSubcommand("sub22", new CommandLine(new GrandChild2Command2()) .addSubcommand("sub22sub1", new GreatGrandChild2Command2_1(), "sub22sub1alias1", "sub22sub1alias2"), "sub22alias1", "sub22alias2") , "cmd2alias1", "cmd2alias2"); return commandLine; }
@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); }
private static CommandLine createNestedCommand() { CommandLine commandLine = new CommandLine(new MainCommand()); commandLine .addSubcommand("cmd1", new CommandLine(new ChildCommand1()) .addSubcommand("sub11", new GrandChild1Command1()) .addSubcommand("sub12", new GrandChild1Command2()) ) .addSubcommand("cmd2", new CommandLine(new ChildCommand2()) .addSubcommand("sub21", new GrandChild2Command1()) .addSubcommand("sub22", new CommandLine(new GrandChild2Command2()) .addSubcommand("sub22sub1", new GreatGrandChild2Command2_1()) ) ); return commandLine; }
public static void main(String[] args) { CommandLine hierarchy = new CommandLine(new TopLevel()) .addSubcommand("sub1", new Sub1()) .addSubcommand("sub2", new CommandLine(new Sub2()) .addSubcommand("subsub1", new Sub2Child1()) .addSubcommand("subsub2", new Sub2Child2()) ); List<CommandLine> commandLines = hierarchy.parse(args); //Collections.reverse(commandLines); for (CommandLine cmdLine : commandLines) { Object command = cmdLine.getCommand(); System.out.printf("Parsed command %s%n", AutoCompleteTest.toString(command)); } } }
@Test public void nestedSubcommands() throws Exception { CommandLine hierarchy = new CommandLine(new TopLevel()) .addSubcommand("sub1", new Sub1()) .addSubcommand("sub2", new CommandLine(new Sub2()) .addSubcommand("subsub1", new Sub2Child1()) .addSubcommand("subsub2", new Sub2Child2()) ); String script = AutoComplete.bash("picocompletion-demo", hierarchy); String expected = format(loadTextFromClasspath("/picocompletion-demo_completion.bash"), CommandLine.VERSION, spaced(TimeUnit.values())); assertEquals(expected, script); }
@Test public void testCustomTypeConverterRegisteredAfterSubcommandsAdded() { @Command class TopLevel { public boolean equals(Object o) {return getClass().equals(o.getClass());}} CommandLine commandLine = new CommandLine(new TopLevel()); commandLine.addSubcommand("main", createNestedCommand()); commandLine.registerConverter(CustomType.class, new CustomType(null)); List<CommandLine> parsed = commandLine.parse("main", "cmd1", "sub12", "-e", "TXT"); assertEquals(4, parsed.size()); assertEquals(TopLevel.class, parsed.get(0).getCommand().getClass()); assertFalse(((MainCommand) parsed.get(1).getCommand()).a); assertFalse(((ChildCommand1) parsed.get(2).getCommand()).b); assertEquals("TXT", ((GrandChild1Command2) parsed.get(3).getCommand()).e.val); }
@Test public void testParentInjectedWhenAddedAsSubcommand() { class Top1 { @Option(names = {"-d", "--directory"}, description = "this option applies to all subcommands") private File baseDirectory; } class Sub1 { @ParentCommand private Top1 parent; @Parameters private int count; } List<CommandLine> result = new CommandLine(new Top1()) .addSubcommand("sub1", new Sub1()) .parse("-d/tmp/blah", "sub1", "3"); Top1 top = result.get(0).getCommand(); assertEquals(new File("/tmp/blah"), top.baseDirectory); Sub1 sub = result.get(1).getCommand(); assertEquals(3, sub.count); assertSame(top, sub.parent); } @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()); }
@Test(expected = MissingTypeConverterException.class) public void testCustomTypeConverterRegisteredBeforeSubcommandsAdded() { @Command class TopLevel {} CommandLine commandLine = new CommandLine(new TopLevel()); commandLine.registerConverter(CustomType.class, new CustomType(null)); commandLine.addSubcommand("main", createNestedCommand()); commandLine.parse("main", "cmd1", "sub12", "-e", "TXT"); }
@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 testIssue443SubcommandWithAliasAnnotation() { Issue443TopLevelCommand top = new Issue443TopLevelCommand(); SubCommandWithAlias sub = new SubCommandWithAlias(); CommandLine cmd = new CommandLine(top).addSubcommand("task", sub); String[] args = {"t"}; List<Object> result = cmd.parseWithHandler(new RunAll(), args); assertTrue("top was executed", top.topWasExecuted); assertTrue("sub was executed", sub.subWasExecuted); }
@Test public void testSubcommandWithoutAnnotationName() { CommandLine top = new CommandLine(new Top563()); top.addSubcommand("subname", new Sub563()); CommandLine sub = top.getSubcommands().get("subname"); assertEquals("subname", sub.getCommandName()); assertEquals("subname", sub.getCommandSpec().name()); assertEquals("<main class> subname", sub.getCommandSpec().qualifiedName()); String expected = String.format("" + "Usage: <main class> subname [-hV]%n" + " -h, --help Show this help message and exit.%n" + " -V, --version Print version information and exit.%n"); sub.usage(System.out); assertEquals(expected, systemOutRule.getLog()); }
@Test public void test381_NPE_whenAddingSubcommand() { CommandSpec toplevel = CommandSpec.create(); toplevel.addOption(OptionSpec.builder("-o").description("o option").build()); CommandSpec sub = CommandSpec.create(); sub.addOption(OptionSpec.builder("-x").description("x option").build()); CommandLine commandLine = new CommandLine(toplevel); commandLine.addSubcommand("sub", sub); // NPE here commandLine.usage(System.out); String expected = String.format("" + "Usage: <main class> [-o] [COMMAND]%n" + " -o o option%n" + "Commands:%n" + " sub%n"); assertEquals(expected, systemOutRule.getLog()); }
@Test public void testSubcommandNameIsInitializedWhenAddedToParent() { CommandSpec toplevel = CommandSpec.create(); toplevel.addOption(OptionSpec.builder("-o").description("o option").build()); CommandSpec sub = CommandSpec.create(); sub.addOption(OptionSpec.builder("-x").description("x option").build()); CommandLine commandLine = new CommandLine(toplevel); CommandLine subCommandLine = new CommandLine(sub); assertEquals("<main class>", sub.name()); assertEquals("<main class>", subCommandLine.getCommandName()); commandLine.addSubcommand("sub", subCommandLine); assertEquals("sub", sub.name()); assertEquals("sub", subCommandLine.getCommandName()); subCommandLine.usage(System.out); String expected = String.format("" + "Usage: <main class> sub [-x]%n" + " -x x option%n"); assertEquals(expected, systemOutRule.getLog()); }
@Test public void testSubcommandNameNotOverwrittenWhenAddedToParent() { CommandSpec toplevel = CommandSpec.create(); toplevel.addOption(OptionSpec.builder("-o").description("o option").build()); CommandSpec sub = CommandSpec.create().name("SOMECOMMAND"); sub.addOption(OptionSpec.builder("-x").description("x option").build()); CommandLine commandLine = new CommandLine(toplevel); CommandLine subCommandLine = new CommandLine(sub); assertEquals("SOMECOMMAND", sub.name()); assertEquals("SOMECOMMAND", subCommandLine.getCommandName()); commandLine.addSubcommand("sub", subCommandLine); assertEquals("SOMECOMMAND", sub.name()); assertEquals("SOMECOMMAND", subCommandLine.getCommandName()); subCommandLine.usage(System.out); String expected = String.format("" + "Usage: <main class> SOMECOMMAND [-x]%n" + " -x x option%n"); assertEquals(expected, systemOutRule.getLog()); }