我遇到了同样的问题,我通过创建自己的类来完成使用 jLine 的命令来解决它。我只需要实现我自己的 Completor。
我正在开发一个应用程序,它可以帮助 DBA 不仅输入命令名称,还输入参数。我将 jLine 仅用于终端交互,并创建了另一个 Completor。
我必须向 Completor 提供完整的语法,这就是我申请的目标。它被称为 Zemucan,托管在 SourceForge 中;此应用程序最初专注于 DB2,但可以合并任何语法。我正在使用的 Completor 示例是:
public final int complete(final String buffer, final int cursor,
@SuppressWarnings("rawtypes") final List candidateRaw) {
final List<String> candidates = candidateRaw;
final String phrase = buffer.substring(0, cursor);
try {
// Analyzes the typed phrase. This is my program: Zemucan.
// ReturnOptions is an object that contains the possible options of the command.
// It can propose complete the command name, or propose options.
final ReturnOptions answer = InterfaceCore.analyzePhrase(phrase);
// The first candidate is the new phrase.
final String complete = answer.getPhrase().toLowerCase();
// Deletes extra spaces.
final String trim = phrase.trim().toLowerCase();
// Compares if they are equal.
if (complete.startsWith(trim)) {
// Takes the difference.
String diff = complete.substring(trim.length());
if (diff.startsWith(" ") && phrase.endsWith(" ")) {
diff = diff.substring(1, diff.length());
}
candidates.add(diff);
} else {
candidates.add("");
}
// There are options or phrases, then add them as
// candidates. There is not a predefined phrase.
candidates.addAll(this.fromArrayToColletion(answer.getPhrases()));
candidates.addAll(this.fromArrayToColletion(answer.getOptions()));
// Adds a dummy option, in order to prevent that
// jLine adds automatically the option as a phrase.
if ((candidates.size() == 2) && (answer.getOptions().length == 1)
&& (answer.getPhrases().length == 0)) {
candidates.add("");
}
} catch (final AbstractZemucanException e) {
String cause = "";
if (e.getCause() != null) {
cause = e.getCause().toString();
}
if (e.getCause() != null) {
final Throwable ex = e.getCause();
}
System.exit(InputReader.ASSISTING_ERROR);
}
return cursor;
这是应用程序的摘录。你可以做一个简单的 Completor,你必须提供一系列选项。最终,您将希望实现自己的 CompletionHandler 以改进向用户呈现选项的方式。
完整的代码可在此处获得。