我正在尝试让JLine进行制表符补全,这样我就可以输入类似以下内容:
commandname --arg1 value1 --arg2 value2我使用了以下代码:
final List<Completor> completors = Arrays.asList(
new SimpleCompletor("commandname "),
new SimpleCompletor("--arg1"),
new SimpleCompletor("--arg2"),
new NullCompletor());
consoleReader.addCompletor(new ArgumentCompletor(completors));但是在我输入value2后,制表符完成就停止了。
(补充问题,我可以使用jline将value1验证为日期吗?)
发布于 2011-10-25 16:31:04
我也遇到了同样的问题,我通过创建自己的类来用jLine完成命令来解决这个问题。我只需要实现我自己的Completor。
我正在开发一个应用程序,该应用程序不仅可以帮助DBA输入命令名,还可以输入参数。我只是为了终端交互而使用jLine,并且我创建了另一个完成器。
我必须向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,以改进向用户显示选项的方式。
完整的代码可以在here上找到。
发布于 2015-03-22 14:33:13
创建2个完成器,然后使用它们来完成任意参数。注意,并不是所有的参数都需要完成。
List<Completer> completors = new LinkedList<>();
// Completes using the filesystem
completors.add(new FileNameCompleter());
// Completes using random words
completors.add(new StringsCompleter("--arg0", "--arg1", "command"));
// Aggregate the above completors
AggregateCompleter aggComp = new AggregateCompleter(completors);
// Parse the buffer line and complete each token
ArgumentCompleter argComp = new ArgumentCompleter(aggComp);
// Don't require all completors to match
argComp.setStrict(false);
// Add it all together
conReader.addCompleter(argComp);发布于 2017-08-29 04:54:44
去掉NullCompletor,你就会得到你想要的东西。NullCompletor确保您的整个命令只有3个单词长。
https://stackoverflow.com/questions/4050068
复制相似问题