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