@Test(groups = "fast") public void testNoCommands() { Help onlyHelp = new Help(io, Collections.<CommandBuilder>emptyList()); CliParser parser = new CliParser(onlyHelp.defineCommand(), Collections.<String>emptyList()); onlyHelp.runCommand(parser); Assert.assertEquals(io.getOut(), "help <command_name>\n Displays help for commands\n"); Assert.assertEquals(io.getErr(), ""); }
@Override public void runCommand(CliParser parser) { help(parser.get("command_name"), parser.getTrailing()); } };
public void help(String commandName, List<String> arguments) { List<CliCommand> cliCommands = new ArrayList<>(commands.size() + 1); CommandBuilder selectedCommand = null; for (CommandBuilder command : commands) { CliCommand cliCommand = command.defineCommand(); cliCommands.add(cliCommand); if (cliCommand.getName().equals(commandName)) { selectedCommand = command; } } if (commandName == null) { printSummary(io.out, cliCommands); } else if (selectedCommand instanceof CommandGroup) { ((CommandGroup) selectedCommand).help(arguments); } else if (selectedCommand != null) { io.out.println(selectedCommand.defineCommand().getDocumentation()); } else { printSummary(io.out, cliCommands); io.out.println(); io.out.flush(); throw new ErrorMessage("Unknown commandName: %s", commandName); } }
public CommandDispatcher(IO io, String group, List<CommandBuilder> commands) { this.io = io; commandBuilderMap = new LinkedHashMap<>(); for (CommandBuilder builder : commands) { CliCommand command = builder.defineCommand(); commandBuilderMap.put(command.getName(), builder); } help = new Help(io, group, commands); commandBuilderMap.put(help.defineCommand().getName(), help); CliCommand.Builder dispatchCommand = new CliCommand.Builder("dispatch", "Dispatches commands"); dispatchCommand.addParameter("command").withDefault(null); dispatchCommand.allowTrailingParameters(); this.dispatchCommand = dispatchCommand.build(); }
@Test(groups = "fast") public void testNoOptions() { CliParser parser = new CliParser(help.defineCommand(), Arrays.asList("foo")); help.runCommand(parser); Assert.assertEquals( io.getOut(), "foo\n" + " My awesome foo command\n" ); Assert.assertEquals(io.getErr(), ""); }
help = new Help(io, commands);
@Test(groups = "fast") public void testWithParameter() { CliParser parser = new CliParser(help.defineCommand(), Arrays.asList("pram")); help.runCommand(parser); Assert.assertEquals( io.getOut(), "pram <cool>\n" + " This has a param\n" ); Assert.assertEquals(io.getErr(), ""); }
help = new Help(io, commands);
@Test(groups = "fast") public void testOneOption() { CliParser parser = new CliParser(help.defineCommand(), Arrays.asList("bar")); help.runCommand(parser); Assert.assertEquals( io.getOut(), "bar\n" + " My awesome bar command\n (even more awesome than foo)\n" + "\n" + " -b --bar <bar>\n" + " [Required]\n" ); Assert.assertEquals(io.getErr(), ""); }
@Test(groups = "fast") public void testNoCommands() { Help onlyHelp = new Help(io, Collections.<CommandBuilder>emptyList()); CliParser parser = new CliParser(onlyHelp.defineCommand(), Collections.<String>emptyList()); onlyHelp.runCommand(parser); Assert.assertEquals(io.getOut(), "help <command_name>\n Displays help for commands\n"); Assert.assertEquals(io.getErr(), ""); }
@Test(groups = "fast") public void testCommandGroupHelp() { CliParser parser = new CliParser(help.defineCommand(), Arrays.asList("cmds", "bar")); help.runCommand(parser); Assert.assertEquals( io.getOut(), "bar\n" + " My awesome bar command\n (even more awesome than foo)\n" + "\n" + " -b --bar <bar>\n" + " [Required]\n" ); Assert.assertEquals(io.getErr(), ""); }
@Test(groups = "fast") public void testCommandGroupSummary() { CliParser parser = new CliParser(help.defineCommand(), Arrays.asList("cmds")); help.runCommand(parser); Assert.assertEquals( io.getOut(), "cmds foo\n" + " My awesome foo command\n" + "cmds bar\n" + " My awesome bar command\n (even more awesome than foo)\n" + "cmds help <command_name>\n" + " Displays help for commands\n" ); Assert.assertEquals(io.getErr(), ""); }
@Test(groups = "fast") public void testSeveralOptions() { CliParser parser = new CliParser(help.defineCommand(), Arrays.asList("baz")); help.runCommand(parser); Assert.assertEquals( io.getOut(), "baz\n" + " Meh\n" + "\n" + " -x <hello>\n" + " [Required] Make everything awesome\n" + " -f\n" + " [Optional] A flag of some sort\n" + " that enables something\n" + " -d --default <option>\n" + " [Optional] Default\n" + " default: testing\n" ); Assert.assertEquals(io.getErr(), ""); }
@Test(groups = "fast") public void testWithNotes() { CliParser parser = new CliParser(help.defineCommand(), Arrays.asList("noted")); help.runCommand(parser); Assert.assertEquals( io.getOut(), "noted\n" + " I have notes\n" + "\n" + " -b --bar <bar>\n" + " [Required]\n" + " e.g., hello\n" + " e.g., goodbye\n" + "\n" + " Yes, that's the same option\n" + " (it's a good option)\n" ); Assert.assertEquals(io.getErr(), ""); }
@Test(groups = "fast") public void testCommandSummary() { CliParser parser = new CliParser(help.defineCommand(), Collections.<String>emptyList()); help.runCommand(parser); Assert.assertEquals( io.getOut(), "foo\n" + " My awesome foo command\n" + "bar\n" + " My awesome bar command\n (even more awesome than foo)\n" + "baz\n" + " Meh\n" + "pram <cool>\n" + " This has a param\n" + "noted\n" + " I have notes\n" + "cmds\n" + " Some commands\n" + "help <command_name>\n" + " Displays help for commands\n" ); Assert.assertEquals(io.getErr(), ""); }
@Test(groups = "fast") public void testNoOptions() { CliParser parser = new CliParser(help.defineCommand(), Arrays.asList("foo")); help.runCommand(parser); Assert.assertEquals( io.getOut(), "foo\n" + " My awesome foo command\n" ); Assert.assertEquals(io.getErr(), ""); }
@Test(groups = "fast") public void testWithParameter() { CliParser parser = new CliParser(help.defineCommand(), Arrays.asList("pram")); help.runCommand(parser); Assert.assertEquals( io.getOut(), "pram <cool>\n" + " This has a param\n" ); Assert.assertEquals(io.getErr(), ""); }
@Test(groups = "fast") public void testCommandGroupSummary() { CliParser parser = new CliParser(help.defineCommand(), Arrays.asList("cmds")); help.runCommand(parser); Assert.assertEquals( io.getOut(), "cmds foo\n" + " My awesome foo command\n" + "cmds bar\n" + " My awesome bar command\n (even more awesome than foo)\n" + "cmds help <command_name>\n" + " Displays help for commands\n" ); Assert.assertEquals(io.getErr(), ""); }
@Test(groups = "fast") public void testOneOption() { CliParser parser = new CliParser(help.defineCommand(), Arrays.asList("bar")); help.runCommand(parser); Assert.assertEquals( io.getOut(), "bar\n" + " My awesome bar command\n (even more awesome than foo)\n" + "\n" + " -b --bar <bar>\n" + " [Required]\n" ); Assert.assertEquals(io.getErr(), ""); }
@Test(groups = "fast") public void testCommandGroupHelp() { CliParser parser = new CliParser(help.defineCommand(), Arrays.asList("cmds", "bar")); help.runCommand(parser); Assert.assertEquals( io.getOut(), "bar\n" + " My awesome bar command\n (even more awesome than foo)\n" + "\n" + " -b --bar <bar>\n" + " [Required]\n" ); Assert.assertEquals(io.getErr(), ""); }