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