Find file
Fetching contributors…
Cannot retrieve contributors at this time
39 lines (24 sloc) 2.71 KB

Annotation based options parsing via Apache Karaf

Apache Karaf has a nice little shell and command line parsing framework which uses annotations on fields/properties to perform type safe injection into Java & Scala objects to provide command line parsing, help and a full tab-completion interactive shell with nice colour & help.

Since using Scopt, I've moved various projects to using it instead and I highly recommend it (particularly as it makes it easy to put all your 'commands' into an interactive shell with full tab completion).

For example Scalate now uses it for all its commands; and to provide a full command line shell.

So you can invoke individual classes from a JVM as a single command; or invoke the shell and a sub command (like 'git foo') or have a full interactive shell all from essentially the same simple model of writing a class with annotations for options & arguments.

There's not heaps of documentation yet but there are docs on creating a new shell.

Bear in mind you can use the command line parsing & interactive shell without any OSGi stuff.

Using Karaf command line parsing and shell

Add the following to your pom.xml


Then add the annotations to your class. For example here is a sample Run command from Scalate.

Notice we can use the argument annotations to specify the positional arguments along with optoins (which take a prefix) and specify things like whether they are mandatory or their name and description etc.

If using Scala there is a clash between the gogo 'Option' annotation and the scala.Option class, so I tend to add [this line|] in Scala to alias them

import org.apache.felix.gogo.commands.{Action, Option => option, Argument => argument, Command => command}

Then here is how to create a full shell for your commands along with this commands.index file