本文整理匯總了Java中org.apache.commons.cli.CommandLine.getOptions方法的典型用法代碼示例。如果您正苦於以下問題:Java CommandLine.getOptions方法的具體用法?Java CommandLine.getOptions怎麽用?Java CommandLine.getOptions使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.cli.CommandLine
的用法示例。
在下文中一共展示了CommandLine.getOptions方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: commandLine2Properties
import org.apache.commons.cli.CommandLine; //導入方法依賴的package包/類
public static Properties commandLine2Properties(final CommandLine commandLine) {
Properties properties = new Properties();
Option[] opts = commandLine.getOptions();
if (opts != null) {
for (Option opt : opts) {
String name = opt.getLongOpt();
String value = commandLine.getOptionValue(name);
if (value != null) {
properties.setProperty(name, value);
}
}
}
return properties;
}
示例2: parse
import org.apache.commons.cli.CommandLine; //導入方法依賴的package包/類
public THashMap<String, String> parse(String[] input) {
THashMap<String, String> result = new THashMap<>();
try {
CommandLine cmdLine = this.parse(this.getOptions(), input);
for (Option option : cmdLine.getOptions()) {
result.put(option.getOpt(), option.getValue());
}
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
示例3: failover
import org.apache.commons.cli.CommandLine; //導入方法依賴的package包/類
private int failover(CommandLine cmd)
throws IOException, ServiceFailedException {
boolean forceFence = cmd.hasOption(FORCEFENCE);
boolean forceActive = cmd.hasOption(FORCEACTIVE);
int numOpts = cmd.getOptions() == null ? 0 : cmd.getOptions().length;
final String[] args = cmd.getArgs();
if (numOpts > 3 || args.length != 2) {
errOut.println("failover: incorrect arguments");
printUsage(errOut, "-failover");
return -1;
}
HAServiceTarget fromNode = resolveTarget(args[0]);
HAServiceTarget toNode = resolveTarget(args[1]);
// Check that auto-failover is consistently configured for both nodes.
Preconditions.checkState(
fromNode.isAutoFailoverEnabled() ==
toNode.isAutoFailoverEnabled(),
"Inconsistent auto-failover configs between %s and %s!",
fromNode, toNode);
if (fromNode.isAutoFailoverEnabled()) {
if (forceFence || forceActive) {
// -forceActive doesn't make sense with auto-HA, since, if the node
// is not healthy, then its ZKFC will immediately quit the election
// again the next time a health check runs.
//
// -forceFence doesn't seem to have any real use cases with auto-HA
// so it isn't implemented.
errOut.println(FORCEFENCE + " and " + FORCEACTIVE + " flags not " +
"supported with auto-failover enabled.");
return -1;
}
try {
return gracefulFailoverThroughZKFCs(toNode);
} catch (UnsupportedOperationException e){
errOut.println("Failover command is not supported with " +
"auto-failover enabled: " + e.getLocalizedMessage());
return -1;
}
}
FailoverController fc = new FailoverController(getConf(),
requestSource);
try {
fc.failover(fromNode, toNode, forceFence, forceActive);
out.println("Failover from "+args[0]+" to "+args[1]+" successful");
} catch (FailoverFailedException ffe) {
errOut.println("Failover failed: " + ffe.getLocalizedMessage());
return -1;
}
return 0;
}
示例4: validate
import org.apache.commons.cli.CommandLine; //導入方法依賴的package包/類
public boolean validate(final CommandLine input) {
for(Option o : input.getOptions()) {
if(Option.UNINITIALIZED == o.getArgs()) {
continue;
}
if(o.hasOptionalArg()) {
continue;
}
if(o.getArgs() != o.getValuesList().size()) {
console.printf("Missing argument for option %s%n", o.getLongOpt());
return false;
}
}
final TerminalAction action = TerminalActionFinder.get(input);
if(null == action) {
console.printf("%s%n", "Missing argument");
return false;
}
if(input.hasOption(TerminalOptionsBuilder.Params.existing.name())) {
final String arg = input.getOptionValue(TerminalOptionsBuilder.Params.existing.name());
if(null == TransferAction.forName(arg)) {
final Set<TransferAction> actions = new HashSet<TransferAction>(TransferAction.forTransfer(Transfer.Type.download));
actions.add(TransferAction.cancel);
console.printf("Invalid argument '%s' for option %s. Must be one of %s%n",
arg, TerminalOptionsBuilder.Params.existing.name(), Arrays.toString(actions.toArray()));
return false;
}
switch(action) {
case download:
if(!validate(arg, Transfer.Type.download)) {
return false;
}
break;
case upload:
if(!validate(arg, Transfer.Type.upload)) {
return false;
}
break;
case synchronize:
if(!validate(arg, Transfer.Type.sync)) {
return false;
}
break;
case copy:
if(!validate(arg, Transfer.Type.copy)) {
return false;
}
break;
}
}
// Validate arguments
switch(action) {
case list:
case download:
if(!validate(input.getOptionValue(action.name()))) {
return false;
}
break;
case upload:
case copy:
case synchronize:
if(!validate(input.getOptionValue(action.name()))) {
return false;
}
break;
}
return true;
}
示例5: main
import org.apache.commons.cli.CommandLine; //導入方法依賴的package包/類
/**
* See class {@code Example}
* @param args
* @throws MalformedURLException
*/
public static void main( String[] args ) throws MalformedURLException, IOException {
CommandLineParser clParser = new DefaultParser();
Options opts = new Options();
opts.addOption("h", "help", false, "display help");
opts.addOption("m", "mzml", true, "mzml file");
Option indexOpts = Option.builder("i")
.hasArgs()
.desc("indices of spectra to report")
.argName("index")
.build();
opts.addOption(indexOpts);
CommandLine cmd = null;
HelpFormatter formatter = new HelpFormatter();
try {
cmd = clParser.parse(opts, args);
if( cmd.hasOption("help") || (cmd.getOptions().length == 0)) {
formatter.printHelp("help", opts);
System.exit(-1);
}
} catch (ParseException e) {
e.printStackTrace();
}
Path mzml = cmd.hasOption("m") ? Paths.get(cmd.getOptionValue("m")) : null;
if(mzml == null ) {
System.err.println("MZML file was not specified");
System.exit(-1);
}
List<Integer> indices = cmd.hasOption("i")
? Stream.of(cmd.getOptionValues("i")).map(s -> Integer.parseInt(s)).collect(Collectors.toList())
: Collections.EMPTY_LIST;
// Creates a parser producing Spectrum instances and uses XMLSpectrumBuilder via method referencing
MzMLStAXParser<Spectrum> parser = new MzMLStAXParser<Spectrum>(mzml, XMLSpectrumBuilder::new);
if(indices.size() == 0) {
// if no indices supplied, sequentially iterate over the file
int maxCount = 10;
for(Spectrum spectrum : parser) {
if(maxCount == 0)
return;
System.out.println(spectrum.toString());
maxCount--;
}
} else {
// if indices are supplied, access spectra by jumping to the position in file
for(int i : indices){
System.out.println(parser.getSpectrumByIndex(i).toString());
}
}
parser.close();
}