我似乎无法找到每行使用多个命令的示例。
例如,假设我想写一个类似于cisco ios的cli,你可能在一行上有多个级别的命令。
例如。第一个单词可以是“show”,然后当你输入“show”并点击tab时,会显示下一组选项(cisco exmaple使用“?”来显示列表)。
eg:
gw1#show ?
aaa Show AAA values
access-expression List access expression
access-lists List access lists
accounting Accounting data for active sessions
adjacency Adjacent nodes
..
gw1#show ip ?
access-lists List IP access lists
accounting The active IP accounting database
admission Network Admission Control information
aliases IP alias table
arp IP ARP table
..
gw1#show ip interface ?
ATM ATM interface
Async Async interface
BVI Bridge-Group Virtual Interface
CDMA-Ix CDMA Ix interface
..
gw1#show ip interface
我正在考虑使用readCharacter一次读取一个字符,然后在看到空格后解析到目前为止的行。
有没有其他人对此类要求有任何Jline经验?
答案 0 :(得分:7)
使用https://github.com/jline/jline2/blob/master/src/test/java/jline/example/Example.java作为参考,您可能需要尝试以下操作。其背后的关键思想是使用AggregateCompleter
类为您完成所有选项的合并。
List<Completer> completors = new LinkedList<Completer>();
completors.add(
new AggregateCompleter(
new ArgumentCompleter(new StringsCompleter("show"), new NullCompleter()),
new ArgumentCompleter(new StringsCompleter("show"), new StringsCompleter("aaa", "access-expression", "access-lists", "accounting", "adjancey"), new NullCompleter()),
new ArgumentCompleter(new StringsCompleter("show"), new StringsCompleter("ip"), new StringsCompleter("access-lists", "accounting", "admission", "aliases", "arp"), new NullCompleter()),
new ArgumentCompleter(new StringsCompleter("show"), new StringsCompleter("ip"), new StringsCompleter("interface"), new StringsCompleter("ATM", "Async", "BVI"), new NullCompleter())
)
);
for (Completer c : completors) {
reader.addCompleter(c);
}
使用上面的内容运行修改后的Example.java后,输出如下所示。
prompt> show
show
prompt> show
aaa access-expression access-lists accounting adjancey ip
prompt> show ip
ip
prompt> show ip
access-lists accounting admission aliases arp interface
prompt> show ip interface
ATM Async BVI
prompt> show ip interface A
ATM Async
prompt> show ip interface A
ATM Async
prompt> show ip interface ATM
======>"show ip interface ATM "
prompt>
答案 1 :(得分:0)
可以使用AggregateCompleter完成,如下所示:
ConsoleReader reader = new MultiWordConsoleReader();
List<Completer> completers = new ArrayList<Completer>();
completors.add(new AggregateCompleter(new StringsCompleter("show"), new StringsCompleter("aaa", "access"), new NullCompleter()));
completors.add(new AggregateCompleter(new StringsCompleter("show"), new StringsCompleter("ip"), new StringsCompleter("accounting", "arp"), new NullCompleter()));
for (Completer c : completors) {
reader.addCompleter(c);
}