JewelCLI: a simple CLI interface for Java

I’ve been working on a command-line interface for my PhD work and was initially doing the parsing of arguments by hand which was becoming extremely unwieldy so I decided it was time to jump into the OpenSource waters and see what was available.  I took a look at many CLI libraries (you can see a whole list of them here from JOpt) but settled on JewelCLI due to it’s simple, powerful interace and ease of use.  It might not be as powerful as Apache’s Commons CLI or CLI2 but it fit my needs perfectly.

Not to steal any thunder from it’s website, but as a quick example, all you have to do to use it (other than managing the dependency through Maven or whatever your tool of choice maybe) is three steps.

First, create an interface class.

public interface MyInterface {
  public String getFilename();
}

Second, annotate that interface class with the JewelCLI annotations.

public interface MyInterface {
  @Option
  public String getFilename();
}

Third, use JewelCLI’s factory methods to parse the command line arguments.

...
try {
  MyInterface myInterface = CliFactory.parseArguments(MyInterface.class, argsFromMain);
  String filename = myInterface.getFilename();
  ...
} catch (ArgumentValidationException e) {
  System.out.println(e.getMessage());
}

As a bonus, it even creates it’s own help output from the annotations so you don’t even have to write that (it appears as the message to the exception)!  Overall, I’m quite impressed with JewelCLI; it takes care of the mundane and lets me focus on what is important in my programming.

Leave a Reply

Your email address will not be published. Required fields are marked *