Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JLine example using multi word commands per line

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?

like image 267
user2507605 Avatar asked Jun 21 '13 04:06

user2507605


1 Answers

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> 
like image 78
Doraemon Avatar answered Nov 05 '22 06:11

Doraemon