/** * Runs the command line interface given some arguments. * * @param arguments the command line arguments * @return whether or not the command successfully executed * @throws Exception if something goes wrong */ public boolean run(String... arguments) throws Exception { try { if (isFlag(HELP, arguments)) { parser.printHelp(stdOut); } else if (isFlag(VERSION, arguments)) { parser.printVersion(stdOut); } else { final Namespace namespace = parser.parseArgs(arguments); if (namespace.get("is-help") == null) { final Command command = commands.get(namespace.getString(COMMAND_NAME_ATTR)); command.run(bootstrap, namespace); } } return true; } catch (ArgumentParserException e) { // TODO: 5/25/13 <coda> -- make ArgumentParser#handleError not depend on System.err stdErr.println(e.getMessage()); e.getParser().printHelp(stdErr); return false; } catch (ConfigurationException e) { // TODO: 7/26/13 <ntelford> -- as above, this probably shouldn't depend on System.err stdErr.println(e.getMessage()); return false; } }
private void addCommand(Command command) { commands.put(command.getName(), command); parser.addSubparsers().help("available commands"); final Subparser subparser = parser.addSubparsers().addParser(command.getName(), false); command.configure(subparser); addHelp(subparser); subparser .description(command.getDescription()) .setDefault(COMMAND_NAME_ATTR, command.getName()) .defaultHelp(true); }