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