Coring: COmmand-line option paRsiNG for Java

About

Coring is a simple command line parser for Java. The goal was to create a library that can be used to specify command line options in few lines, without much extensions. If you need more powerful library or you simply do not like this one, consider one of many alternatives (some are listed below).

Example

The following code defines three command line options (of type Boolean, Integer, and list of integers):

@OptHelp("Help message") public class OptExample { @OptHelp("Help message for boolean argument") final BooleanOpt boolOpt = new BooleanOpt("ex-boolean", "z"); @OptHelp("Help message for int argument") final IntegerOpt intOpt = new IntegerOpt("ex-int", "i", 84); @OptRequired @OptHelp("Help message for list of integers argument") final IntegerLst intLst = new IntegerLst("ex-int-list"); }

The following code illustrates recommended use pattern:

public static void main(String[] args) { OptExample options = new OptExample(); OptParserStatus status = OptParser.helpOrParse(options, args); if (status.isSuccessfullyParsed()) { System.out.println(options.boolOpt.val()); System.out.println(options.intOpt.val()); System.out.println(options.intLst.val()); } else if (status.isError()) { System.out.println(status.getExceptionMsg()); } }
A few runs (and corresponding outputs) are shown below:
$ java -cp .:coring-1.4.jar OptExample -h Help message -h, --help Print this help message -z, --ex-boolean Help message for boolean argument -i, --ex-int INTEGER Help message for int argument --ex-int-list* INTEGER_0,INTEGER_1,...,INTEGER_N Help message for list of integers argument * Required arguments.
$ java -cp .:coring-1.4.jar OptExample --ex-boolean --ex-int 5 --ex-int-list is required.
$ java -cp .:coring-1.4.jar OptExample --ex-boolean --ex-int 5 --ex-int-list 1,2,3 true 5 [1, 2, 3]

Features

  • Fields that describe an option must be final
  • Constructor requires only the name for the option (optionally, one can specify short name and/or default value)
  • Help option (-h and --help) is automatically included
  • @OptHelp is an optional annotation that can be used to describe an option or class
  • @OptRequired is an optional annotation that marks an option that must be provided on the command line

Download

Alternatives

There are a number of alternatives that offer different syntax and features. Consider:

Acknowledgments

I would like to thank Yu Lin, Rohan Sharma and Darko Marinov for providing feedback on the initial version of the library.