2013-06-21 9 views
5

1行に複数のコマンドを使用する例はありません。複数行のコマンドを1行に使用するJLineの例

例えば、私は、cisco iosに似たcliを書きたいとします。ここでは、1行に複数のレベルのコマンドがあるかもしれません。

例えば、最初の単語を「表示」にすると、「表示」と「ヒット」タブを押すと、次のオプションが表示されます(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 

私は、一度に1つの文字を読み、これまでのところ、私はスペースを見たら、行を解析するreadCharacterを使用してと思っています。

このタイプの要件を持つJlineの経験は他に誰もいませんか?

答えて

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> 
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); 
} 
関連する問題