I was wondering how I could implement an ArgumentCompleter
such that if I complete a full and valid command, then it would begin tab completing for a new command.
I would have assumed it could be constructed doing something like this:
final ConsoleReader consoleReader = new ConsoleReader()
final ArgumentCompleter cyclicalArgument = new ArgumentCompleter();
cyclicalArgument.getCompleters().addAll(Arrays.asList(
new StringsCompleter("foo"),
new StringsCompleter("bar"),
cyclicalArgument));
consoleReader.addCompleter(cyclicalArgument);
consoleReader.readLine();
However right now this stops working after tab completeing the first foo bar
Is anyone familiar enough with the library to tell me how I would go about implementing this? Or is there a known way to do this that I am missing? Also this is using JLine2.
That was quite a task :-)
It is handled by the completer you are using. The
complete()
method of the completer has to use for the search only what comes after the last blank.If you look for example at the
FileNameCompleter
of the library: this is not done at all, so you will find no completion, because the completer searches for<input1> <input2>
and not only for<input2>
:-)You will have to do your own implementation of a completer that is able to find input2.
Additionally the
CompletionHandler
has to append what you found to what you already typed.Here is a basic implementation changing the default
FileNameCompleter
:And here the
complete()
-Method of theCompletionHandler
changing the defaultCandidateListCompletionHandler
: