Search in sources :

Example 6 with Argument

use of org.apache.karaf.shell.api.action.Argument 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();
}
Also used : Argument(org.apache.karaf.shell.api.action.Argument) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) Command(org.apache.karaf.shell.api.action.Command) HelpOption(org.apache.karaf.shell.impl.action.command.HelpOption) Option(org.apache.karaf.shell.api.action.Option) HashSet(java.util.HashSet)

Example 7 with Argument

use of org.apache.karaf.shell.api.action.Argument 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);
        }
    }
}
Also used : Argument(org.apache.karaf.shell.api.action.Argument) Command(org.apache.karaf.shell.api.action.Command) ArrayList(java.util.ArrayList) Option(org.apache.karaf.shell.api.action.Option) HashSet(java.util.HashSet)

Aggregations

Argument (org.apache.karaf.shell.api.action.Argument)7 Command (org.apache.karaf.shell.api.action.Command)5 Option (org.apache.karaf.shell.api.action.Option)5 Field (java.lang.reflect.Field)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 HelpOption (org.apache.karaf.shell.impl.action.command.HelpOption)3 Annotation (java.lang.annotation.Annotation)2 IOException (java.io.IOException)1 List (java.util.List)1 Map (java.util.Map)1 Token (org.apache.felix.gogo.runtime.Token)1 CommandException (org.apache.karaf.shell.support.CommandException)1 GenericType (org.apache.karaf.shell.support.converter.GenericType)1