use of com.terran4j.commons.jfinger.CommandException in project commons by terran4j.
the class CommandLineService method execute.
private void execute(String groupName, String commandName, CommandDefine command, String[] args, PrintStream out) throws BusinessException {
Options options = new Options();
List<CommandOptionDefine> optionConfigs = command.getOptions();
if (optionConfigs != null && optionConfigs.size() > 0) {
for (CommandOptionDefine optionConfig : optionConfigs) {
String key = optionConfig.getKey();
String name = optionConfig.getName();
String desc = optionConfig.getDesc();
OptionType type = optionConfig.getType();
if (type == OptionType.BOOLEAN) {
options.addOption(key, name, false, desc);
} else if (type == OptionType.PROPERTIES) {
Option option = //
Option.builder(key).argName("property=value").numberOfArgs(5).valueSeparator('=').desc(//
desc).build();
options.addOption(option);
} else {
Builder builder = Option.builder(key).hasArg();
if (!StringUtils.isEmpty(name)) {
builder = builder.longOpt(name);
}
Option option = builder.desc(desc).build();
options.addOption(option);
}
}
}
CommandLine commandLine = null;
try {
commandLine = parser.parse(options, args);
} catch (UnrecognizedOptionException e) {
String optionKey = e.getOption();
out.println("无效的命令行选项: " + optionKey);
out.println(getHelp(groupName, commandName));
return;
} catch (ParseException e) {
out.println("解析命令出错:" + e.getMessage());
out.println(getHelp(groupName, commandName));
return;
}
CommandExecutor executor = command.getExecutor();
CommandInterpreter ci = new CommandInterpreterImpl(out, commandLine, command);
if (optionConfigs != null && optionConfigs.size() > 0) {
for (CommandOptionDefine optionConfig : optionConfigs) {
if (optionConfig.isRequired() && optionConfig.getType() != OptionType.BOOLEAN) {
String key = optionConfig.getKey();
String name = optionConfig.getName();
String value = ci.getOption(key);
if (value == null) {
throw //
new CommandException(CommandErrorCode.OPTION_KEY_EMPTY.getName()).put("group", //
groupName).put("commandName", //
commandName).put("optionKey", //
key).put("optionName", //
name).setMessage("命令 [${group} ${commandName}] 需要 ${optionName} 选项," + "请在命令后面附上 -${optionKey} [${optionName}]");
}
}
}
}
executor.execute(ci);
}
use of com.terran4j.commons.jfinger.CommandException in project commons by terran4j.
the class CommandLineService method execute.
public final boolean execute(String command, PrintStream out) {
if (StringUtils.isEmpty(command)) {
return true;
}
if (log.isInfoEnabled()) {
log.info("command: {}", command);
}
command = command.trim();
if ("quit".equals(command)) {
return false;
}
// Print All Commands Help
if (command.startsWith("help ") || command.equals("help")) {
String[] helpArgs = util.split(command, " ");
if (helpArgs.length > 2) {
out.println(getHelp(helpArgs[1], helpArgs[2]));
return true;
}
if (helpArgs.length > 1) {
out.println(getHelp(helpArgs[1]));
return true;
}
if (helpArgs.length > 0) {
out.println(getHelp());
return true;
}
}
try {
String[] args = parseCommand(command);
if (args.length < 2) {
out.println(getHelpPrompt());
out.println("命令格式不正确,至少要输入: [命令组名称] [命令名称] ,请用上面的 help 命令查询其详细用法");
return true;
}
String group = args[0];
CommandGroupDefine commandGroup = commands.get(group);
if (commandGroup == null) {
out.println(getHelpPrompt());
out.println("不存在此命令组: " + group + ",请用上面的 help 命令查询其详细用法。\n");
return true;
}
if (commandGroup.size() == 0) {
out.println("命令组【 " + group + "】中没有任何命令!\n");
return true;
}
String commandName = args[1];
CommandDefine commandConfig = commandGroup.getCommand(commandName);
if (commandConfig == null) {
out.println("在命令组【" + group + "】中,不存在此命令: " + commandName);
out.println(getHelp(group));
return true;
}
String[] commandArgs = Arrays.copyOfRange(args, 2, args.length);
execute(group, commandName, commandConfig, commandArgs, out);
} catch (CommandException ce) {
String msg = ce.getMessage();
out.println(msg);
} catch (Throwable e) {
printHelpPrompt(e, out);
}
return true;
}
Aggregations