Search in sources :

Example 11 with CommandOption

use of com.bluenimble.platform.cli.command.CommandOption 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)

Example 12 with CommandOption

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

the class ChangeContextCommand method execute.

@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
    CommandOption co = options.get(CommandOption.CMD_LINE);
    if (co == null || co.getArg(0) == null) {
        return new DefaultCommandResult(CommandResult.KO, "Specify a valid context name");
    }
    String contextName = (String) co.getArg(0);
    contextName = contextName.toLowerCase();
    ToolContext ctx = tool.getContext(contextName);
    if (ctx == null) {
        return new DefaultCommandResult(CommandResult.KO, "Context [" + contextName + "] not registered with this tool");
    }
    tool.changeContext(ctx);
    return new DefaultCommandResult(CommandResult.OK, "Context changed to [" + contextName + "]");
}
Also used : CommandOption(com.bluenimble.platform.cli.command.CommandOption) ToolContext(com.bluenimble.platform.cli.ToolContext)

Example 13 with CommandOption

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

the class DelimeterCommand method execute.

@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
    CommandOption co = options.get(CommandOption.CMD_LINE);
    if (co == null || co.getArg(0) == null) {
        return new DefaultCommandResult(CommandResult.KO, "Specify a delimiter");
    }
    String delimiter = (String) co.getArg(0);
    String oldDelimiter = tool.currentContext().getDelimiter();
    if ("\n".equals(oldDelimiter)) {
        oldDelimiter = "\\n";
    }
    String realDelimiter = delimiter;
    realDelimiter = Lang.replace(realDelimiter, "\\n", "\n");
    tool.currentContext().setDelimiter(realDelimiter);
    return new DefaultCommandResult(CommandResult.OK, "Delimiter changed with success for context [" + tool.currentContext().getName() + ", (" + delimiter + ")]");
}
Also used : CommandOption(com.bluenimble.platform.cli.command.CommandOption)

Aggregations

CommandOption (com.bluenimble.platform.cli.command.CommandOption)13 Map (java.util.Map)6 Tool (com.bluenimble.platform.cli.Tool)4 AbstractCommand (com.bluenimble.platform.cli.command.impls.AbstractCommand)4 CommandParser (com.bluenimble.platform.cli.command.parser.CommandParser)4 CommandParserImpl (com.bluenimble.platform.cli.command.parser.impls.CommandParserImpl)4 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)3 File (java.io.File)3 Command (com.bluenimble.platform.cli.command.Command)2 CommandResult (com.bluenimble.platform.cli.command.CommandResult)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 InstallI18nException (com.bluenimble.platform.cli.InstallI18nException)1 ToolContext (com.bluenimble.platform.cli.ToolContext)1 ToolStartupException (com.bluenimble.platform.cli.ToolStartupException)1 CommandHandler (com.bluenimble.platform.cli.command.CommandHandler)1 CommandOptionImpl (com.bluenimble.platform.cli.command.impls.CommandOptionImpl)1 CommandParsingError (com.bluenimble.platform.cli.command.parser.CommandParsingError)1 FriendlyJsonEmitter (com.bluenimble.platform.cli.printing.impls.FriendlyJsonEmitter)1