i would like to share some ideas on Apache karaf shell.
karaf shell is based on apache felix gogo.
If you want to create a new command, you have to extends org.apache.karaf.shell.console.AbstractAction or XxxCommandSupport.
public class CatAction extends AbstractAction {
protected Object doExecute() throws Exception {
//todo something here
}
}
and then add to blueprint config, would be something like this:
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.0.0">
<command name="shell/cat">
<action class="org.apache.karaf.shell.commands.CatAction"/>
</command>
</command-bundle>
</blueprint>
it's somewhat a little bit verbose in my opinion.
i would perfer something like this:
@CommandProvider(scope = "shell", name = "ShellCommandProvider", description = "Unix Shell alike commands.")
public class ShellCommandProvider {
@Option(name = "-n", aliases = {"--show-line-number"}, description = "The number the output lines, starting at 1.")
private boolean displayLineNumbers;
@Argument(name = "paths or urls", description = "A list of file paths or urls to display separated by whitespaces (use - for STDIN)", required = true, multiValued = true)
private List paths;
@Command(scope="my_scope", options = { "displayLineNumbers" },arguments={"paths"}, description = "Displays the content of a file or URL.")
public void cat(CommandSession session) throws Exception {
//todo something here
}
@Argument(name = "paths or urls")
private String arg1;
@Command(options = { "displayLineNumbers" },arguments={"arg1"})
public void cat1() throws Exception {
//todo something here
}
}
1. The CommandProvider no need to extends some class or implements some interface, and it groups a set of command.
2. Each method can be a command by annotated with @Command, and opt or arg can be shared by different commands.
3. The scope specified at @CommandProvider which is shared by all the commands within this provider. and also each command can override the scope.
i.e. options = { "displayLineNumbers" }, it ref the opt by the field name. and also the same for arguments. the order indicate the index of the opt or arg.
arguments = { "arg1","arg2" }: means the command will accept the args in order: arg1 arg2
finally, add to blueprint config:
<blueprint>
<shell:command-provider class="org.apache.karaf.shell.commands.ShellCommandProvider">
<shell:property name="bundleContext" ref="blueprintBundleContext"/>
any standard blueprint elements here
</shell:command-provider>
</blueprint>
thats it.
any ideas are welcome.