Package org.apache.felix.gogo.commands

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


        Map<Argument, Field> arguments = new HashMap<Argument, Field>();
        List<Argument> orderedArguments = new ArrayList<Argument>();
        // Introspect
        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.put(option, field);
                }
                Argument argument = field.getAnnotation(Argument.class);
                if (argument != null) {
                    if (Argument.DEFAULT.equals(argument.name())) {
                        final Argument delegate = argument;
                        final String name = field.getName();
                        argument = new Argument() {
                            public String name() {
                                return name;
                            }
                            public String description() {
                                return delegate.description();
                            }
                            public boolean required() {
                                return delegate.required();
                            }
                            public int index() {
                                return delegate.index();
                            }
                            public boolean multiValued() {
                                return delegate.multiValued();
                            }
                            public Class<? extends Annotation> annotationType() {
                                return delegate.annotationType();
                            }
                        };
                    }
                    arguments.put(argument, field);
                    int index = argument.index();
                    while (orderedArguments.size() <= index) {
                        orderedArguments.add(null);
                    }
                    if (orderedArguments.get(index) != null) {
                        throw new IllegalArgumentException("Duplicate argument index: " + index);
                    }
                    orderedArguments.set(index, argument);
                }
            }
        }
        // Check indexes are correct
        for (int i = 0; i < orderedArguments.size(); i++) {
            if (orderedArguments.get(i) == null) {
                throw new IllegalArgumentException("Missing argument for index: " + i);
            }
        }
        // Populate
        Map<Option, Object> optionValues = new HashMap<Option, Object>();
        Map<Argument, Object> argumentValues = new HashMap<Argument, Object>();
        boolean processOptions = true;
        int argIndex = 0;
        for (Iterator<Object> it = params.iterator(); it.hasNext();) {
            Object param = it.next();
            // Check for help
            if (HELP.name().equals(param) || Arrays.asList(HELP.aliases()).contains(param)) {
                printUsage(session, action, options, arguments, System.out);
                return false;
            }
            if (processOptions && param instanceof String && ((String) param).startsWith("-")) {
                boolean isKeyValuePair = ((String) param).indexOf('=') != -1;
                String name;
                Object value = null;
                if (isKeyValuePair) {
                    name = ((String) param).substring(0, ((String) param).indexOf('='));
                    value = ((String) param).substring(((String) param).indexOf('=') + 1);
                } else {
                    name = (String) param;
                }
                Option option = null;
                for (Option opt : options.keySet()) {
                    if (name.equals(opt.name()) || Arrays.asList(opt.aliases()).contains(name)) {
                        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) {
View Full Code Here


        Map<Argument, Field> arguments = new HashMap<Argument, Field>();
        List<Argument> orderedArguments = new ArrayList<Argument>();
        // Introspect
        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.put(option, field);
                }
                Argument argument = field.getAnnotation(Argument.class);
                if (argument != null) {
                    if (Argument.DEFAULT.equals(argument.name())) {
                        final Argument delegate = argument;
                        final String name = field.getName();
                        argument = new Argument() {
                            public String name() {
                                return name;
                            }

                            public String description() {
                                return delegate.description();
                            }

                            public boolean required() {
                                return delegate.required();
                            }

                            public int index() {
                                return delegate.index();
                            }

                            public boolean multiValued() {
                                return delegate.multiValued();
                            }

                            public String valueToShowInHelp() {
                                return delegate.valueToShowInHelp();
                            }

                            public Class<? extends Annotation> annotationType() {
                                return delegate.annotationType();
                            }
                        };
                    }
                    arguments.put(argument, field);
                    int index = argument.index();
                    while (orderedArguments.size() <= index) {
                        orderedArguments.add(null);
                    }
                    if (orderedArguments.get(index) != null) {
                        throw new IllegalArgumentException("Duplicate argument index: " + index);
                    }
                    orderedArguments.set(index, argument);
                }
            }
        }
        // Check indexes are correct
        for (int i = 0; i < orderedArguments.size(); i++) {
            if (orderedArguments.get(i) == null) {
                throw new IllegalArgumentException("Missing argument for index: " + i);
            }
        }
        // Populate
        Map<Option, Object> optionValues = new HashMap<Option, Object>();
        Map<Argument, Object> argumentValues = new HashMap<Argument, Object>();
        boolean processOptions = true;
        int argIndex = 0;
        for (Iterator<Object> it = params.iterator(); it.hasNext(); ) {
            Object param = it.next();
            // Check for help
            if (HELP.name().equals(param) || Arrays.asList(HELP.aliases()).contains(param)) {
                printUsage(session, action, options, arguments, System.out);
                return false;
            }
            if (processOptions && param instanceof String && ((String) param).startsWith("-")) {
                boolean isKeyValuePair = ((String) param).indexOf('=') != -1;
                String name;
                Object value = null;
                if (isKeyValuePair) {
                    name = ((String) param).substring(0, ((String) param).indexOf('='));
                    value = ((String) param).substring(((String) param).indexOf('=') + 1);
                } else {
                    name = (String) param;
                }
                Option option = null;
                for (Option opt : options.keySet()) {
                    if (name.equals(opt.name()) || Arrays.asList(opt.aliases()).contains(name)) {
                        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) {
View Full Code Here

        // Command name completer
        commandCompleter = new StringsCompleter(getNames(session, command));
        // Build options completer
        for (Class<?> type = function.getActionClass(); type != null; type = type.getSuperclass()) {
            for (Field field : type.getDeclaredFields()) {
                Option option = field.getAnnotation(Option.class);
                if (option != null) {
                    fields.put(option, field);
                    options.put(option.name(), option);
                    String[] aliases = option.aliases();
                    if (aliases != null) {
                        for (String alias : aliases) {
                            options.put(alias, option);
                        }
                    }
View Full Code Here

        if (comp == null) {
            while (index < argIndex && args[index].startsWith("-")) {
                if (!verifyCompleter(optionsCompleter, args[index])) {
                    return -1;
                }
                Option option = options.get(args[index]);
                if (option == null) {
                    return -1;
                }
                Field field = fields.get(option);
                if (field != null && field.getType() != boolean.class && field.getType() != Boolean.class) {
                    if (++index == argIndex) {
                        comp = NullCompleter.INSTANCE;
                    }
                }
                index++;
            }
            if (comp == null && index >= argIndex && index < args.length && args[index].startsWith("-")) {
                comp = optionsCompleter;
            }
        }
        //Now check for if last Option has a completer
        int lastAgurmentIndex = argIndex - 1;
        if (lastAgurmentIndex >= 1) {
            Option lastOption = options.get(args[lastAgurmentIndex]);
            if (lastOption != null) {

                Field lastField = fields.get(lastOption);
                if (lastField != null && lastField.getType() != boolean.class && lastField.getType() != Boolean.class) {
                    Option option = lastField.getAnnotation(Option.class);
                    if (option != null) {
                        Completer optionValueCompleter = null;
                        String name = option.name();
                        if (optionalCompleters != null && name != null) {
                            optionValueCompleter = optionalCompleters.get(name);
                            if (optionValueCompleter == null) {
                                String[] aliases = option.aliases();
                                if (aliases.length > 0) {
                                    for (int i = 0; i < aliases.length && optionValueCompleter == null; i++) {
                                        optionValueCompleter = optionalCompleters.get(option.aliases()[i]);
                                    }
                                }
                            }
                        }
                        if(optionValueCompleter != null) {
View Full Code Here

        }
        params = newParams;
        // Introspect
        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.put(option, field);
                }
                Argument argument = field.getAnnotation(Argument.class);
                if (argument != null) {
                    if (Argument.DEFAULT.equals(argument.name())) {
                        final Argument delegate = argument;
                        final String name = field.getName();
                        argument = new Argument() {
                            public String name() {
                                return name;
                            }

                            public String description() {
                                return delegate.description();
                            }

                            public boolean required() {
                                return delegate.required();
                            }

                            public int index() {
                                return delegate.index();
                            }

                            public boolean multiValued() {
                                return delegate.multiValued();
                            }

                            public String valueToShowInHelp() {
                                return delegate.valueToShowInHelp();
                            }

                            public Class<? extends Annotation> annotationType() {
                                return delegate.annotationType();
                            }
                        };
                    }
                    arguments.put(argument, field);
                    int index = argument.index();
                    while (orderedArguments.size() <= index) {
                        orderedArguments.add(null);
                    }
                    if (orderedArguments.get(index) != null) {
                        throw new IllegalArgumentException("Duplicate argument index: " + index);
                    }
                    orderedArguments.set(index, argument);
                }
            }
        }
        // Check indexes are correct
        for (int i = 0; i < orderedArguments.size(); i++) {
            if (orderedArguments.get(i) == null) {
                throw new IllegalArgumentException("Missing argument for index: " + i);
            }
        }
        // Populate
        Map<Option, Object> optionValues = new HashMap<Option, Object>();
        Map<Argument, Object> argumentValues = new HashMap<Argument, Object>();
        boolean processOptions = true;
        int argIndex = 0;
        for (Iterator<Object> it = params.iterator(); it.hasNext(); ) {
            Object param = it.next();
            // Check for help
            if (HELP.name().equals(param) || Arrays.asList(HELP.aliases()).contains(param)) {
                printUsage(session, action, options, arguments, System.out);
                return false;
            }
            if (processOptions && param instanceof String && ((String) param).startsWith("-")) {
                boolean isKeyValuePair = ((String) param).indexOf('=') != -1;
                String name;
                Object value = null;
                if (isKeyValuePair) {
                    name = ((String) param).substring(0, ((String) param).indexOf('='));
                    value = ((String) param).substring(((String) param).indexOf('=') + 1);
                } else {
                    name = (String) param;
                }
                Option option = null;
                for (Option opt : options.keySet()) {
                    if (name.equals(opt.name()) || Arrays.asList(opt.aliases()).contains(name)) {
                        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) {
View Full Code Here

        // Command name completer
        commandCompleter = new StringsCompleter(getNames(session, command));
        // Build options completer
        for (Class type = function.getActionClass(); type != null; type = type.getSuperclass()) {
            for (Field field : type.getDeclaredFields()) {
                Option option = field.getAnnotation(Option.class);
                if (option != null) {
                    fields.put(option, field);
                    options.put(option.name(), option);
                    String[] aliases = option.aliases();
                    if (aliases != null) {
                        for (String alias : aliases) {
                            options.put(alias, option);
                        }
                    }
View Full Code Here

        if (comp == null) {
            while (index < argIndex && args[index].startsWith("-")) {
                if (!verifyCompleter(optionsCompleter, args[index])) {
                    return -1;
                }
                Option option = options.get(args[index]);
                if (option == null) {
                    return -1;
                }
                Field field = fields.get(option);
                if (field != null && field.getType() != boolean.class && field.getType() != Boolean.class) {
                    if (++index == argIndex) {
                        comp = NullCompleter.INSTANCE;
                    }
                }
                index++;
            }
            if (comp == null && index >= argIndex && index < args.length && args[index].startsWith("-")) {
                comp = optionsCompleter;
            }
        }
        //Now check for if last Option has a completer
        int lastAgurmentIndex = argIndex - 1;
        if (lastAgurmentIndex >= 1) {
            Option lastOption = options.get(args[lastAgurmentIndex]);
            if (lastOption != null) {

                Field lastField = fields.get(lastOption);
                if (lastField != null && lastField.getType() != boolean.class && lastField.getType() != Boolean.class) {
                    Option option = lastField.getAnnotation(Option.class);
                    if (option != null) {
                        Completer optionValueCompleter = null;
                        String name = option.name();
                        if (name != null) {
                            optionValueCompleter = optionalCompleters.get(name);
                            if (optionValueCompleter == null) {
                                String[] aliases = option.aliases();
                                if (aliases.length > 0) {
                                    for (int i = 0; i < aliases.length && optionValueCompleter == null; i++) {
                                        optionValueCompleter = optionalCompleters.get(option.aliases()[i]);
                                    }
                                }
                            }
                        }
                        if(optionValueCompleter != null) {
View Full Code Here

        Map<Argument, Field> arguments = new HashMap<Argument, Field>();
        List<Argument> orderedArguments = new ArrayList<Argument>();
        // Introspect
        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.put(option, field);
                }
                Argument argument = field.getAnnotation(Argument.class);
                if (argument != null) {
                    if (Argument.DEFAULT.equals(argument.name())) {
                        final Argument delegate = argument;
                        final String name = field.getName();
                        argument = new Argument() {
                            public String name() {
                                return name;
                            }

                            public String description() {
                                return delegate.description();
                            }

                            public boolean required() {
                                return delegate.required();
                            }

                            public int index() {
                                return delegate.index();
                            }

                            public boolean multiValued() {
                                return delegate.multiValued();
                            }

                            public String valueToShowInHelp() {
                                return delegate.valueToShowInHelp();
                            }

                            public Class<? extends Annotation> annotationType() {
                                return delegate.annotationType();
                            }
                        };
                    }
                    arguments.put(argument, field);
                    int index = argument.index();
                    while (orderedArguments.size() <= index) {
                        orderedArguments.add(null);
                    }
                    if (orderedArguments.get(index) != null) {
                        throw new IllegalArgumentException("Duplicate argument index: " + index);
                    }
                    orderedArguments.set(index, argument);
                }
            }
        }
        // Check indexes are correct
        for (int i = 0; i < orderedArguments.size(); i++) {
            if (orderedArguments.get(i) == null) {
                throw new IllegalArgumentException("Missing argument for index: " + i);
            }
        }
        // Populate
        Map<Option, Object> optionValues = new HashMap<Option, Object>();
        Map<Argument, Object> argumentValues = new HashMap<Argument, Object>();
        boolean processOptions = true;
        int argIndex = 0;
        for (Iterator<Object> it = params.iterator(); it.hasNext(); ) {
            Object param = it.next();
            // Check for help
            if (HELP.name().equals(param) || Arrays.asList(HELP.aliases()).contains(param)) {
                printUsage(session, action, options, arguments, System.out);
                return false;
            }
            if (processOptions && param instanceof String && ((String) param).startsWith("-")) {
                boolean isKeyValuePair = ((String) param).indexOf('=') != -1;
                String name;
                Object value = null;
                if (isKeyValuePair) {
                    name = ((String) param).substring(0, ((String) param).indexOf('='));
                    value = ((String) param).substring(((String) param).indexOf('=') + 1);
                } else {
                    name = (String) param;
                }
                Option option = null;
                for (Option opt : options.keySet()) {
                    if (name.equals(opt.name()) || Arrays.asList(opt.aliases()).contains(name)) {
                        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 : arguments.keySet()) {
            if (argument.required() && argumentValues.get(argument) == null) {
View Full Code Here

        Map<Argument, Field> arguments = new HashMap<Argument, Field>();
        List<Argument> orderedArguments = new ArrayList<Argument>();
        // Introspect
        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.put(option, field);
                }
                Argument argument = field.getAnnotation(Argument.class);
                if (argument != null) {
                    if (Argument.DEFAULT.equals(argument.name())) {
                        final Argument delegate = argument;
                        final String name = field.getName();
                        argument = new Argument() {
                            public String name() {
                                return name;
                            }

                            public String description() {
                                return delegate.description();
                            }

                            public boolean required() {
                                return delegate.required();
                            }

                            public int index() {
                                return delegate.index();
                            }

                            public boolean multiValued() {
                                return delegate.multiValued();
                            }

                            public String valueToShowInHelp() {
                                return delegate.valueToShowInHelp();
                            }

                            public Class<? extends Annotation> annotationType() {
                                return delegate.annotationType();
                            }
                        };
                    }
                    arguments.put(argument, field);
                    int index = argument.index();
                    while (orderedArguments.size() <= index) {
                        orderedArguments.add(null);
                    }
                    if (orderedArguments.get(index) != null) {
                        throw new IllegalArgumentException("Duplicate argument index: " + index);
                    }
                    orderedArguments.set(index, argument);
                }
            }
        }
        // Check indexes are correct
        for (int i = 0; i < orderedArguments.size(); i++) {
            if (orderedArguments.get(i) == null) {
                throw new IllegalArgumentException("Missing argument for index: " + i);
            }
        }
        // Populate
        Map<Option, Object> optionValues = new HashMap<Option, Object>();
        Map<Argument, Object> argumentValues = new HashMap<Argument, Object>();
        boolean processOptions = true;
        int argIndex = 0;
        for (Iterator<Object> it = params.iterator(); it.hasNext(); ) {
            Object param = it.next();
            // Check for help
            if (HELP.name().equals(param) || Arrays.asList(HELP.aliases()).contains(param)) {
                printUsage(session, action, options, arguments, System.out);
                return false;
            }
            if (processOptions && param instanceof String && ((String) param).startsWith("-")) {
                boolean isKeyValuePair = ((String) param).indexOf('=') != -1;
                String name;
                Object value = null;
                if (isKeyValuePair) {
                    name = ((String) param).substring(0, ((String) param).indexOf('='));
                    value = ((String) param).substring(((String) param).indexOf('=') + 1);
                } else {
                    name = (String) param;
                }
                Option option = null;
                for (Option opt : options.keySet()) {
                    if (name.equals(opt.name()) || Arrays.asList(opt.aliases()).contains(name)) {
                        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 : arguments.keySet()) {
            if (argument.required() && argumentValues.get(argument) == null) {
View Full Code Here

        Map<Argument, Field> arguments = new HashMap<Argument, Field>();
        List<Argument> orderedArguments = new ArrayList<Argument>();
        // Introspect
        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.put(option, field);
                }
                Argument argument = field.getAnnotation(Argument.class);
                if (argument != null) {
                    arguments.put(argument, field);
                    int index = argument.index();
                    while (orderedArguments.size() <= index) {
                        orderedArguments.add(null);
                    }
                    if (orderedArguments.get(index) != null) {
                        throw new IllegalArgumentException("Duplicate argument index: " + index);
                    }
                    orderedArguments.set(index, argument);
                }
            }
        }
        // Check indexes are correct
        for (int i = 0; i < orderedArguments.size(); i++) {
            if (orderedArguments.get(i) == null) {
                throw new IllegalArgumentException("Missing argument for index: " + i);
            }
        }
        // Populate
        Map<Option, Object> optionValues = new HashMap<Option, Object>();
        Map<Argument, Object> argumentValues = new HashMap<Argument, Object>();
        boolean processOptions = true;
        int argIndex = 0;
        for (Iterator<Object> it = params.iterator(); it.hasNext();) {
            Object param = it.next();
            // Check for help
            if (HELP.name().equals(param) || Arrays.asList(HELP.aliases()).contains(param)) {
                printUsage(action.getClass().getAnnotation(Command.class), options.keySet(), arguments.keySet(), System.out);
                return false;
            }
            if (processOptions && param instanceof String && ((String) param).startsWith("-")) {
                boolean isKeyValuePair = ((String) param).indexOf('=') != -1;
                String name;
                Object value = null;
                if (isKeyValuePair) {
                    name = ((String) param).substring(0, ((String) param).indexOf('='));
                    value = ((String) param).substring(((String) param).indexOf('=') + 1);
                } else {
                    name = (String) param;
                }
                Option option = null;
                for (Option opt : options.keySet()) {
                    if (name.equals(opt.name()) || Arrays.binarySearch(opt.aliases(), name) >= 0) {
                        option = opt;
                        break;
                    }
                }
                if (option == null) {
                    throw new IllegalArgumentException("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) {
                    throw new IllegalArgumentException("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()) {
                    throw new IllegalArgumentException("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) {
                throw new IllegalArgumentException("Option " + option.name() + " is required");
            }
        }
        for (Argument argument : arguments.keySet()) {
            if (argument.required() && argumentValues.get(argument) == null) {
                throw new IllegalArgumentException("Argument " + argument.name() + " is required");
View Full Code Here

TOP

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

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.