Search in sources :

Example 11 with Command

use of org.apache.karaf.shell.api.console.Command in project karaf by apache.

the class CommandNamesCompleter method complete.

@Override
public int complete(Session session, CommandLine commandLine, List<String> candidates) {
    // TODO: optimize
    List<Command> list = session.getRegistry().getCommands();
    Set<String> names = new HashSet<>();
    for (Command command : list) {
        names.add(command.getScope() + ":" + command.getName());
        names.add(command.getName());
    }
    int res = new StringsCompleter(names).complete(session, commandLine, candidates);
    Collections.sort(candidates);
    return res;
}
Also used : Command(org.apache.karaf.shell.api.console.Command) StringsCompleter(org.apache.karaf.shell.support.completers.StringsCompleter) HashSet(java.util.HashSet)

Example 12 with Command

use of org.apache.karaf.shell.api.console.Command in project karaf by apache.

the class CommandLineParser method buildCommandLine.

public static CommandLine buildCommandLine(Session session, final String command, int cursor, AtomicInteger begOfLine) {
    int pos = 0;
    while (true) {
        String rem = command.substring(pos);
        GogoParser cmdNameParser = new GogoParser(rem, rem.length());
        String name = cmdNameParser.value();
        name = session.resolveCommand(name);
        Parser cmdParser = null;
        for (Command cmd : session.getRegistry().getCommands()) {
            if (name.equals(cmd.getScope() + ":" + cmd.getName())) {
                cmdParser = cmd.getParser();
                break;
            }
        }
        if (cmdParser == null) {
            cmdParser = new DefaultParser();
        }
        CommandLine cmdLine = cmdParser.parse(session, rem, cursor - pos);
        int length = cmdLine.getBuffer().length();
        if (length < rem.length()) {
            char ch = rem.charAt(length);
            if (ch == ';' || ch == '|') {
                length++;
            } else {
                throw new IllegalArgumentException("Unrecognized character: '" + ch + "'");
            }
        }
        pos += length;
        if (cursor <= pos) {
            begOfLine.set(pos - length);
            return cmdLine;
        }
    }
}
Also used : CommandLine(org.apache.karaf.shell.api.console.CommandLine) Command(org.apache.karaf.shell.api.console.Command) GogoParser(org.apache.karaf.shell.support.parsing.GogoParser) DefaultParser(org.apache.karaf.shell.support.parsing.DefaultParser) GogoParser(org.apache.karaf.shell.support.parsing.GogoParser) Parser(org.apache.karaf.shell.api.console.Parser) DefaultParser(org.apache.karaf.shell.support.parsing.DefaultParser)

Example 13 with Command

use of org.apache.karaf.shell.api.console.Command in project karaf by apache.

the class KarafParser method doParse.

private ParsedLine doParse(CharSequence line, int cursor) throws SyntaxError {
    org.apache.felix.gogo.runtime.Parser parser = new org.apache.felix.gogo.runtime.Parser(line);
    Program program = parser.program();
    List<Statement> statements = parser.statements();
    // Find corresponding statement
    Statement statement = null;
    for (int i = statements.size() - 1; i >= 0; i--) {
        Statement s = statements.get(i);
        if (s.start() <= cursor) {
            boolean isOk = true;
            // check if there are only spaces after the previous statement
            if (s.start() + s.length() < cursor) {
                for (int j = s.start() + s.length(); isOk && j < cursor; j++) {
                    isOk = Character.isWhitespace(line.charAt(j));
                }
            }
            statement = s;
            break;
        }
    }
    if (statement != null && statement.tokens() != null && !statement.tokens().isEmpty()) {
        String cmdName = session.resolveCommand(statement.tokens().get(0).toString());
        String[] parts = cmdName.split(":");
        Command cmd = parts.length == 2 ? session.getRegistry().getCommand(parts[0], parts[1]) : null;
        Parser cmdParser = cmd != null ? cmd.getParser() : null;
        if (cmdParser != null) {
            final CommandLine cmdLine = cmdParser.parse(session, statement.toString(), cursor - statement.start());
            return new ParsedLine() {

                @Override
                public String word() {
                    return cmdLine.getCursorArgument();
                }

                @Override
                public int wordCursor() {
                    return cmdLine.getArgumentPosition();
                }

                @Override
                public int wordIndex() {
                    return cmdLine.getCursorArgumentIndex();
                }

                @Override
                public List<String> words() {
                    return Arrays.asList(cmdLine.getArguments());
                }

                @Override
                public String line() {
                    return cmdLine.getBuffer();
                }

                @Override
                public int cursor() {
                    return cmdLine.getBufferPosition();
                }
            };
        }
        return new ParsedLineImpl(program, statement, cursor, statement.tokens());
    } else {
        // TODO:
        return new ParsedLineImpl(program, program, cursor, Collections.singletonList(program));
    }
}
Also used : Program(org.apache.felix.gogo.runtime.Parser.Program) Statement(org.apache.felix.gogo.runtime.Parser.Statement) Parser(org.apache.karaf.shell.api.console.Parser) CommandLine(org.apache.karaf.shell.api.console.CommandLine) Command(org.apache.karaf.shell.api.console.Command) ParsedLineImpl(org.apache.felix.gogo.jline.ParsedLineImpl) ParsedLine(org.jline.reader.ParsedLine)

Example 14 with Command

use of org.apache.karaf.shell.api.console.Command in project karaf by apache.

the class CommandsHelpProvider method getCommands.

private Set<Command> getCommands(Session session, String path) {
    // TODO: this is not really clean
    List<Command> commands = session.getRegistry().getCommands();
    String subshell = (String) session.get(Session.SUBSHELL);
    String completionMode = (String) session.get(Session.COMPLETION_MODE);
    Set<Command> matchingCommands = new HashSet<>();
    for (Command command : commands) {
        String name = command.getScope() + ":" + command.getName();
        if (command != null && !name.startsWith(path)) {
            continue;
        }
        if (completionMode != null && completionMode.equalsIgnoreCase(Session.COMPLETION_MODE_SUBSHELL)) {
            // filter the help only for "global" commands
            if (subshell == null || subshell.trim().isEmpty()) {
                if (!name.startsWith(Session.SCOPE_GLOBAL)) {
                    continue;
                }
            }
        }
        if (completionMode != null && (completionMode.equalsIgnoreCase(Session.COMPLETION_MODE_SUBSHELL) || completionMode.equalsIgnoreCase(Session.COMPLETION_MODE_FIRST))) {
            // filter the help only for commands local to the subshell
            if (!name.startsWith(subshell)) {
                continue;
            }
        }
        matchingCommands.add(command);
    }
    return matchingCommands;
}
Also used : Command(org.apache.karaf.shell.api.console.Command) HashSet(java.util.HashSet)

Example 15 with Command

use of org.apache.karaf.shell.api.console.Command 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

Command (org.apache.karaf.shell.api.console.Command)20 HashSet (java.util.HashSet)5 List (java.util.List)4 CommandLine (org.apache.karaf.shell.api.console.CommandLine)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Parser (org.apache.karaf.shell.api.console.Parser)3 StringsCompleter (org.apache.karaf.shell.support.completers.StringsCompleter)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintStream (java.io.PrintStream)2 LinkedHashMap (java.util.LinkedHashMap)2 TreeMap (java.util.TreeMap)2 Callable (java.util.concurrent.Callable)2 Completer (org.apache.karaf.shell.api.console.Completer)2 Registry (org.apache.karaf.shell.api.console.Registry)2 Session (org.apache.karaf.shell.api.console.Session)2 ActionCommand (org.apache.karaf.shell.impl.action.command.ActionCommand)2 ExitCommand (org.apache.karaf.shell.impl.console.commands.ExitCommand)2 SubShellCommand (org.apache.karaf.shell.impl.console.commands.SubShellCommand)2