use of org.apache.karaf.shell.api.action.Command in project karaf by apache.
the class Execute method listCommands.
private static void listCommands() {
System.out.println("Available commands:");
for (Map.Entry<String, Class<?>> entry : COMMANDS.entrySet()) {
Command ann = entry.getValue().getAnnotation(Command.class);
System.out.printf(" %s - %s\n", entry.getKey(), ann.description());
}
System.out.println("Type 'command --help' for more help on the specified command.");
}
use of org.apache.karaf.shell.api.action.Command in project karaf by apache.
the class GenerateHelpMojo method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
try {
if (!FORMAT_DOCBX.equals(format) && !FORMAT_CONF.equals(format) && !FORMAT_ASCIIDOC.equals(format)) {
throw new MojoFailureException("Unsupported format: " + format + ". Supported formats are: asciidoc, docbx, or conf.");
}
if (!targetFolder.exists()) {
targetFolder.mkdirs();
}
ClassFinder finder = createFinder(classLoader);
List<Class<?>> classes = finder.findAnnotatedClasses(Command.class);
if (classes.isEmpty()) {
throw new MojoFailureException("No command found");
}
CommandHelpPrinter helpPrinter = null;
if (FORMAT_ASCIIDOC.equals(format)) {
helpPrinter = new AsciiDoctorCommandHelpPrinter();
}
if (FORMAT_CONF.equals(format)) {
helpPrinter = new UserConfCommandHelpPrinter();
}
if (FORMAT_DOCBX.equals(format)) {
helpPrinter = new DocBookCommandHelpPrinter();
}
Map<String, Set<String>> commands = new TreeMap<>();
String commandSuffix = null;
if (FORMAT_ASCIIDOC.equals(format)) {
commandSuffix = "adoc";
}
if (FORMAT_CONF.equals(format)) {
commandSuffix = "conf";
}
if (FORMAT_DOCBX.equals(format)) {
commandSuffix = "xml";
}
for (Class<?> clazz : classes) {
try {
Action action = (Action) clazz.newInstance();
Command cmd = clazz.getAnnotation(Command.class);
// skip the *-help command
if (cmd.scope().equals("*"))
continue;
File output = new File(targetFolder, cmd.scope() + "-" + cmd.name() + "." + commandSuffix);
FileOutputStream outStream = new FileOutputStream(output);
PrintStream out = new PrintStream(outStream);
helpPrinter.printHelp(action, out, includeHelpOption);
out.close();
outStream.close();
commands.computeIfAbsent(cmd.scope(), k -> new TreeSet<>()).add(cmd.name());
getLog().info("Found command: " + cmd.scope() + ":" + cmd.name());
} catch (Exception e) {
getLog().warn("Unable to write help for " + clazz.getName(), e);
}
}
String overViewSuffix = null;
if (FORMAT_ASCIIDOC.equals(format)) {
overViewSuffix = "adoc";
}
if (FORMAT_CONF.equals(format)) {
overViewSuffix = "conf";
}
if (FORMAT_DOCBX.equals(format)) {
overViewSuffix = "xml";
}
PrintStream writer = new PrintStream(new FileOutputStream(new File(targetFolder, "commands." + overViewSuffix)));
helpPrinter.printOverview(commands, writer);
writer.close();
} catch (Exception e) {
throw new MojoExecutionException("Error building commands help", e);
}
}
use of org.apache.karaf.shell.api.action.Command in project karaf by apache.
the class UserConfCommandHelpPrinter method printHelp.
@Override
public void printHelp(Action action, PrintStream out, boolean includeHelpOption) {
Command command = action.getClass().getAnnotation(Command.class);
Set<Option> options = new HashSet<>();
List<Argument> arguments = new ArrayList<>();
Map<Argument, Field> argFields = new HashMap<>();
Map<Option, Field> optFields = new HashMap<>();
for (Class<?> type = action.getClass(); type != null; type = type.getSuperclass()) {
for (Field field : type.getDeclaredFields()) {
Option option = field.getAnnotation(Option.class);
if (option != null) {
options.add(option);
}
Argument argument = field.getAnnotation(Argument.class);
if (argument != null) {
argument = replaceDefaultArgument(field, argument);
argFields.put(argument, field);
int index = argument.index();
while (arguments.size() <= index) {
arguments.add(null);
}
if (arguments.get(index) != null) {
throw new IllegalArgumentException("Duplicate argument index: " + index + " on Action " + action.getClass().getName());
}
arguments.set(index, argument);
}
}
}
if (includeHelpOption)
options.add(HelpOption.HELP);
out.println("h1. " + command.scope() + ":" + command.name());
out.println();
out.println("h2. Description");
out.println(command.description());
out.println();
StringBuffer syntax = new StringBuffer();
syntax.append(String.format("%s:%s", command.scope(), command.name()));
if (options.size() > 0) {
syntax.append(" \\[options\\]");
}
if (arguments.size() > 0) {
syntax.append(' ');
for (Argument argument : arguments) {
syntax.append(String.format(argument.required() ? "%s " : "\\[%s\\] ", argument.name()));
}
}
out.println("h2. Syntax");
out.println(syntax.toString());
out.println();
if (arguments.size() > 0) {
out.println("h2. Arguments");
out.println("|| Name || Description ||");
for (Argument argument : arguments) {
String description = argument.description();
if (!argument.required()) {
Object o = getDefaultValue(action, argFields.get(argument));
String defaultValue = getDefaultValueString(o);
if (defaultValue != null) {
description += " (defaults to " + o.toString() + ")";
}
}
out.println("| " + argument.name() + " | " + description + " |");
}
out.println();
}
if (options.size() > 0) {
out.println("h2. Options");
out.println("|| Name || Description ||");
for (Option option : options) {
String opt = option.name();
String desc = option.description();
for (String alias : option.aliases()) {
opt += ", " + alias;
}
Object o = getDefaultValue(action, optFields.get(option));
String defaultValue = getDefaultValueString(o);
if (defaultValue != null) {
desc += " (defaults to " + defaultValue + ")";
}
out.println("| " + opt + " | " + desc + " |");
}
out.println();
}
if (command.detailedDescription().length() > 0) {
out.println("h2. Details");
out.println(command.detailedDescription());
}
out.println();
}
use of org.apache.karaf.shell.api.action.Command in project karaf by apache.
the class DefaultActionPreparator method printUsage.
public void printUsage(Action action, Map<Option, Field> options, Map<Argument, Field> arguments, PrintStream out, boolean globalScope, int termWidth) {
Command command = action.getClass().getAnnotation(Command.class);
if (command != null) {
List<Argument> argumentsSet = new ArrayList<Argument>(arguments.keySet());
argumentsSet.sort(Comparator.comparing(Argument::index));
Set<Option> optionsSet = new HashSet<Option>(options.keySet());
optionsSet.add(HelpOption.HELP);
if (command != null && (command.description() != null || command.name() != null)) {
out.println(INTENSITY_BOLD + "DESCRIPTION" + INTENSITY_NORMAL);
out.print(" ");
if (command.name() != null) {
if (globalScope) {
out.println(INTENSITY_BOLD + command.name() + INTENSITY_NORMAL);
} else {
out.println(command.scope() + ":" + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL);
}
out.println();
}
out.print("\t");
out.println(command.description());
out.println();
}
StringBuffer syntax = new StringBuffer();
if (command != null) {
if (globalScope) {
syntax.append(command.name());
} else {
syntax.append(String.format("%s:%s", command.scope(), command.name()));
}
}
if (options.size() > 0) {
syntax.append(" [options]");
}
if (arguments.size() > 0) {
syntax.append(' ');
for (Argument argument : argumentsSet) {
if (!argument.required()) {
syntax.append(String.format("[%s] ", argument.name()));
} else {
syntax.append(String.format("%s ", argument.name()));
}
}
}
out.println(INTENSITY_BOLD + "SYNTAX" + INTENSITY_NORMAL);
out.print(" ");
out.println(syntax.toString());
out.println();
if (arguments.size() > 0) {
out.println(INTENSITY_BOLD + "ARGUMENTS" + INTENSITY_NORMAL);
for (Argument argument : argumentsSet) {
out.print(" ");
out.println(INTENSITY_BOLD + argument.name() + INTENSITY_NORMAL);
printFormatted(" ", argument.description(), termWidth, out, true);
if (!argument.required()) {
if (argument.valueToShowInHelp() != null && argument.valueToShowInHelp().length() != 0) {
if (Argument.DEFAULT_STRING.equals(argument.valueToShowInHelp())) {
Object o = getDefaultValue(action, arguments.get(argument));
String defaultValue = getDefaultValueString(o);
if (defaultValue != null) {
printDefaultsTo(out, defaultValue);
}
} else {
printDefaultsTo(out, argument.valueToShowInHelp());
}
}
}
}
out.println();
}
if (options.size() > 0) {
out.println(INTENSITY_BOLD + "OPTIONS" + INTENSITY_NORMAL);
for (Option option : optionsSet) {
String opt = option.name();
for (String alias : option.aliases()) {
opt += ", " + alias;
}
out.print(" ");
out.println(INTENSITY_BOLD + opt + INTENSITY_NORMAL);
printFormatted(" ", option.description(), termWidth, out, true);
if (option.valueToShowInHelp() != null && option.valueToShowInHelp().length() != 0) {
if (Option.DEFAULT_STRING.equals(option.valueToShowInHelp())) {
Object o = getDefaultValue(action, options.get(option));
String defaultValue = getDefaultValueString(o);
if (defaultValue != null) {
printDefaultsTo(out, defaultValue);
}
} else {
printDefaultsTo(out, option.valueToShowInHelp());
}
}
}
out.println();
}
if (command.detailedDescription().length() > 0) {
out.println(INTENSITY_BOLD + "DETAILS" + INTENSITY_NORMAL);
String desc = loadDescription(action.getClass(), command.detailedDescription());
printFormatted(" ", desc, termWidth, out, true);
}
}
}
Aggregations