Search in sources :

Example 1 with CommandException

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);
}
Also used : Options(org.apache.commons.cli.Options) Builder(org.apache.commons.cli.Option.Builder) CommandExecutor(com.terran4j.commons.jfinger.CommandExecutor) CommandException(com.terran4j.commons.jfinger.CommandException) CommandOptionDefine(com.terran4j.commons.jfinger.CommandOptionDefine) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) ParseException(org.apache.commons.cli.ParseException) OptionType(com.terran4j.commons.jfinger.OptionType) CommandInterpreter(com.terran4j.commons.jfinger.CommandInterpreter)

Example 2 with CommandException

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;
}
Also used : CommandDefine(com.terran4j.commons.jfinger.CommandDefine) CommandException(com.terran4j.commons.jfinger.CommandException) CommandGroupDefine(com.terran4j.commons.jfinger.CommandGroupDefine)

Aggregations

CommandException (com.terran4j.commons.jfinger.CommandException)2 CommandDefine (com.terran4j.commons.jfinger.CommandDefine)1 CommandExecutor (com.terran4j.commons.jfinger.CommandExecutor)1 CommandGroupDefine (com.terran4j.commons.jfinger.CommandGroupDefine)1 CommandInterpreter (com.terran4j.commons.jfinger.CommandInterpreter)1 CommandOptionDefine (com.terran4j.commons.jfinger.CommandOptionDefine)1 OptionType (com.terran4j.commons.jfinger.OptionType)1 CommandLine (org.apache.commons.cli.CommandLine)1 Option (org.apache.commons.cli.Option)1 Builder (org.apache.commons.cli.Option.Builder)1 Options (org.apache.commons.cli.Options)1 ParseException (org.apache.commons.cli.ParseException)1 UnrecognizedOptionException (org.apache.commons.cli.UnrecognizedOptionException)1