Search in sources :

Example 1 with CommandHandler

use of com.bluenimble.platform.cli.command.CommandHandler in project serverless by bluenimble.

the class PrefixedCommand method execute.

@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
    String cmd = (String) tool.currentContext().get(ToolContext.CommandLine);
    if (Lang.isNullOrEmpty(cmd)) {
        throw new CommandExecutionException("Command '" + name + "' missing arguments").showUsage();
    }
    String subject = cmd;
    int indexOfSapce = cmd.indexOf(Lang.SPACE);
    if (indexOfSapce > 0) {
        subject = cmd.substring(0, indexOfSapce).trim();
        cmd = cmd.substring(indexOfSapce + 1);
    } else {
        cmd = null;
    }
    CommandHandler handler = handlers.get(subject);
    if (handler == null) {
        throw new CommandExecutionException("'" + getName() + " " + subject + "' not found");
    }
    // list of commands
    String[] commands = Lang.split(cmd, Lang.SEMICOLON);
    if (commands != null && commands.length > 1) {
        for (String c : commands) {
            c = c.trim();
            CommandResult result = handler.execute(tool, args(handler.getArgs(), c));
            try {
                if (result != null && result.getContent() != null) {
                    if (result.getType() == CommandResult.OK) {
                        tool.printer().content(null, String.valueOf(result.getContent()));
                    } else {
                        tool.printer().error(String.valueOf(result.getContent()));
                    }
                }
            } catch (Exception ex) {
            // Ignore
            }
        }
        return null;
    }
    return handler.execute(tool, args(handler.getArgs(), cmd));
}
Also used : CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) CommandHandler(com.bluenimble.platform.cli.command.CommandHandler) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) CommandResult(com.bluenimble.platform.cli.command.CommandResult)

Example 2 with CommandHandler

use of com.bluenimble.platform.cli.command.CommandHandler in project serverless by bluenimble.

the class AbstractCommand method describe.

@Override
public String describe() {
    if (formatedDesc != null) {
        return formatedDesc;
    }
    boolean prefixed = PrefixedCommand.class.isAssignableFrom(this.getClass());
    int rem = 30 - name.length();
    StringBuilder sb = new StringBuilder("[").append(name.toLowerCase()).append("]");
    if (!Lang.isNullOrEmpty(synonym)) {
        sb.append(" ").append(" or ").append("[").append(synonym.toLowerCase()).append("]");
        rem = rem - synonym.length();
    }
    if (!prefixed) {
        for (int i = 0; i < rem; i++) {
            sb.append(" ");
        }
        sb.append(": ");
        sb.append(description);
    }
    // add options
    if (options != null && !options.isEmpty()) {
        sb.append("\n");
        Iterator<CommandOption> optionsIter = options.values().iterator();
        while (optionsIter.hasNext()) {
            CommandOption option = optionsIter.next();
            String acceptsArg = "No Arg";
            if (option.acceptsArgs() == CommandOption.ONE_ARG) {
                acceptsArg = "One Arg";
            } else if (option.acceptsArgs() == CommandOption.MULTI_ARG) {
                acceptsArg = "Multi Args";
            }
            String required = "Required";
            if (!option.isRequired()) {
                required = "Optional";
            }
            sb.append("        ").append("-").append(option.name()).append(" ");
            if (option.label() != null) {
                sb.append("'").append(option.label()).append("'");
            }
            sb.append(" ( ").append(required).append(", ").append(acceptsArg);
            if (option.cast() != null) {
                sb.append(", ").append(option.cast());
            }
            sb.append(" )").append("\n");
        }
    }
    if (PrefixedCommand.class.isAssignableFrom(this.getClass())) {
        PrefixedCommand pCommand = (PrefixedCommand) this;
        Map<String, CommandHandler> handlers = pCommand.handlers();
        if (handlers != null && !handlers.isEmpty()) {
            sb.append(Lang.ENDLN).append(Lang.ENDLN);
            for (CommandHandler ch : handlers.values()) {
                String chName = S2 + Lang.APOS + name + Lang.SPACE + ch.getName() + Lang.APOS;
                rem = 32 - chName.length();
                sb.append(chName);
                for (int i = 0; i < rem; i++) {
                    sb.append(" ");
                }
                sb.append(Lang.COLON).append(Lang.SPACE).append(ch.getDescription()).append(Lang.ENDLN).append(Lang.ENDLN);
            }
        }
    }
    formatedDesc = sb.toString();
    sb.setLength(0);
    return formatedDesc;
}
Also used : CommandOption(com.bluenimble.platform.cli.command.CommandOption) CommandHandler(com.bluenimble.platform.cli.command.CommandHandler)

Aggregations

CommandHandler (com.bluenimble.platform.cli.command.CommandHandler)2 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)1 CommandOption (com.bluenimble.platform.cli.command.CommandOption)1 CommandResult (com.bluenimble.platform.cli.command.CommandResult)1