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?
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 AggregateCompleter
class 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With