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(), ""); }
@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 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 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(), ""); }
@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 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 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 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(), ""); }
@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 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 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 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(), ""); }