Search in sources :

Example 1 with DefaultParser

use of org.apache.karaf.shell.support.parsing.DefaultParser in project karaf by apache.

the class CommandLineParser method parse.

public static String parse(Session session, String command) {
    StringBuilder parsed = new StringBuilder();
    int pos = 0;
    while (pos < command.length()) {
        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, rem.length());
        parsed.append(cmdParser.preprocess(session, cmdLine));
        int length = cmdLine.getBuffer().length();
        if (length < rem.length()) {
            char ch = rem.charAt(length);
            if (ch == ';' || ch == '|') {
                parsed.append(" ");
                parsed.append(ch);
                parsed.append(" ");
                length++;
            } else if (ch == '\n') {
                parsed.append(ch);
                length++;
            } else {
                throw new IllegalArgumentException("Unrecognized character: '" + ch + "'");
            }
        }
        pos += length;
    }
    return parsed.toString();
}
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 2 with DefaultParser

use of org.apache.karaf.shell.support.parsing.DefaultParser in project karaf by apache.

the class ParsingTest method testDefaultParser.

@Test
public void testDefaultParser() {
    String command = " foo  bar (a +  b) ; another command ";
    String subCmd = " foo  bar (a +  b) ";
    DefaultParser parser = new DefaultParser();
    CommandLine line = parser.parse(null, command, command.length());
    assertEquals(3, line.getArguments().length);
    assertEquals("foo", line.getArguments()[0]);
    assertEquals("bar", line.getArguments()[1]);
    assertEquals("(a +  b)", line.getArguments()[2]);
    assertEquals(subCmd, line.getBuffer());
}
Also used : CommandLine(org.apache.karaf.shell.api.console.CommandLine) DefaultParser(org.apache.karaf.shell.support.parsing.DefaultParser) Test(org.junit.Test)

Example 3 with DefaultParser

use of org.apache.karaf.shell.support.parsing.DefaultParser 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)

Aggregations

CommandLine (org.apache.karaf.shell.api.console.CommandLine)3 DefaultParser (org.apache.karaf.shell.support.parsing.DefaultParser)3 Command (org.apache.karaf.shell.api.console.Command)2 Parser (org.apache.karaf.shell.api.console.Parser)2 GogoParser (org.apache.karaf.shell.support.parsing.GogoParser)2 Test (org.junit.Test)1