java commandline 使用_Java CommandLine.getOptions方法代码示例

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 actions = new HashSet(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;

}


版权声明:本文为weixin_42170064原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。