Package org.apache.karaf.shell.api.console

Examples of org.apache.karaf.shell.api.console.Completer


        return null;
    }

    @Override
    public Completer getCompleter(final boolean scoped) {
        return new Completer() {
            @Override
            public int complete(Session session, CommandLine commandLine, List<String> candidates) {
                String[] args = commandLine.getArguments();
                int argIndex = commandLine.getCursorArgumentIndex();
                StringsCompleter completer = new StringsCompleter(Collections.singletonList(getName()));
View Full Code Here


                }

                @Override
                public Completer getCompleter(final boolean scoped) {
                    final ArgumentCompleter completer = new ArgumentCompleter(oldCommand, getScope(), getName(), scoped);
                    return new Completer() {
                        @Override
                        public int complete(Session session, CommandLine commandLine, List<String> candidates) {
                            return completer.complete(session, commandLine, candidates);
                        }
                    };
                }

                @Override
                public Object execute(Session session, List<Object> arguments) throws Exception {
                    // TODO: remove not really nice cast
                    CommandSession commandSession = (CommandSession) session.get(".commandSession");
                    return oldCommand.execute(commandSession, arguments);
                }
            };
            sessionFactory.getRegistry().register(command);
            return command;
        } else if (service instanceof org.apache.felix.gogo.commands.CommandWithAction) {
            final org.apache.felix.gogo.commands.CommandWithAction oldCommand = (org.apache.felix.gogo.commands.CommandWithAction) service;
            final org.apache.karaf.shell.api.console.Command command = new org.apache.karaf.shell.api.console.Command() {
                @Override
                public String getScope() {
                    return reference.getProperty(CommandProcessor.COMMAND_SCOPE).toString();
                }

                @Override
                public String getName() {
                    return reference.getProperty(CommandProcessor.COMMAND_FUNCTION).toString();
                }

                @Override
                public String getDescription() {
                    final org.apache.felix.gogo.commands.Command cmd = oldCommand.getActionClass().getAnnotation(org.apache.felix.gogo.commands.Command.class);
                    if (cmd != null) {
                        return cmd.description();
                    } else {
                        return getName();
                    }
                }

                @Override
                public Completer getCompleter(final boolean scoped) {
                    final OldArgumentCompleter completer = new OldArgumentCompleter(oldCommand, getScope(), getName(), scoped);
                    return new Completer() {
                        @Override
                        public int complete(Session session, CommandLine commandLine, List<String> candidates) {
                            return completer.complete(session, commandLine, candidates);
                        }
                    };
View Full Code Here

        argsCompleters = new ArrayList<>();
        optionsCompleter = new StringsCompleter(options.keySet());

        boolean multi = false;
        for (int key = 0; key < arguments.size(); key++) {
            Completer completer = null;
            Field field = arguments.get(key);
            if (field != null) {
                Argument argument = field.getAnnotation(Argument.class);
                multi = (argument != null && argument.multiValued());
                Completion ann = field.getAnnotation(Completion.class);
                if (ann != null) {
                    Class<?> clazz = ann.value();
                    String[] value = ann.values();
                    if (clazz != null) {
                        if (value.length > 0 && clazz == StringsCompleter.class) {
                            completer = new StringsCompleter(value, ann.caseSensitive());
                        } else {
                            completer = command.getCompleter(clazz);
                        }
                    }
                } else {
                    completer = getDefaultCompleter(field, multi);
                }
            }
            if (completer == null) {
                completer = NullCompleter.INSTANCE;
            }
            argsCompleters.add(completer);
        }
        if (argsCompleters.isEmpty() || !multi) {
            argsCompleters.add(NullCompleter.INSTANCE);
        }
        optionalCompleters = new HashMap<>();
        for (Option option : fields.keySet()) {
            Completer completer = null;
            Field field = fields.get(option);
            if (field != null) {
                Completion ann = field.getAnnotation(Completion.class);
                if (ann != null) {
                    Class clazz = ann.value();
View Full Code Here

        }
    }

    @SuppressWarnings("unchecked")
    private Completer getDefaultCompleter(Field field, boolean multi) {
        Completer completer = null;
        Class<?> type = field.getType();
        GenericType genericType = new GenericType(field.getGenericType());
        if (Collection.class.isAssignableFrom(genericType.getRawClass()) && multi) {
            type = genericType.getActualTypeArgument(0).getRawClass();
        }
View Full Code Here

    }

    public int complete(Session session, final CommandLine list, final List<String> candidates) {
        int argIndex = list.getCursorArgumentIndex();

        Completer comp = null;
        String[] args = list.getArguments();
        int index = 0;
        // First argument is command name
        if (index < argIndex) {
            // Verify scope
            if (!Session.SCOPE_GLOBAL.equals(command.getScope()) && !session.resolveCommand(args[index]).equals(command.getScope() + ":" + command.getName())) {
                return -1;
            }
            // Verify command name
            if (!verifyCompleter(session, commandCompleter, args[index])) {
                return -1;
            }
            index++;
        } else {
            comp = commandCompleter;
        }
        // Now, check options
        if (comp == null) {
            while (index < argIndex && args[index].startsWith("-")) {
                if (!verifyCompleter(session, 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) {
                            comp = optionValueCompleter;
                        }
                    }
                }
            }
        }

        // Check arguments
        if (comp == null) {
            int indexArg = 0;
            while (index < argIndex) {
                Completer sub = argsCompleters.get(indexArg >= argsCompleters.size() ? argsCompleters.size() - 1 : indexArg);
                if (!verifyCompleter(session, sub, args[index])) {
                    return -1;
                }
                index++;
                indexArg++;
View Full Code Here

        registry.register(registry);
        registry.register(terminal);
        registry.register(history);

        // Completers
        Completer completer = new CommandsCompleter(factory);
        reader.addCompleter(new CompleterAsCompletor(this, completer));
        registry.register(completer);
        registry.register(new CommandNamesCompleter());
        registry.register(new FileCompleter());
        registry.register(new UriCompleter());
View Full Code Here

            Map<String, Completer> local = new HashMap<String, Completer>();

            // add argument completers for each command
            for (Command command : commands) {
                String key = command.getScope() + ":" + command.getName();
                Completer cg = command.getCompleter(false);
                Completer cl = command.getCompleter(true);
                if (cg == null) {
                    if (Session.SCOPE_GLOBAL.equals(command.getScope())) {
                        cg = new FixedSimpleCommandCompleter(Arrays.asList(command.getName()));
                    } else {
                        cg = new FixedSimpleCommandCompleter(Arrays.asList(key, command.getName()));
View Full Code Here

TOP

Related Classes of org.apache.karaf.shell.api.console.Completer

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.