Package org.apache.felix.gogo.commands

Examples of org.apache.felix.gogo.commands.Command


                        option = opt;
                        break;
                    }
                }
                if (option == null) {
                    Command command = action.getClass().getAnnotation(Command.class);
                    throw new CommandException(
                            Ansi.ansi()
                                    .fg(Ansi.Color.RED)
                                    .a("Error executing command ")
                                    .a(command.scope())
                                    .a(":")
                                    .a(Ansi.Attribute.INTENSITY_BOLD)
                                    .a(command.name())
                                    .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                    .a(" undefined option ")
                                    .a(Ansi.Attribute.INTENSITY_BOLD)
                                    .a(param)
                                    .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                    .fg(Ansi.Color.DEFAULT)
                                    .toString(),
                            "Undefined option: " + param
                    );
                }
                Field field = options.get(option);
                if (value == null && (field.getType() == boolean.class || field.getType() == Boolean.class)) {
                    value = Boolean.TRUE;
                }
                if (value == null && it.hasNext()) {
                    value = it.next();
                }
                if (value == null) {
                    Command command = action.getClass().getAnnotation(Command.class);
                    throw new CommandException(
                            Ansi.ansi()
                                    .fg(Ansi.Color.RED)
                                    .a("Error executing command ")
                                    .a(command.scope())
                                    .a(":")
                                    .a(Ansi.Attribute.INTENSITY_BOLD)
                                    .a(command.name())
                                    .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                    .a(" missing value for option ")
                                    .a(Ansi.Attribute.INTENSITY_BOLD)
                                    .a(param)
                                    .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                    .fg(Ansi.Color.DEFAULT)
                                    .toString(),
                            "Missing value for option: " + param
                    );
                }
                if (option.multiValued()) {
                    List<Object> l = (List<Object>) optionValues.get(option);
                    if (l == null) {
                        l = new ArrayList<Object>();
                        optionValues.put(option,  l);
                    }
                    l.add(value);
                } else {
                    optionValues.put(option, value);
                }
            } else {
                processOptions = false;
                if (argIndex >= orderedArguments.size()) {
                    Command command = action.getClass().getAnnotation(Command.class);
                    throw new CommandException(
                            Ansi.ansi()
                                    .fg(Ansi.Color.RED)
                                    .a("Error executing command ")
                                    .a(command.scope())
                                    .a(":")
                                    .a(Ansi.Attribute.INTENSITY_BOLD)
                                    .a(command.name())
                                    .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                    .a(": too many arguments specified")
                                    .fg(Ansi.Color.DEFAULT)
                                    .toString(),
                            "Too many arguments specified"
                    );
                }
                Argument argument = orderedArguments.get(argIndex);
                if (!argument.multiValued()) {
                    argIndex++;
                }
                if (argument.multiValued()) {
                    List<Object> l = (List<Object>) argumentValues.get(argument);
                    if (l == null) {
                        l = new ArrayList<Object>();
                        argumentValues.put(argument, l);
                    }
                    l.add(param);
                } else {
                    argumentValues.put(argument, param);
                }
            }
        }
        // Check required arguments / options
        for (Option option : options.keySet()) {
            if (option.required() && optionValues.get(option) == null) {
                Command command = action.getClass().getAnnotation(Command.class);
                throw new CommandException(
                        Ansi.ansi()
                                .fg(Ansi.Color.RED)
                                .a("Error executing command ")
                                .a(command.scope())
                                .a(":")
                                .a(Ansi.Attribute.INTENSITY_BOLD)
                                .a(command.name())
                                .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                .a(": option ")
                                .a(Ansi.Attribute.INTENSITY_BOLD)
                                .a(option.name())
                                .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                .a(" is required")
                                .fg(Ansi.Color.DEFAULT)
                                .toString(),
                        "Option " + option.name() + " is required"
                );
            }
        }
        for (Argument argument : arguments.keySet()) {
            if (argument.required() && argumentValues.get(argument) == null) {
                Command command = action.getClass().getAnnotation(Command.class);
                throw new CommandException(
                        Ansi.ansi()
                                .fg(Ansi.Color.RED)
                                .a("Error executing command ")
                                .a(command.scope())
                                .a(":")
                                .a(Ansi.Attribute.INTENSITY_BOLD)
                                .a(command.name())
                                .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                .a(": argument ")
                                .a(Ansi.Attribute.INTENSITY_BOLD)
                                .a(argument.name())
                                .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                .a(" is required")
                                .fg(Ansi.Color.DEFAULT)
                                .toString(),
                        "Argument " + argument.name() + " is required"
                );
            }
        }
        // Convert and inject values
        for (Map.Entry<Option, Object> entry : optionValues.entrySet()) {
            Field field = options.get(entry.getKey());
            Object value;
            try {
                value = convert(action, session, entry.getValue(), field.getGenericType());
            } catch (Exception e) {
                Command command = action.getClass().getAnnotation(Command.class);
                throw new CommandException(
                        Ansi.ansi()
                                .fg(Ansi.Color.RED)
                                .a("Error executing command ")
                                .a(command.scope())
                                .a(":")
                                .a(Ansi.Attribute.INTENSITY_BOLD)
                                .a(command.name())
                                .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                .a(": unable to convert option ")
                                .a(Ansi.Attribute.INTENSITY_BOLD)
                                .a(entry.getKey().name())
                                .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                .a(" with value '")
                                .a(entry.getValue())
                                .a("' to type ")
                                .a(new GenericType(field.getGenericType()).toString())
                                .fg(Ansi.Color.DEFAULT)
                                .toString(),
                        "Unable to convert option " + entry.getKey().name() + " with value '"
                                + entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(),
                        e
                );
            }
            field.setAccessible(true);
            field.set(action, value);
        }
        for (Map.Entry<Argument, Object> entry : argumentValues.entrySet()) {
            Field field = arguments.get(entry.getKey());
            Object value;
            try {
                value = convert(action, session, entry.getValue(), field.getGenericType());
            } catch (Exception e) {
                Command command = action.getClass().getAnnotation(Command.class);
                throw new CommandException(
                        Ansi.ansi()
                                .fg(Ansi.Color.RED)
                                .a("Error executing command ")
                                .a(command.scope())
                                .a(":")
                                .a(Ansi.Attribute.INTENSITY_BOLD)
                                .a(command.name())
                                .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                .a(": unable to convert argument ")
                                .a(Ansi.Attribute.INTENSITY_BOLD)
                                .a(entry.getKey().name())
                                .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
View Full Code Here


        return true;
    }

    protected void printUsage(CommandSession session, Action action, Map<Option,Field> optionsMap, Map<Argument,Field> argsMap, PrintStream out)
    {
        Command command = action.getClass().getAnnotation(Command.class);
        Terminal term = session != null ? (Terminal) session.get(".jline.terminal") : null;
        List<Argument> arguments = new ArrayList<Argument>(argsMap.keySet());
        Collections.sort(arguments, new Comparator<Argument>() {
            public int compare(Argument o1, Argument o2) {
                return Integer.valueOf(o1.index()).compareTo(Integer.valueOf(o2.index()));
            }
        });
        Set<Option> options = new HashSet<Option>(optionsMap.keySet());
        options.add(HELP);
        boolean globalScope = NameScoping.isGlobalScope(session, command.scope());
        if (command != null && (command.description() != null || command.name() != null))
        {
            out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("DESCRIPTION").a(Ansi.Attribute.RESET));
            out.print("        ");
            if (command.name() != null) {
                if (globalScope) {
                    out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(command.name()).a(Ansi.Attribute.RESET));
                } else {
                    out.println(Ansi.ansi().a(command.scope()).a(":").a(Ansi.Attribute.INTENSITY_BOLD).a(command.name()).a(Ansi.Attribute.RESET));
                }
                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 : arguments)
            {
                if (!argument.required())
                {
                    syntax.append(String.format("[%s] ", argument.name()));
                }
                else
                {
                    syntax.append(String.format("%s ", argument.name()));
                }
            }
        }

        out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("SYNTAX").a(Ansi.Attribute.RESET));
        out.print("        ");
        out.println(syntax.toString());
        out.println();
        if (arguments.size() > 0)
        {
            out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("ARGUMENTS").a(Ansi.Attribute.RESET));
            for (Argument argument : arguments)
            {
                out.print("        ");
                out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(argument.name()).a(Ansi.Attribute.RESET));
                printFormatted("                ", argument.description(), term != null ? term.getWidth() : 80, out);
                if (!argument.required()) {
                    try {
                        argsMap.get(argument).setAccessible(true);
                        Object o = argsMap.get(argument).get(action);
                        if (o != null
                                && (!(o instanceof Boolean) || ((Boolean) o))
                                && (!(o instanceof Number) || ((Number) o).doubleValue() != 0.0)) {
                            out.print("                    (defaults to ");
                            out.print(o.toString());
                            out.println(")");
                        }
                    } catch (Throwable t) {
                        // Ignore
                    }
                }
            }
            out.println();
        }
        if (options.size() > 0)
        {
            out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("OPTIONS").a(Ansi.Attribute.RESET));
            for (Option option : options)
            {
                String opt = option.name();
                for (String alias : option.aliases())
                {
                    opt += ", " + alias;
                }
                out.print("        ");
                out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(opt).a(Ansi.Attribute.RESET));
                printFormatted("                ", option.description(), term != null ? term.getWidth() : 80, out);
                try {
                    optionsMap.get(option).setAccessible(true);
                    Object o = optionsMap.get(option).get(action);
                    if (o != null
                            && (!(o instanceof Boolean) || ((Boolean) o))
                            && (!(o instanceof Number) || ((Number) o).doubleValue() != 0.0)) {
                        out.print("                (defaults to ");
                        out.print(o.toString());
                        out.println(")");
                    }
                } catch (Throwable t) {
                    // Ignore
                }
            }
            out.println();
        }
        if (command.detailedDescription().length() > 0) {
            out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("DETAILS").a(Ansi.Attribute.RESET));
            String desc = loadDescription(action.getClass(), command.detailedDescription());
            printFormatted("        ", desc, term != null ? term.getWidth() : 80, out);
        }
    }
View Full Code Here

            Map<String, Set<String>> commands = new TreeMap<String, Set<String>>();

            for (Class clazz : classes) {
                try {
                    String help = new HelpPrinter(clazz).printHelp(format);
                    Command cmd = (Command) clazz.getAnnotation(Command.class);
                    File output = null;
                    if (FORMAT_DOCBX.equals(format)) {
                        output = new File(targetFolder, cmd.scope() + "-" + cmd.name() + ".xml");
                    } else if (FORMAT_CONF.equals(format)) {
                        output = new File(targetFolder, cmd.scope() + "-" + cmd.name() + ".conf");
                    }
                    Writer writer = new OutputStreamWriter(new FileOutputStream(output));
                    writer.write(help);
                    writer.close();

                    Set<String> cmds = commands.get(cmd.scope());
                    if (cmds == null) {
                        cmds = new TreeSet<String>();
                        commands.put(cmd.scope(), cmds);
                    }
                    cmds.add(cmd.name());
                    getLog().info("Found command: " + cmd.scope() + ":" + cmd.name());
                } catch (Exception e) {
                    getLog().warn("Unable to write help for " + clazz.getName(), e);
                }
            }
View Full Code Here

        protected class DocbxPreparator extends BlueprintActionPreparator {

            @Override
            protected void printUsage(CommandSession session, Action action, Map<Option,Field> optionsMap, Map<Argument,Field> argsMap, PrintStream out)
            {
                Command command = action.getClass().getAnnotation(Command.class);
                List<Argument> arguments = new ArrayList<Argument>(argsMap.keySet());
                Collections.sort(arguments, new Comparator<Argument>() {
                    public int compare(Argument o1, Argument o2) {
                        return Integer.valueOf(o1.index()).compareTo(Integer.valueOf(o2.index()));
                    }
                });
                Set<Option> options = new HashSet<Option>(optionsMap.keySet());
                options.add(HELP);

                out.println("<section>");
                out.print("  <title>");
                out.print(command.scope());
                out.print(":");
                out.print(command.name());
                out.println("</title>");
                out.println("  <section>");
                out.println("    <title>Description</title>");
                out.println("    <para>");
                out.println(command.description());
                out.println("    </para>");
                out.println("  </section>");

                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(' ');
View Full Code Here

        protected class ConfPreparator extends BlueprintActionPreparator {

            @Override
            protected void printUsage(CommandSession session, Action action, Map<Option, Field> optionsMap, Map<Argument,Field> argsMap, PrintStream out)
            {
                Command command = action.getClass().getAnnotation(Command.class);
                List<Argument> arguments = new ArrayList<Argument>(argsMap.keySet());
                Collections.sort(arguments, new Comparator<Argument>() {
                    public int compare(Argument o1, Argument o2) {
                        return Integer.valueOf(o1.index()).compareTo(Integer.valueOf(o2.index()));
                    }
                });
                Set<Option> options = new HashSet<Option>(optionsMap.keySet());
                options.add(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()) {
                            try {
                                argsMap.get(argument).setAccessible(true);
                                Object o = argsMap.get(argument).get(action);
                                if (o != null
                                        && (!(o instanceof Boolean) || ((Boolean) o))
                                        && (!(o instanceof Number) || ((Number) o).doubleValue() != 0.0)) {
                                    description += " (defaults to " + o.toString() + ")";
                                }
                            } catch (Throwable t) {
                                // Ignore
                            }
                        }
                        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;
                        }
                        try {
                            optionsMap.get(option).setAccessible(true);
                            Object o = optionsMap.get(option).get(action);
                            if (o != null
                                    && (!(o instanceof Boolean) || ((Boolean) o))
                                    && (!(o instanceof Number) || ((Number) o).doubleValue() != 0.0)) {
                                desc += " (defaults to " + o.toString() + ")";
                            }
                        } catch (Throwable t) {
                            // Ignore
                        }
                        out.println("| " + opt + " | " + desc + " |");
                    }
                    out.println();
                }
                if (command.detailedDescription().length() > 0) {
                    out.println("h2. Details");
                    String desc = loadDescription(action.getClass(), command.detailedDescription());
                    out.println(desc);
                }
                out.println();
            }
View Full Code Here

    }

    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.");
    }
View Full Code Here

            String line = r.readLine();
            while (line != null) {
                line = line.trim();
                if (line.length() > 0 && line.charAt(0) != '#') {
                    final Class<Action> actionClass = (Class<Action>) cl.loadClass(line);
                    Command cmd = actionClass.getAnnotation(Command.class);
                    Function function = new AbstractCommand() {
                        @Override
                        public Action createNewAction() {
                            try {
                                return ((Class<? extends Action>) actionClass).newInstance();
View Full Code Here

    }

    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.");
    }
View Full Code Here

                        option = opt;
                        break;
                    }
                }
                if (option == null) {
                    Command command = action.getClass().getAnnotation(Command.class);
                    if (command != null) {
                        throw new CommandException(
                                Ansi.ansi()
                                        .fg(Ansi.Color.RED)
                                        .a("Error executing command ")
                                        .a(command.scope())
                                        .a(":")
                                        .a(Ansi.Attribute.INTENSITY_BOLD)
                                        .a(command.name())
                                        .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                        .a(" undefined option ")
                                        .a(Ansi.Attribute.INTENSITY_BOLD)
                                        .a(param)
                                        .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                        .fg(Ansi.Color.DEFAULT)
                                        .toString(),
                                "Undefined option: " + param
                        );
                    } else {
                        throw new CommandException("Undefined option: " + param);
                    }
                }
                Field field = options.get(option);
                if (value == null && (field.getType() == boolean.class || field.getType() == Boolean.class)) {
                    value = Boolean.TRUE;
                }
                if (value == null && it.hasNext()) {
                    value = it.next();
                }
                if (value == null) {
                    Command command = action.getClass().getAnnotation(Command.class);
                    if (command != null) {
                        throw new CommandException(
                                Ansi.ansi()
                                        .fg(Ansi.Color.RED)
                                        .a("Error executing command ")
                                        .a(command.scope())
                                        .a(":")
                                        .a(Ansi.Attribute.INTENSITY_BOLD)
                                        .a(command.name())
                                        .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                        .a(" missing value for option ")
                                        .a(Ansi.Attribute.INTENSITY_BOLD)
                                        .a(param)
                                        .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                        .fg(Ansi.Color.DEFAULT)
                                        .toString(),
                                "Missing value for option: " + param
                        );
                    } else {
                        throw new CommandException("Missing value for option: " + param);
                    }
                }
                if (option.multiValued()) {
                    List<Object> l = (List<Object>) optionValues.get(option);
                    if (l == null) {
                        l = new ArrayList<Object>();
                        optionValues.put(option, l);
                    }
                    l.add(value);
                } else {
                    optionValues.put(option, value);
                }
            } else {
                processOptions = false;
                if (argIndex >= orderedArguments.size()) {
                    Command command = action.getClass().getAnnotation(Command.class);
                    if (command != null) {
                        throw new CommandException(
                                Ansi.ansi()
                                        .fg(Ansi.Color.RED)
                                        .a("Error executing command ")
                                        .a(command.scope())
                                        .a(":")
                                        .a(Ansi.Attribute.INTENSITY_BOLD)
                                        .a(command.name())
                                        .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                        .a(": too many arguments specified")
                                        .fg(Ansi.Color.DEFAULT)
                                        .toString(),
                                "Too many arguments specified"
                        );
                    } else {
                        throw new CommandException("Too many arguments specified");
                    }
                }
                Argument argument = orderedArguments.get(argIndex);
                if (!argument.multiValued()) {
                    argIndex++;
                }
                if (argument.multiValued()) {
                    List<Object> l = (List<Object>) argumentValues.get(argument);
                    if (l == null) {
                        l = new ArrayList<Object>();
                        argumentValues.put(argument, l);
                    }
                    l.add(param);
                } else {
                    argumentValues.put(argument, param);
                }
            }
        }
        // Check required arguments / options
        for (Option option : options.keySet()) {
            if (option.required() && optionValues.get(option) == null) {
                Command command = action.getClass().getAnnotation(Command.class);
                if (command != null) {
                    throw new CommandException(
                            Ansi.ansi()
                                    .fg(Ansi.Color.RED)
                                    .a("Error executing command ")
                                    .a(command.scope())
                                    .a(":")
                                    .a(Ansi.Attribute.INTENSITY_BOLD)
                                    .a(command.name())
                                    .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                    .a(": option ")
                                    .a(Ansi.Attribute.INTENSITY_BOLD)
                                    .a(option.name())
                                    .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                    .a(" is required")
                                    .fg(Ansi.Color.DEFAULT)
                                    .toString(),
                            "Option " + option.name() + " is required"
                    );
                } else {
                    throw new CommandException("Option " + option.name() + " is required");
                }
            }
        }
        for (Argument argument : orderedArguments) {
            if (argument.required() && argumentValues.get(argument) == null) {
                Command command = action.getClass().getAnnotation(Command.class);
                if (command != null) {
                    throw new CommandException(
                            Ansi.ansi()
                                    .fg(Ansi.Color.RED)
                                    .a("Error executing command ")
                                    .a(command.scope())
                                    .a(":")
                                    .a(Ansi.Attribute.INTENSITY_BOLD)
                                    .a(command.name())
                                    .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                    .a(": argument ")
                                    .a(Ansi.Attribute.INTENSITY_BOLD)
                                    .a(argument.name())
                                    .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                    .a(" is required")
                                    .fg(Ansi.Color.DEFAULT)
                                    .toString(),
                            "Argument " + argument.name() + " is required"
                    );
                } else {
                    throw new CommandException("Argument " + argument.name() + " is required");
                }
            }
        }
        // Convert and inject values
        for (Map.Entry<Option, Object> entry : optionValues.entrySet()) {
            Field field = options.get(entry.getKey());
            Object value;
            try {
                value = convert(action, session, entry.getValue(), field.getGenericType());
            } catch (Exception e) {
                Command command = action.getClass().getAnnotation(Command.class);
                if (command != null) {
                    throw new CommandException(
                            Ansi.ansi()
                                    .fg(Ansi.Color.RED)
                                    .a("Error executing command ")
                                    .a(command.scope())
                                    .a(":")
                                    .a(Ansi.Attribute.INTENSITY_BOLD)
                                    .a(command.name())
                                    .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                    .a(": unable to convert option ")
                                    .a(Ansi.Attribute.INTENSITY_BOLD)
                                    .a(entry.getKey().name())
                                    .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                    .a(" with value '")
                                    .a(entry.getValue())
                                    .a("' to type ")
                                    .a(new GenericType(field.getGenericType()).toString())
                                    .fg(Ansi.Color.DEFAULT)
                                    .toString(),
                            "Unable to convert option " + entry.getKey().name() + " with value '"
                                    + entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(),
                            e
                    );
                } else {
                    throw new CommandException("Unable to convert option " + entry.getKey().name() + " with value '"
                            + entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(),
                            e);
                }
            }
            field.setAccessible(true);
            field.set(action, value);
        }
        for (Map.Entry<Argument, Object> entry : argumentValues.entrySet()) {
            Field field = arguments.get(entry.getKey());
            Object value;
            try {
                value = convert(action, session, entry.getValue(), field.getGenericType());
            } catch (Exception e) {
                Command command = action.getClass().getAnnotation(Command.class);
                if (command != null) {
                    throw new CommandException(
                            Ansi.ansi()
                                    .fg(Ansi.Color.RED)
                                    .a("Error executing command ")
                                    .a(command.scope())
                                    .a(":")
                                    .a(Ansi.Attribute.INTENSITY_BOLD)
                                    .a(command.name())
                                    .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                                    .a(": unable to convert argument ")
                                    .a(Ansi.Attribute.INTENSITY_BOLD)
                                    .a(entry.getKey().name())
                                    .a(Ansi.Attribute.INTENSITY_BOLD_OFF)
View Full Code Here

        }
        return true;
    }

    protected void printUsage(CommandSession session, Action action, Map<Option, Field> optionsMap, Map<Argument, Field> argsMap, PrintStream out) {
        Command command = action.getClass().getAnnotation(Command.class);
        if (command != null) {
            Terminal term = session != null ? (Terminal) session.get(".jline.terminal") : null;
            List<Argument> arguments = new ArrayList<Argument>(argsMap.keySet());
            Collections.sort(arguments, new Comparator<Argument>() {
                public int compare(Argument o1, Argument o2) {
                    return Integer.valueOf(o1.index()).compareTo(Integer.valueOf(o2.index()));
                }
            });
            Set<Option> options = new HashSet<Option>(optionsMap.keySet());
            options.add(HELP);
            boolean globalScope = NameScoping.isGlobalScope(session, command.scope());
            if (command != null && (command.description() != null || command.name() != null)) {
                out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("DESCRIPTION").a(Ansi.Attribute.RESET));
                out.print("        ");
                if (command.name() != null) {
                    if (globalScope) {
                        out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(command.name()).a(Ansi.Attribute.RESET));
                    } else {
                        out.println(Ansi.ansi().a(command.scope()).a(":").a(Ansi.Attribute.INTENSITY_BOLD).a(command.name()).a(Ansi.Attribute.RESET));
                    }
                    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 : arguments) {
                    if (!argument.required()) {
                        syntax.append(String.format("[%s] ", argument.name()));
                    } else {
                        syntax.append(String.format("%s ", argument.name()));
                    }
                }
            }

            out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("SYNTAX").a(Ansi.Attribute.RESET));
            out.print("        ");
            out.println(syntax.toString());
            out.println();
            if (arguments.size() > 0) {
                out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("ARGUMENTS").a(Ansi.Attribute.RESET));
                for (Argument argument : arguments) {
                    out.print("        ");
                    out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(argument.name()).a(Ansi.Attribute.RESET));
                    printFormatted("                ", argument.description(), term != null ? term.getWidth() : 80, out);
                    if (!argument.required()) {
                        if (argument.valueToShowInHelp() != null && argument.valueToShowInHelp().length() != 0) {
                            try {
                                if (Argument.DEFAULT_STRING.equals(argument.valueToShowInHelp())) {
                                    argsMap.get(argument).setAccessible(true);
                                    Object o = argsMap.get(argument).get(action);
                                    printObjectDefaultsTo(out, o);
                                } else {
                                    printDefaultsTo(out, argument.valueToShowInHelp());
                                }
                            } catch (Throwable t) {
                                // Ignore
                            }
                        }
                    }
                }
                out.println();
            }
            if (options.size() > 0) {
                out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("OPTIONS").a(Ansi.Attribute.RESET));
                for (Option option : options) {
                    String opt = option.name();
                    for (String alias : option.aliases()) {
                        opt += ", " + alias;
                    }
                    out.print("        ");
                    out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(opt).a(Ansi.Attribute.RESET));
                    printFormatted("                ", option.description(), term != null ? term.getWidth() : 80, out);
                    if (option.valueToShowInHelp() != null && option.valueToShowInHelp().length() != 0) {
                        try {
                            if (Option.DEFAULT_STRING.equals(option.valueToShowInHelp())) {
                                optionsMap.get(option).setAccessible(true);
                                Object o = optionsMap.get(option).get(action);
                                printObjectDefaultsTo(out, o);
                            } else {
                                printDefaultsTo(out, option.valueToShowInHelp());
                            }
                        } catch (Throwable t) {
                            // Ignore
                        }
                    }
                }
                out.println();
            }
            if (command.detailedDescription().length() > 0) {
                out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("DETAILS").a(Ansi.Attribute.RESET));
                String desc = loadDescription(action.getClass(), command.detailedDescription());
                printFormatted("        ", desc, term != null ? term.getWidth() : 80, out);
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.felix.gogo.commands.Command

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.