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