CommandManager implements Command { public CommandState execute(CommandState state, String[] args) throws Exception { if ((args.length == 0) || "help".equals(args[0]) && (args.length == 1)) { printHelp(); return state; } String commandName = args[0]; if ("help".equals(commandName)) { commandName = args[1]; Command cmd = commands.get(commandName); CommandLineHelper.printHelp(cmd.getUsage(), cmd.getOptions()); return state; } else if ("version".equals(commandName)) { CommandLineHelper.printVersion(); return state; } CommandLine line = CommandLineHelper.maybeGetCommandLine(getUsage(), getOptions(), args, true); if (line == null) { return null; } if (state == null) { state = new CommandState(); } List<String> arguments = new ArrayList<>(Arrays.asList(args)); List<String> globalOptionArgs = getOptionArgs(globalOptions, arguments); if (arguments.size() == 0) { throw new IllegalArgumentException(missingCommandError); } while (arguments.size() > 0) { state = executeCommand(state, globalOptionArgs, arguments); } return state; } CommandManager(); String getName(); String getDescription(); String getUsage(); Options getOptions(); void addCommand(String commandName, Command command); List<String> getOptionArgs(Options options, List<String> arguments); void main(String[] args); CommandState execute(CommandState state, String[] args); CommandState executeCommand(
      CommandState state, List<String> globalOptionArgs, List<String> arguments); void printHelp(); void printHelpEntry(Map.Entry<String, Command> entry); void printHelpEntry(String name, String description);  }
@Test public void testExecute() throws Exception { reset(); manager.execute(null, split("--help mock1")); manager.execute(null, split("--help mock1 mock2")); }