/** * 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); }
@Before public void setUp() { // use a real, dummy Subparser impl to avoid having to mock out every single call final ArgumentParser parser = ArgumentParsers.newArgumentParser("test"); final Subparser subparser = parser.addSubparsers().addParser("inspect"); command = new DeploymentGroupInspectCommand(subparser); when(client.deploymentGroup(NAME)).thenReturn(Futures.immediateFuture(DEPLOYMENT_GROUP)); final ListenableFuture<DeploymentGroup> nullFuture = Futures.immediateFuture(null); when(client.deploymentGroup(NON_EXISTENT_NAME)).thenReturn(nullFuture); }
private ArgumentParser buildParser(JarLocation location) { final String usage = "java -jar " + location; final ArgumentParser p = ArgumentParsers.newArgumentParser(usage, false); p.version( location .getVersion() .or( "No application version detected. Add a Implementation-Version " + "entry to your JAR's manifest to enable this.")); addHelp(p); p.addArgument("-v", "--version") .action(Arguments.help()) // never gets called; intercepted in #run .help("show the application version and exit"); return p; }
@Override public void run( ArgumentParser parser, Argument arg, Map<String, Object> attrs, String flag, Object value) throws ArgumentParserException { parser.printVersion(); System.exit(0); }
@Override public void run( ArgumentParser parser, Argument arg, Map<String, Object> attrs, String flag, Object value) throws ArgumentParserException { parser.printHelp(out); attrs.put("is-help", Boolean.TRUE); }
public static Namespace handleArgumentString(String[] args, ArgumentParser parser) { Namespace parsedArgs = null; try { parsedArgs = parser.parseArgs(args); String selectedModule = parsedArgs.getString("Module"); try { String propPath = parsedArgs.getString("propertyFile"); if (propPath != null) { parsedArgs = loadProperties(propPath); // 선택된 모듈이 무엇이었는지 다시 기억 parsedArgs.getAttrs().put("Module", selectedModule); } } catch (IOException e) { e.printStackTrace(); System.exit(1); } } catch (ArgumentParserException e) { parser.handleError(e); System.exit(1); } // 만약 output 디렉토리가 존재하지 않으면 새로 생성 Path outputPath = null; try { String outputPathString = parsedArgs.getString("outputDirPath"); if (outputPathString != null) { outputPath = Paths.get(outputPathString); if (!Files.exists(outputPath)) { Files.createDirectories(outputPath); } } } catch (NullPointerException e) { // System.err.println(); } catch (InvalidPathException e) { } catch (IOException e) { e.printStackTrace(); } return parsedArgs; }
public static void configureArguments(ArgumentParser parser) { parser.description("Trains a recommendation model and write it to disk."); ScriptEnvironment.configureArguments(parser); InputData.configureArguments(parser); parser .addArgument("-o", "--output-file") .type(File.class) .metavar("FILE") .setDefault("model.bin") .help("write trained model to FILE"); parser .addArgument("config") .type(File.class) .nargs("+") .metavar("CONFIG") .help("load algorithm configuration from CONFIG"); }
private static void loadEnvironment(String[] args) { final String defaultConfDir = getProperty("dash.application.conf.dir"); final String defaultConfFile = (defaultConfDir != null ? defaultConfDir + File.separatorChar : "") + "conf.yml"; ArgumentParser parser = ArgumentParsers.newArgumentParser("Dash", true) .description("Runs a performance test mix.") .version("${prog} " + Version.id()) .epilog("Dash is a free software under Apache License Version 2.0"); parser .addArgument("-c", "--conf") .help("the config file containing the test specification to run (default: ../conf/conf.yml") .required(false) .setDefault(new File(defaultConfFile)) .type(File.class); parser .addArgument("-t", "--test") .help("the name of the test to run") .required(true) .type(String.class); parser .addArgument("-v", "--version") .help("print the version number") .action(Arguments.version()); try { YamlEnv yamlEnv = new YamlEnv(); Namespace namespace = parser.parseArgs(args); @SuppressWarnings("unchecked") HashMap<String, Object> env = (HashMap<String, Object>) yamlEnv.loadProperties((File) namespace.get("conf")); String test = namespace.getString("test"); @SuppressWarnings("unchecked") HashMap<String, Object> testSpec = (HashMap<String, Object>) env.get(test); if (testSpec == null) { System.err.println("Test spec (" + test + ") does not exist in the config file."); throw new Error(); // todo: message or log or exit } for (Map.Entry<String, Object> entry : testSpec.entrySet()) { if (entry.getValue() != null) { System.setProperty(entry.getKey(), entry.getValue().toString()); } } } catch (ArgumentParserException e) { parser.handleError(e); } catch (IOException e) { e.printStackTrace(); } }
public static void configureArguments(ArgumentParser parser) { InputData.configureArguments(parser); ScriptEnvironment.configureArguments(parser); parser .addArgument("-c", "--config-file") .type(File.class) .action(Arguments.append()) .metavar("FILE") .help("use configuration from FILE"); parser .addArgument("-m", "--model-file") .type(File.class) .metavar("FILE") .help("load model from FILE"); parser.addArgument("--print-channel").metavar("CHAN").help("also print value from CHAN"); parser.addArgument("user").type(Long.class).metavar("USER").help("predict for USER"); parser .addArgument("items") .type(Long.class) .metavar("ITEM") .nargs("+") .help("predict for ITEMs"); }
private void addHelp(ArgumentParser p) { p.addArgument("-h", "--help") .action(new SafeHelpAction(stdOut)) .help("show this help message and exit") .setDefault(Arguments.SUPPRESS); }
public ServiceParser(final String programName, final String description, final String... args) throws ArgumentParserException { final ArgumentParser parser = ArgumentParsers.newArgumentParser(programName).defaultHelp(true).description(description); nameArg = parser.addArgument("--name").setDefault(getHostName()).help("hostname to register as"); domainArg = parser .addArgument("--domain") .setDefault(ResolverConfReader.getDomainFromResolverConf("/etc/resolv.conf")) .help("Service registration domain."); serviceRegistryArg = parser .addArgument("--service-registry") .help("Service registry address. Overrides domain."); serviceRegistrarPluginArg = parser .addArgument("--service-registrar-plugin") .type(fileType().verifyExists().verifyCanRead()) .help("Service registration plugin."); zooKeeperConnectStringArg = parser.addArgument("--zk").setDefault("localhost:2181").help("zookeeper connection string"); zooKeeperSessiontimeoutArg = parser .addArgument("--zk-session-timeout") .type(Integer.class) .setDefault((int) SECONDS.toMillis(60)) .help("zookeeper session timeout"); zooKeeperConnectiontimeoutArg = parser .addArgument("--zk-connection-timeout") .type(Integer.class) .setDefault((int) SECONDS.toMillis(15)) .help("zookeeper connection timeout"); zooKeeperClusterId = parser .addArgument("--zk-cluster-id") .type(String.class) .setDefault((String) null) .help("Optional cluster ID to ensure we are connected to the right cluster"); noZooKeeperRegistrationArg = parser .addArgument("--no-zk-registration") .setDefault(SUPPRESS) .action(storeTrue()) .help("Do not register this master in zookeeper. Useful for debugging."); zooKeeperEnableAcls = parser .addArgument("--zk-enable-acls") .action(storeTrue()) .setDefault(false) .help("Enable zookeeper ACLs."); zooKeeperAclMasterUser = parser .addArgument("--zk-acl-master-user") .type(String.class) .setDefault("helios-master") .help("zookeeper ACL username used for masters."); zooKeeperAclAgentUser = parser .addArgument("--zk-acl-agent-user") .type(String.class) .setDefault("helios-agent") .help("zookeeper ACL username used for agents."); noMetricsArg = parser .addArgument("--no-metrics") .setDefault(SUPPRESS) .action(storeTrue()) .help("Turn off all collection and reporting of metrics"); statsdHostPortArg = parser .addArgument("--statsd-host-port") .setDefault((String) null) .help( "host:port of where to send statsd metrics " + "(to be useful, --no-metrics must *NOT* be specified)"); riemannHostPortArg = parser .addArgument("--riemann-host-port") .setDefault((String) null) .help( "host:port of where to send riemann events and metrics " + "(to be useful, --no-metrics must *NOT* be specified)"); verboseArg = parser.addArgument("-v", "--verbose").action(Arguments.count()); syslogArg = parser.addArgument("--syslog").help("Log to syslog.").action(storeTrue()); logconfigArg = parser .addArgument("--logconfig") .type(fileType().verifyExists().verifyCanRead()) .help("Logback configuration file."); noLogSetupArg = parser.addArgument("--no-log-setup").action(storeTrue()).help(SUPPRESS); sentryDsnArg = parser .addArgument("--sentry-dsn") .setDefault((String) null) .help("The sentry data source name"); kafkaArg = parser .addArgument("--kafka") .action(append()) .setDefault(new ArrayList<String>()) .help("Kafka brokers to bootstrap with"); stateDirArg = parser .addArgument("--state-dir") .setDefault(".") .help("Directory for persisting state locally."); ffwdEnabled = parser .addArgument("--ffwd-enabled") .action(storeTrue()) .setDefault(false) .help( "Enables reporting of metrics to FastForward. " + "Only functional when --no-metrics is not set."); ffwdAddress = parser .addArgument("--ffwd-address") .help( "Overrides the default FastForward address, " + "should contain host and port like `host:port`."); ffwdInterval = parser .addArgument("--ffwd-interval-secs") .type(Integer.class) .setDefault(30) .help("Interval in seconds at which to report metrics to FastForward."); ffwdMetricKey = parser .addArgument("--ffwd-key") .setDefault(programName) .help( "Value to use for `key` in metric data sent to FastForward. " + "Defaults to '" + programName + "'"); addArgs(parser); try { this.options = parser.parseArgs(args); } catch (ArgumentParserException e) { parser.handleError(e); throw e; } }
public static void main(String[] args) { EccoService eccoService = new EccoService(); CLI cli = new CLI(eccoService); // # PICK APART ARGUMENTS ... ######################################### // TODO: parse arguments ArgumentParser parser = ArgumentParsers.newArgumentParser("ecco").description("ECCO Description."); Subparsers subparsers = parser .addSubparsers() .title("subcommands") .description("valid subcommands") .help("additional help") .metavar("COMMAND") .dest("command"); // initialize empty local repository Subparser parserInit = subparsers.addParser("init").help("init help"); // status of local repository and working copy Subparser parserStatus = subparsers.addParser("status").help("status help"); // get a configuration property <name> Subparser parserGet = subparsers.addParser("get").help("get help"); parserGet.addArgument("name"); // set a configuration property <name> to <value> Subparser parserSet = subparsers.addParser("set").help("set help"); parserSet.addArgument("name"); parserSet.addArgument("value"); // checkout a configuration from the local repository as working copy (composition) Subparser parserCheckout = subparsers.addParser("checkout").help("checkout help"); parserCheckout.addArgument("configurationString"); // commit the working copy as a new configuration into the local repository Subparser parserCommit = subparsers.addParser("commit").help("commit help"); parserCommit.addArgument("configurationString").required(false); // TODO: clone (cloning remote locally), fetch (fetching changes from remote), update (update // working copy), pull (fetch + update), push (push changes in local repository to remote), ... try { Namespace res = parser.parseArgs(args); System.out.println(res); switch (res.getString("command")) { case "init": cli.init(); break; case "status": cli.status(); break; case "get": cli.getProperty(res.getString("name")); break; case "set": cli.setProperty(res.getString("name"), res.getString("value")); break; case "checkout": cli.checkout(res.getString("configurationString")); break; case "commit": if (res.getString("configurationString") != null) cli.commit(res.getString("configurationString")); break; } } catch (HelpScreenException e) { parser.handleError(e); } catch (ArgumentParserException e) { System.err.println("ERROR: " + e.getMessage()); } catch (EccoException e) { System.err.println("ERROR: " + e.getMessage()); } }