Search in sources :

Example 1 with ArgumentCommandLine

use of org.apache.karaf.shell.support.completers.ArgumentCommandLine in project karaf by apache.

the class ArgumentCompleter method verifyCompleter.

protected boolean verifyCompleter(Session session, Completer completer, String argument) {
    List<Candidate> candidates = new ArrayList<>();
    completer.completeCandidates(session, new ArgumentCommandLine(argument, argument.length()), candidates);
    return !candidates.isEmpty();
}
Also used : Candidate(org.apache.karaf.shell.api.console.Candidate) ArgumentCommandLine(org.apache.karaf.shell.support.completers.ArgumentCommandLine) ArrayList(java.util.ArrayList)

Example 2 with ArgumentCommandLine

use of org.apache.karaf.shell.support.completers.ArgumentCommandLine in project karaf by apache.

the class HelpCommand method getCompleter.

@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()));
            if (argIndex == 0) {
                return completer.complete(session, new ArgumentCommandLine(args[argIndex], commandLine.getArgumentPosition()), candidates);
            } else if (!verifyCompleter(session, completer, args[0])) {
                return -1;
            }
            // TODO: use CommandNamesCompleter and better completion wrt parsing etc...
            completer = new StringsCompleter();
            for (Command command : session.getRegistry().getCommands()) {
                if (!Session.SCOPE_GLOBAL.equals(command.getScope())) {
                    completer.getStrings().add(command.getScope() + ":" + command.getName());
                }
                completer.getStrings().add(command.getName());
            }
            completer.getStrings().add("--help");
            if (argIndex == 1) {
                int res;
                if (argIndex < args.length) {
                    res = completer.complete(session, new ArgumentCommandLine(args[argIndex], commandLine.getArgumentPosition()), candidates);
                } else {
                    res = completer.complete(session, new ArgumentCommandLine("", 0), candidates);
                }
                return res + (commandLine.getBufferPosition() - commandLine.getArgumentPosition());
            } else if (!verifyCompleter(session, completer, args[1])) {
                return -1;
            }
            return -1;
        }

        protected boolean verifyCompleter(Session session, Completer completer, String argument) {
            List<String> candidates = new ArrayList<>();
            return completer.complete(session, new ArgumentCommandLine(argument, argument.length()), candidates) != -1 && !candidates.isEmpty();
        }
    };
}
Also used : ArgumentCommandLine(org.apache.karaf.shell.support.completers.ArgumentCommandLine) CommandLine(org.apache.karaf.shell.api.console.CommandLine) ArgumentCommandLine(org.apache.karaf.shell.support.completers.ArgumentCommandLine) StringsCompleter(org.apache.karaf.shell.support.completers.StringsCompleter) Command(org.apache.karaf.shell.api.console.Command) ArrayList(java.util.ArrayList) Completer(org.apache.karaf.shell.api.console.Completer) StringsCompleter(org.apache.karaf.shell.support.completers.StringsCompleter) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.apache.karaf.shell.api.console.Session)

Aggregations

ArrayList (java.util.ArrayList)2 ArgumentCommandLine (org.apache.karaf.shell.support.completers.ArgumentCommandLine)2 List (java.util.List)1 Candidate (org.apache.karaf.shell.api.console.Candidate)1 Command (org.apache.karaf.shell.api.console.Command)1 CommandLine (org.apache.karaf.shell.api.console.CommandLine)1 Completer (org.apache.karaf.shell.api.console.Completer)1 Session (org.apache.karaf.shell.api.console.Session)1 StringsCompleter (org.apache.karaf.shell.support.completers.StringsCompleter)1