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
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:
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 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.