Menu

Home

Paul_A._Rubin

JCLILIB is the Java Command Line Interface LIBrary (falls trippingly off the tongue, doesn't it?), a small library package in Java 7 to facilitate accessing command line arguments in a Java program. The library is available under the Apache 2.0 license. It consists of two classes, OptionManager and FileLister.

OptionManager

OptionManager parses the command line (typically the args vector in a Java main program) and extracts arguments according to specifications you declare in the calling program. Each argument is declared with a default value (can be null) and a return class (Object, String, Boolean, Integer, Long, or Double). For return types other than Object and String, OptionManager will attempt to convert the string representation from the command line into the appropriate class.

OptionManager understands the following types of arguments:

  • a single key (can be any string, such as "-h", "H" or "!!help!!") that toggles a parameter from its default value to a specified alternate value;
  • a pair of consecutive arguments, the first being a key and the second a value (such as "dir some_directory");
  • a key/value pair separated by a specified connector (such as "file=my_file" or "file::my_file"), with the connector an arbitrary string; and
  • a parameter specified by its position (indexing starts at 0) among those arguments not matched by any of the previous methods.

In addition, you can instruct OptionManager to allow a key to be repeated (such as "name=Joe ... name=Tom ... name=Jane", where the repetitions need not be contiguous). For repeated options, the matched values are returned as a list of objects. Keys may be declared as case-sensitive or case-insensitive. Any surplus command line arguments (not recognized as keys, values or positional arguments) are returned in an array of strings.

FileLister

FileLister provides static methods that take two arguments, a directory (String) and list of file specifications (String[]), and returns a vector of all files in the named directory that match any of the file specifications. The return value can be either String[] or File[]. FileLister understands glob-style wildcards.

The code base includes a set of JUnit tests and a small test program that you can use to verify the features of OptionManager.

You might also want to take a look at the Apache Commons CLI library.

Project Admins: