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();
}
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());
}
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;
}
}
}
Aggregations