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));
}
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;
}
Aggregations