Args Engine - A reusable solution for Command Line Arguments Parsing in Java
12th May 2008Args Engine is a simple command line parsing engine for Java. The library has a single class and can be imported into any project of your choice easily. The only prerequisite, however, is that Java 1.5 or higher is required for the library to work.
This library works for applications which need to parse either (or both) short form (e.g. -h) or long form (e.g. --help) command line options. It also supports parsing valued options with the assumption that the value for an option follows the option itself (as in -out /usr/myhome/result.log).
Downloads
args-1.0.jar ~ Binary 3.21 kB Downloaded 191 timesargs-src-1.0.zip ~ Source 3.79 kB Downloaded 131 times
API Usage
The API of Args Engine has been kept as simple as possible. The following self explanatory listing shows typical usage which should cater to most needs.
// Initiate the arguments engine.
ArgsEngine engine = new ArgsEngine();
// Configure the switches/options. Use true for valued options.
engine.add("-q", "--quiet");
engine.add("-o", "--redirect-output", true);
engine.add("-h", "--help");
// Perform the parsing. The 'args' is the String[] received by main method.
engine.parse(args);
// Start fetching states of switches.
boolean quiet = engine.getBoolean("-q");
if(engine.getBoolean("-o"))
{
// For valued options, use getString.
String redir = engine.getString("-o");
}
// Use getNonOptions to filter out all options.
String[] nonOptions = engine.getNonOptions();
Listing 1: Java code showing the Args Engine in use.
That's about the Args Engine. You are free to post your suggestions/feedback or report any bug. Please use the form below to do so.
What others say
2) If you pass an argument that was not configured, the program throws runtime exception. It would be good if the method is made to throw exception, so that user could catch it. Not sure, if you had chance to look into apache-commons CLI - a command line parser.

