Search in sources :

Example 46 with PosixParser

use of org.apache.commons.cli.PosixParser in project hbase by apache.

the class ThriftServer method processOptions.

/**
   * Parse the command line options to set parameters the conf.
   */
private void processOptions(final String[] args) throws Exception {
    Options options = new Options();
    options.addOption("b", BIND_OPTION, true, "Address to bind " + "the Thrift server to. [default: " + DEFAULT_BIND_ADDR + "]");
    options.addOption("p", PORT_OPTION, true, "Port to bind to [default: " + DEFAULT_LISTEN_PORT + "]");
    options.addOption("f", FRAMED_OPTION, false, "Use framed transport");
    options.addOption("c", COMPACT_OPTION, false, "Use the compact protocol");
    options.addOption("h", "help", false, "Print help information");
    options.addOption(null, "infoport", true, "Port for web UI");
    options.addOption("m", MIN_WORKERS_OPTION, true, "The minimum number of worker threads for " + ImplType.THREAD_POOL.simpleClassName());
    options.addOption("w", MAX_WORKERS_OPTION, true, "The maximum number of worker threads for " + ImplType.THREAD_POOL.simpleClassName());
    options.addOption("q", MAX_QUEUE_SIZE_OPTION, true, "The maximum number of queued requests in " + ImplType.THREAD_POOL.simpleClassName());
    options.addOption("k", KEEP_ALIVE_SEC_OPTION, true, "The amount of time in secods to keep a thread alive when idle in " + ImplType.THREAD_POOL.simpleClassName());
    options.addOption("t", READ_TIMEOUT_OPTION, true, "Amount of time in milliseconds before a server thread will timeout " + "waiting for client to send data on a connected socket. Currently, " + "only applies to TBoundedThreadPoolServer");
    options.addOptionGroup(ImplType.createOptionGroup());
    CommandLineParser parser = new PosixParser();
    CommandLine cmd = parser.parse(options, args);
    // This is so complicated to please both bin/hbase and bin/hbase-daemon.
    // hbase-daemon provides "start" and "stop" arguments
    // hbase should print the help if no argument is provided
    List<String> commandLine = Arrays.asList(args);
    boolean stop = commandLine.contains("stop");
    boolean start = commandLine.contains("start");
    boolean invalidStartStop = (start && stop) || (!start && !stop);
    if (cmd.hasOption("help") || invalidStartStop) {
        if (invalidStartStop) {
            LOG.error("Exactly one of 'start' and 'stop' has to be specified");
        }
        printUsageAndExit(options, 1);
    }
    // Get port to bind to
    try {
        if (cmd.hasOption(PORT_OPTION)) {
            int listenPort = Integer.parseInt(cmd.getOptionValue(PORT_OPTION));
            conf.setInt(ThriftServerRunner.PORT_CONF_KEY, listenPort);
        }
    } catch (NumberFormatException e) {
        LOG.error("Could not parse the value provided for the port option", e);
        printUsageAndExit(options, -1);
    }
    // check for user-defined info server port setting, if so override the conf
    try {
        if (cmd.hasOption("infoport")) {
            String val = cmd.getOptionValue("infoport");
            conf.setInt("hbase.thrift.info.port", Integer.parseInt(val));
            LOG.debug("Web UI port set to " + val);
        }
    } catch (NumberFormatException e) {
        LOG.error("Could not parse the value provided for the infoport option", e);
        printUsageAndExit(options, -1);
    }
    // Make optional changes to the configuration based on command-line options
    optionToConf(cmd, MIN_WORKERS_OPTION, conf, TBoundedThreadPoolServer.MIN_WORKER_THREADS_CONF_KEY);
    optionToConf(cmd, MAX_WORKERS_OPTION, conf, TBoundedThreadPoolServer.MAX_WORKER_THREADS_CONF_KEY);
    optionToConf(cmd, MAX_QUEUE_SIZE_OPTION, conf, TBoundedThreadPoolServer.MAX_QUEUED_REQUESTS_CONF_KEY);
    optionToConf(cmd, KEEP_ALIVE_SEC_OPTION, conf, TBoundedThreadPoolServer.THREAD_KEEP_ALIVE_TIME_SEC_CONF_KEY);
    optionToConf(cmd, READ_TIMEOUT_OPTION, conf, ThriftServerRunner.THRIFT_SERVER_SOCKET_READ_TIMEOUT_KEY);
    // Set general thrift server options
    boolean compact = cmd.hasOption(COMPACT_OPTION) || conf.getBoolean(ThriftServerRunner.COMPACT_CONF_KEY, false);
    conf.setBoolean(ThriftServerRunner.COMPACT_CONF_KEY, compact);
    boolean framed = cmd.hasOption(FRAMED_OPTION) || conf.getBoolean(ThriftServerRunner.FRAMED_CONF_KEY, false);
    conf.setBoolean(ThriftServerRunner.FRAMED_CONF_KEY, framed);
    if (cmd.hasOption(BIND_OPTION)) {
        conf.set(ThriftServerRunner.BIND_CONF_KEY, cmd.getOptionValue(BIND_OPTION));
    }
    ImplType.setServerImpl(cmd, conf);
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Example 47 with PosixParser

use of org.apache.commons.cli.PosixParser in project zookeeper by apache.

the class DeleteAllCommand method parse.

@Override
public CliCommand parse(String[] cmdArgs) throws CliParseException {
    Parser parser = new PosixParser();
    CommandLine cl;
    try {
        cl = parser.parse(options, cmdArgs);
    } catch (ParseException ex) {
        throw new CliParseException(ex);
    }
    args = cl.getArgs();
    if (args.length < 2) {
        throw new CliParseException(getUsageStr());
    }
    return this;
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) ParseException(org.apache.commons.cli.ParseException) Parser(org.apache.commons.cli.Parser) PosixParser(org.apache.commons.cli.PosixParser)

Example 48 with PosixParser

use of org.apache.commons.cli.PosixParser in project zookeeper by apache.

the class GetAclCommand method parse.

@Override
public CliCommand parse(String[] cmdArgs) throws CliParseException {
    Parser parser = new PosixParser();
    try {
        cl = parser.parse(options, cmdArgs);
    } catch (ParseException ex) {
        throw new CliParseException(ex);
    }
    args = cl.getArgs();
    if (args.length < 2) {
        throw new CliParseException(getUsageStr());
    }
    return this;
}
Also used : PosixParser(org.apache.commons.cli.PosixParser) ParseException(org.apache.commons.cli.ParseException) Parser(org.apache.commons.cli.Parser) PosixParser(org.apache.commons.cli.PosixParser)

Example 49 with PosixParser

use of org.apache.commons.cli.PosixParser in project zookeeper by apache.

the class RemoveWatchesCommand method parse.

@Override
public CliCommand parse(String[] cmdArgs) throws CliParseException {
    Parser parser = new PosixParser();
    try {
        cl = parser.parse(options, cmdArgs);
    } catch (ParseException ex) {
        throw new CliParseException(ex);
    }
    args = cl.getArgs();
    if (args.length < 2) {
        throw new CliParseException(getUsageStr());
    }
    return this;
}
Also used : PosixParser(org.apache.commons.cli.PosixParser) ParseException(org.apache.commons.cli.ParseException) Parser(org.apache.commons.cli.Parser) PosixParser(org.apache.commons.cli.PosixParser)

Example 50 with PosixParser

use of org.apache.commons.cli.PosixParser in project glacier-cli by carlossg.

the class Glacier method main.

public static void main(String[] args) throws Exception {
    options = commonOptions();
    try {
        CommandLineParser parser = new PosixParser();
        CommandLine cmd = parser.parse(options, args);
        List<String> arguments = Arrays.asList(cmd.getArgs());
        if (cmd.hasOption("help")) {
            usage();
        // Not reached
        }
        if (arguments.isEmpty()) {
            throw new GlacierCliException("Must provide at least one command.");
        }
        GlacierCliCommand command = GlacierCliCommand.get(arguments.get(0));
        if (command == null) {
            throw new GlacierCliException("Invalid command given: " + arguments.get(0));
        }
        String defaultPropertiesPath = System.getProperty("user.home") + "/AwsCredentials.properties";
        String propertiesPath = cmd.getOptionValue("properties", defaultPropertiesPath);
        File props = new File(propertiesPath);
        AWSCredentials credentials = new PropertiesCredentials(props);
        Glacier glacier = new Glacier(credentials, cmd.getOptionValue("region", "us-east-1"));
        switch(command) {
            // Archive commands
            case UPLOAD:
                if (arguments.size() < 3) {
                    throw new GlacierCliException("The upload command requires at least three parameters.");
                }
                for (String archive : arguments.subList(2, arguments.size())) {
                    glacier.upload(arguments.get(1), archive);
                }
                break;
            case DELETE:
                if (arguments.size() != 3) {
                    throw new GlacierCliException("The delete command requires exactly three parameters.");
                }
                glacier.delete(arguments.get(1), arguments.get(2));
                break;
            case DOWNLOAD:
                if (arguments.size() != 4) {
                    throw new GlacierCliException("The download command requires exactly four parameters.");
                }
                glacier.download(arguments.get(1), arguments.get(2), arguments.get(3));
                break;
            // Vault commands
            case CREATE_VAULT:
                if (arguments.size() != 2) {
                    throw new GlacierCliException("The create-vault command requires exactly one parameter.");
                }
                glacier.createVault(arguments.get(1));
                break;
            case DELETE_VAULT:
                if (arguments.size() != 2) {
                    throw new GlacierCliException("The delete-vault command requires exactly two parameters.");
                }
                glacier.deleteVault(arguments.get(1));
                break;
            case INVENTORY:
                if (arguments.size() != 2) {
                    throw new GlacierCliException("The inventory command requires exactly two parameters.");
                }
                glacier.inventory(arguments.get(1), cmd.getOptionValue("topic", "glacier"), cmd.getOptionValue("queue", "glacier"), cmd.getOptionValue("file", "glacier.json"));
                break;
            case INFO:
                if (arguments.size() != 2) {
                    throw new GlacierCliException("The info command requires exactly two parameters.");
                }
                glacier.info(arguments.get(1));
                break;
            case LIST:
                glacier.list();
                break;
        }
    } catch (GlacierCliException e) {
        System.err.println("error: " + e.getMessage());
        System.err.println();
        usage();
    } catch (UnrecognizedOptionException e) {
        System.err.println("error: Invalid argument: " + e.getOption());
        usage();
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) PropertiesCredentials(com.amazonaws.auth.PropertiesCredentials) CommandLineParser(org.apache.commons.cli.CommandLineParser) File(java.io.File) AWSCredentials(com.amazonaws.auth.AWSCredentials) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException)

Aggregations

PosixParser (org.apache.commons.cli.PosixParser)79 CommandLine (org.apache.commons.cli.CommandLine)74 CommandLineParser (org.apache.commons.cli.CommandLineParser)61 Options (org.apache.commons.cli.Options)52 ParseException (org.apache.commons.cli.ParseException)50 IOException (java.io.IOException)19 File (java.io.File)17 HelpFormatter (org.apache.commons.cli.HelpFormatter)13 SystemExitException (org.apache.openejb.cli.SystemExitException)7 Properties (java.util.Properties)6 Parser (org.apache.commons.cli.Parser)6 List (java.util.List)5 Option (org.apache.commons.cli.Option)5 Path (org.apache.hadoop.fs.Path)4 EOFException (java.io.EOFException)3 InputStream (java.io.InputStream)3 InitialContext (javax.naming.InitialContext)3 NamingException (javax.naming.NamingException)3 ServiceUnavailableException (javax.naming.ServiceUnavailableException)3 Configuration (org.apache.hadoop.conf.Configuration)3