Args Engine - A reusable solution for Command Line Arguments Parsing in Java

12th May 2008


Args 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 times
args-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

Naveen 13th May 2008
Hey,

Nice little utility for command line dwellers. I have couple of comments.

1) Java doc doesn't work for me
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.

Adarsh R 13th May 2008
Thanks for the comment Naveen. If you're generating JavaDoc, you've to use JavaDoc tool that comes with JDK 1.5 or higher.

I had only heard about Apache Commons CLI. But just now had a look at the API! Impressive and comprehensive. Would be really useful for a large scale command line application.

Naveen 13th May 2008
Sorry for using wrong terminology.. When i used IDE, the autocomplete option didn't show description for the method, its parameters.

Adarsh R 13th May 2008
Guess you've not associated the Source with the JAR file you added to your build path. Also, make sure the Source Compliance for the project is 1.5 or higher as Args Engine uses Generics

Hey Adarsh,

Looks nice :). I am yet to use it and give you full and further critics/comments if any.

Precisely, is it like a wrapper for the command line arguments?

Keep rocking maga..

Cheers!

Mark 28th May 2008
Thanks Adarsh, very handy and very compact.


Let me know what you think

Thank you. Please note that comments are moderated and will take some time to show up here.
Name (required)
Email (required but never published)
Website
Your Comments (2000 characters max) Characters left: 2000