下面列出了org.apache.commons.cli.GnuParser#parse ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void _configure(String[] args) {
GnuParser parser = new GnuParser();
CommandLine cmd;
try {
cmd = parser.parse(new MetricsOptions(), args);
} catch (ParseException e) {
log.warn("Could not parse command line options. Defaults from metrics.xml will be used.", e);
return;
}
Configuration conf = getConf();
URL metricsConfig = MetricsIngester.class.getClassLoader().getResource("metrics.xml");
if (metricsConfig != null) {
conf.addResource(metricsConfig);
}
for (Option opt : cmd.getOptions()) {
conf.set(MetricsConfig.MTX + opt.getOpt(), opt.getValue());
}
}
protected CommandLine parseArgs(CliContext context, String[] stringArgs) {
GnuParser parser = new GnuParser();
CommandLine args;
Options options = getOptions();
Io io = context.getIo();
try {
args = parser.parse(options, stringArgs);
} catch (ParseException e) {
logger.warn("Invalid command line arguments.", e);
io.errorln("Invalid command line arguments.");
io.errorln(e.getMessage());
return null;
}
if (args.hasOption("h")) {
printHelp(context);
return null;
}
return args;
}
private void processSQS(String [] args) throws Exception {
GnuParser parser = new GnuParser();
Options options = new Options();
CommandLine cmdline = parser.parse(options, args);
SQS_CMD cmd = SQS_CMD.valueOf(cmdline.getArgs()[0].toUpperCase());
AlertJanitor janitor = new AlertJanitor();
janitor.initalize(PathUtil.S3N, new Configuration());
String queue = janitor.resolveQueueUrl(cmdline.getArgs()[1]);
switch(cmd) {
case LOG:
janitor.writeLogs(queue, new Path(cmdline.getArgs()[2]));
break;
case PURGE:
janitor.clearAll(queue);
break;
default:
usage();
}
}
private void parseCommandLineAndGetRunJobParameters(String[] args)
throws ParseException, IOException, YarnException {
try {
GnuParser parser = new GnuParser();
CommandLine cli = parser.parse(options, args);
parametersHolder = createParametersHolder(cli);
parametersHolder.updateParameters(clientContext);
} catch (ParseException e) {
LOG.error("Exception in parse: {}", e.getMessage());
printUsages();
throw e;
}
}
private void parseCommandLineAndGetShowJobParameters(String[] args)
throws IOException, YarnException {
// Do parsing
GnuParser parser = new GnuParser();
CommandLine cli;
try {
cli = parser.parse(options, args);
parametersHolder = ParametersHolder
.createWithCmdLine(cli, Command.SHOW_JOB);
parametersHolder.updateParameters(clientContext);
} catch (ParseException e) {
printUsages();
}
}
private static Map<String, Object> parseOptions(String[] args) {
final GnuParser parser = new GnuParser();
final Map<String, Object> options = new HashMap<String, Object>();
try {
final long now = System.currentTimeMillis();
CommandLine line = parser.parse(cliOptions, args);
options.put(SRC, line.getOptionValue(SRC));
options.put(DST, line.getOptionValue(DST));
// default range is one year ago until now.
options.put(FROM, line.hasOption(FROM) ? parseDateTime(line.getOptionValue(FROM)) : now-(365L*24L*60L*60L*1000L));
options.put(TO, line.hasOption(TO) ? parseDateTime(line.getOptionValue(TO)) : now);
options.put(LIMIT, line.hasOption(LIMIT) ? Integer.parseInt(line.getOptionValue(LIMIT)) : Integer.MAX_VALUE);
options.put(SKIP, line.hasOption(SKIP) ? Integer.parseInt(line.getOptionValue(SKIP)) : 0);
options.put(BATCH_SIZE, line.hasOption(BATCH_SIZE) ? Integer.parseInt(line.getOptionValue(BATCH_SIZE)) : 100);
// create a mapping of all cf names -> cf.
// then determine which column family to process.
Map<String, ColumnFamily<Locator, Long>> nameToCf = new HashMap<String, ColumnFamily<Locator, Long>>() {{
for (CassandraModel.MetricColumnFamily cf : CassandraModel.getMetricColumnFamilies()) {
put(cf.getName(), cf);
}
}};
if (nameToCf.get(line.getOptionValue(COLUMN_FAMILY)) == null) {
throw new ParseException("Invalid column family");
}
CassandraModel.MetricColumnFamily columnFamily = (CassandraModel.MetricColumnFamily)nameToCf.get(line.getOptionValue(COLUMN_FAMILY));
options.put(COLUMN_FAMILY, columnFamily);
options.put(TTL, line.hasOption(TTL) ? Integer.parseInt(line.getOptionValue(TTL)) : (int)(5 * columnFamily.getDefaultTTL().toSeconds()));
options.put(READ_THREADS, line.hasOption(READ_THREADS) ? Integer.parseInt(line.getOptionValue(READ_THREADS)) : 1);
options.put(WRITE_THREADS, line.hasOption(WRITE_THREADS) ? Integer.parseInt(line.getOptionValue(WRITE_THREADS)) : 1);
options.put(VERIFY, line.hasOption(VERIFY));
options.put(DISCOVER, line.hasOption(DISCOVER) ? NodeDiscoveryType.RING_DESCRIBE : NodeDiscoveryType.NONE);
options.put(RATE, line.hasOption(RATE) ? Integer.parseInt(line.getOptionValue(RATE)) : 500);
} catch (ParseException ex) {
HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter.printHelp("bf-migrate", cliOptions);
System.exit(-1);
}
return options;
}