JLine example using multi word commands per line
Asked Answered
K

2

5

I can't seem to find an example that uses more than one command per line.

For example, say I want to write a cli similar to cisco ios, where you may have multiple levels of commands on the one line.

eg. first word could be "show", then when you type "show " and hit tab, the next set of options are displayed (the cisco exmaple uses "?" to show the list).

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

I'm thinking of using readCharacter to read one character at a time and then parse the line so far once I see a space.

Has anyone else had any Jline experience with this type of requirement?

Kozak answered 21/6, 2013 at 4:27 Comment(0)
E
7

Using https://github.com/jline/jline2/blob/master/src/test/java/jline/example/Example.java as reference, you might want to try the following. The key idea behind it is to use the AggregateCompleterclass to do all the merging of the options for you.

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

After running the modified Example.java with the above, the output would look like the following.

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> 
Emilioemily answered 9/1, 2014 at 22:58 Comment(0)
M
0

It can be done by using AggregateCompleter as shown below:

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);
}
Munniks answered 11/9, 2017 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.