. A completer is simply a bean which is injected to a command.
Of course to be able to complete it, the command should require an argument.
h2. Command argument
We add an argument to the HelloCommand:
{code}
package org.apache.karaf.shell.samples;
import org.apache.felix.gogo.commands.Command;
import org.apache.felix.gogo.commands.Argument;
import org.apache.karaf.shell.console.OsgiCommandSupport;
@Command(scope = "test", name = "hello", description="Says hello")
public class HelloShellCommand extends OsgiCommandSupport {
@Argument(index = 0, name = "arg", description = "The command argument", required = false, multiValued = false)
String arg = null;
@Override
protected Object doExecute() throws Exception {
System.out.println("Executing Hello command");
return null;
}
}
{code}
The Blueprint configuration file is the same as previously.
h2. Completer bean
A completer is a bean which implements the Completer interface:
{code}
package org.apache.karaf.shell.samples;
import org.apache.karaf.shell.console.completer.StringsCompleter;
import org.apache.karaf.shell.console.Completer;
/**
*
* A very simple completer.
*
*/
public class SimpleCompleter implements Completer {
/**
* @param buffer the beginning string typed by the user
* @param cursor the position of the cursor
* @param candidates the list of completions proposed to the user
*/
public int complete(String buffer, int cursor, List candidates) {
StringsCompleter delegate = new StringsCompleter();
delegate.getStrings().add("one");
delegate.getStrings().add("two");
delegate.getStrings().add("three");
return delegate.complete(buffer, cursor, candidates);
}
}
{code}
h2. Blueprint configuration file
Using Blueprint, you can "inject" the completer linked to your command. The same completer could be used for several commands and a command can have several completers:
{code:lang=xml}
{code}
h2. Test in Karaf
Launch a Karaf instance and run the following command to install the newly created bundle:
{code}
karaf@root> osgi:install -s mvn:org.apache.karaf.shell.samples/shell-sample-commands/1.0-SNAPSHOT
{code}
Let's try running the command:
{code}
karaf@root> test:hello
one two three
{code}