Example usage for org.apache.commons.cli DefaultParser DefaultParser

List of usage examples for org.apache.commons.cli DefaultParser DefaultParser

Introduction

In this page you can find the example usage for org.apache.commons.cli DefaultParser DefaultParser.

Prototype

DefaultParser

Source Link

Usage

From source file:com.google.endpoints.examples.bookstore.BookstoreServer.java

public static void main(String[] args) throws Exception {
    Options options = createOptions();//from  w  w w. ja  v  a 2  s  .  c  o  m
    CommandLineParser parser = new DefaultParser();
    CommandLine line;
    try {
        line = parser.parse(options, args);
    } catch (ParseException e) {
        System.err.println("Invalid command line: " + e.getMessage());
        printUsage(options);
        return;
    }

    int port = DEFAULT_PORT;

    if (line.hasOption("port")) {
        String portOption = line.getOptionValue("port");
        try {
            port = Integer.parseInt(portOption);
        } catch (java.lang.NumberFormatException e) {
            System.err.println("Invalid port number: " + portOption);
            printUsage(options);
            return;
        }
    }

    final BookstoreData data = initializeBookstoreData();
    final BookstoreServer server = new BookstoreServer();
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                System.out.println("Shutting down");
                server.stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    server.start(port, data);
    System.out.format("Bookstore service listening on %d\n", port);
    server.blockUntilShutdown();
}

From source file:com.google.api.codegen.config.ConfigGeneratorTool.java

public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("h", "help", false, "show usage");
    options.addOption(Option.builder().longOpt("descriptor_set")
            .desc("The descriptor set representing the compiled input protos.").hasArg()
            .argName("DESCRIPTOR-SET").required(true).build());
    options.addOption(//ww  w  .  j a v a 2  s .c  o m
            Option.builder("o").longOpt("output").desc("The directory in which to output the generated config.")
                    .hasArg().argName("OUTPUT-FILE").required(true).build());

    CommandLine cl = (new DefaultParser()).parse(options, args);
    if (cl.hasOption("help")) {
        HelpFormatter formater = new HelpFormatter();
        formater.printHelp("ConfigGeneratorTool", options);
    }

    generate(cl.getOptionValue("descriptor_set"), cl.getOptionValue("output"));
}

From source file:com.google.cloud.trace.v1.TraceServiceSmokeTest.java

public static void main(String args[]) {
    Logger.getLogger("").setLevel(Level.WARNING);
    try {//from ww  w .j a  v  a  2 s.c  om
        Options options = new Options();
        options.addOption("h", "help", false, "show usage");
        options.addOption(Option.builder().longOpt("project_id").desc("Project id").hasArg()
                .argName("PROJECT-ID").required(true).build());
        CommandLine cl = (new DefaultParser()).parse(options, args);
        if (cl.hasOption("help")) {
            HelpFormatter formater = new HelpFormatter();
            formater.printHelp("TraceServiceSmokeTest", options);
        }
        executeNoCatch(cl.getOptionValue("project_id"));
        System.out.println("OK");
    } catch (Exception e) {
        System.err.println("Failed with exception:");
        e.printStackTrace(System.err);
        System.exit(1);
    }
}

From source file:com.linkedin.python.importer.ImporterCLI.java

public static void main(String[] args) throws Exception {

    Options options = createOptions();//from   w  w  w. j a  v  a  2  s .  com

    CommandLineParser parser = new DefaultParser();
    CommandLine line = parser.parse(options, args);

    if (line.hasOption("quite")) {
        Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
        root.setLevel(Level.WARN);
    }

    final File repoPath = new File(line.getOptionValue("repo"));
    final DependencySubstitution replacements = new DependencySubstitution(buildSubstitutionMap(line));

    repoPath.mkdirs();

    if (!repoPath.exists() || !repoPath.isDirectory()) {
        throw new RuntimeException(
                "Unable to continue, " + repoPath.getAbsolutePath() + " does not exist, or is not a directory");
    }

    for (String dependency : line.getArgList()) {
        new DependencyDownloader(dependency, repoPath, replacements).download();
    }

    logger.info("Execution Finished!");
}

From source file:com.google.cloud.pubsub.v1.TopicAdminSmokeTest.java

public static void main(String args[]) {
    Logger.getLogger("").setLevel(Level.WARNING);
    try {/*w w  w  .  j  a v a2 s .  c om*/
        Options options = new Options();
        options.addOption("h", "help", false, "show usage");
        options.addOption(Option.builder().longOpt("project_id").desc("Project id").hasArg()
                .argName("PROJECT-ID").required(true).build());
        CommandLine cl = (new DefaultParser()).parse(options, args);
        if (cl.hasOption("help")) {
            HelpFormatter formater = new HelpFormatter();
            formater.printHelp("TopicAdminSmokeTest", options);
        }
        executeNoCatch(cl.getOptionValue("project_id"));
        System.out.println("OK");
    } catch (Exception e) {
        System.err.println("Failed with exception:");
        e.printStackTrace(System.err);
        System.exit(1);
    }
}

From source file:com.navercorp.client.Main.java

public static void main(String[] args) {
    Client clnt = null;//from w  w  w.  jav  a 2s. c  o  m
    try {
        CommandLine cmd = new DefaultParser()
                .parse(new Options().addOption("z", true, "zookeeper address (ip:port,ip:port,...)")
                        .addOption("t", true, "zookeeper connection timeout")
                        .addOption("c", true, "command and arguments"), args);

        final String connectionString = cmd.hasOption("z") ? cmd.getOptionValue("z") : "127.0.0.1:2181";
        final int timeout = cmd.hasOption("t") ? Integer.valueOf(cmd.getOptionValue("t")) : 10000;
        clnt = new Client(connectionString, timeout);

        String command;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        while ((command = in.readLine()) != null) {
            printResult(clnt.execute(command.split(" ")));
        }
        System.exit(Code.OK.n());
    } catch (ParseException e) {
        e.printStackTrace(System.err);
        System.err.println(Client.getUsage());
        System.exit(INVALID_ARGUMENT.n());
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(ZK_CONNECTION_LOSS.n());
    } catch (InterruptedException e) {
        e.printStackTrace(System.err);
        System.exit(INTERNAL_ERROR.n());
    } finally {
        if (clnt != null) {
            try {
                clnt.close();
            } catch (Exception e) {
                ; // nothing to do
            }
        }
    }
}

From source file:com.garethahealy.quotalimitsgenerator.cli.Application.java

public static void main(String[] args) {
    LOG.info("Starting...");

    DefaultCLIParser parser = new DefaultCLIParser(new DefaultParser());

    try {/*from  w w  w.  ja  v  a  2  s  .  c om*/
        CommandLine commandLine = parser.parse(args, parser.getOptions());
        CLIOptions options = parser.getParsedOptions(commandLine);

        LOG.info(options.toString());

        QuotaLimitModel quotaLimitModel = options.calculate();

        LOG.info(quotaLimitModel.toString());

        YamlTemplateProcessor yamlTemplateProcessor = new YamlTemplateProcessor();
        yamlTemplateProcessor.init();

        yamlTemplateProcessor.process(quotaLimitModel);
    } catch (NumberFormatException | IOException | ParseException | URISyntaxException | TemplateException ex) {
        LOG.error("We hit a problem! {}", ExceptionUtils.getStackTrace(ex));

        parser.displayHelp(false);
    }
}

From source file:com.floragunn.searchguard.tools.Hasher.java

public static void main(final String[] args) {

    final Options options = new Options();
    final HelpFormatter formatter = new HelpFormatter();
    options.addOption(/*from   ww w  .  j  a  v a  2s . c om*/
            Option.builder("p").argName("password").hasArg().desc("Cleartext password to hash").build());
    options.addOption(Option.builder("env").argName("name environment variable").hasArg()
            .desc("name environment variable to read password from").build());

    final CommandLineParser parser = new DefaultParser();
    try {
        final CommandLine line = parser.parse(options, args);

        if (line.hasOption("p")) {
            System.out.println(hash(line.getOptionValue("p").getBytes("UTF-8")));
        } else if (line.hasOption("env")) {
            final String pwd = System.getenv(line.getOptionValue("env"));
            if (pwd == null || pwd.isEmpty()) {
                throw new Exception("No environment variable '" + line.getOptionValue("env") + "' set");
            }
            System.out.println(hash(pwd.getBytes("UTF-8")));
        } else {
            final Console console = System.console();
            if (console == null) {
                throw new Exception("Cannot allocate a console");
            }
            final char[] passwd = console.readPassword("[%s]", "Password:");
            System.out.println(hash(new String(passwd).getBytes("UTF-8")));
        }
    } catch (final Exception exp) {
        System.err.println("Parsing failed.  Reason: " + exp.getMessage());
        formatter.printHelp("hasher.sh", options, true);
        System.exit(-1);
    }
}

From source file:com.falcon.orca.Main.java

public static void main(String[] args) throws IOException {
    Option mode = Option.builder("mo").longOpt("mode").required(true).hasArg(true)
            .desc("Mode to start the node " + "in, possible values are standalone master slave").build();
    Option host = Option.builder("ho").longOpt("host").hasArg(true).desc("Machine name").build();
    Options modeOptions = new Options();
    modeOptions.addOption(mode);//w w  w.j a v  a2 s .  co  m
    modeOptions.addOption(host);
    CommandLineParser commandLineParser = new DefaultParser();
    ModeHandler handler;
    final ActorSystem actorSystem = ActorSystem.create();
    String machineName = "127.0.0.1";
    try {
        CommandLine commandLine = commandLineParser.parse(modeOptions, args);
        if (commandLine.hasOption("host")) {
            machineName = commandLine.getOptionValue("host");
        }
        if (commandLine.hasOption("mode")) {
            String modeValue = commandLine.getOptionValue("mode");
            if ("standalone".equalsIgnoreCase(modeValue)) {
                handler = new StandAloneHandler(actorSystem);
                handler.handle();
            } else if ("master".equalsIgnoreCase(modeValue)) {
                handler = new MasterHandler(actorSystem, 1, machineName);
                handler.handle();
            } else if ("slave".equalsIgnoreCase(modeValue)) {
                handler = new SlaveHandler(actorSystem);
                handler.handle();
            } else {
                actorSystem.shutdown();
                System.out
                        .println("Mode is required, use -mo or --mode, possible values are standalone, master "
                                + "and slave");
            }
        } else {
            actorSystem.shutdown();
            System.out
                    .println("Mode is required, use -mo or --mode, possible values are standalone, master and "
                            + "slave");
        }
    } catch (ParseException e) {
        actorSystem.shutdown();
        System.out.println(
                "Mode is required, use -mo or --mode, possible values are standalone, master and slave");
    }
}

From source file:com.google.cloud.monitoring.v3.MetricServiceSmokeTest.java

public static void main(String args[]) {
    Logger.getLogger("").setLevel(Level.WARNING);
    try {//  w ww.j  a  v a  2  s  .  c o m
        Options options = new Options();
        options.addOption("h", "help", false, "show usage");
        options.addOption(Option.builder().longOpt("project_id").desc("Project id").hasArg()
                .argName("PROJECT-ID").required(true).build());
        CommandLine cl = (new DefaultParser()).parse(options, args);
        if (cl.hasOption("help")) {
            HelpFormatter formater = new HelpFormatter();
            formater.printHelp("MetricServiceSmokeTest", options);
        }
        executeNoCatch(cl.getOptionValue("project_id"));
        System.out.println("OK");
    } catch (Exception e) {
        System.err.println("Failed with exception:");
        e.printStackTrace(System.err);
        System.exit(1);
    }
}