Friday, July 14, 2006

Java: Apache Jakarta Commons CLI Class

While continuing to experiment with Java, I needed something to easily parse command line arguments for CLI programs. Fortunately,  came across the Apache Projects Commons CLI class. This has to be the easiest way to parse command line interfaces I have come across yet. The concept is pretty simple, you have an Options object that contains Option objects. Each Option is one command line option, either single character, single string, or string/value pairs. You pass the Options object and the command line string to a parser, and you can test against the results to see if certain options were passed. Plus it provides a simple helper to print out usage messages based on options. It is a very simple to use interface. Below is my testing code.

package clitest;

import org.apache.commons.cli.*;

public class CliTest {
     public static void main(String[] args) {
          Option opt = new Option("test", "This is a generic option");
          Option message = OptionBuilder.withArgName("message")
                                               .hasArg()
                                               .withDescription("Message to print")
                                               .create("message");
          CommandLineParser parser = new GnuParser();
          Options options = new Options();
          CommandLine line = null;
          HelpFormatter formatter = new HelpFormatter();
                    
          options.addOption(opt);
          options.addOption(message);
          
          if (args.length < 1)
          {
               formatter.printHelp( "clitest", options );
               System.exit(0);
          }
          
          try
          {
               line = parser.parse(options, args);
          }
          catch (Exception e)
          {
               System.err.println( "Parsing failed.  Reason: " + e.getMessage() );
               System.exit(-1);
          }
          
          if (line.hasOption("message"))
               System.out.println(line.getOptionValue("message"));
          
          if (line.hasOption("test"))
          {
               System.out.println("Test parameter passed");
          }
     }

}

2 comments:

Unknown said...

The Apache Jakarta Commons CLI package is intended to assist in the creation of command-line applications. In conjunction with the String manipulation afforded by the Commons Lang package, aspiring developers could potentially create quite sophisticated console applications. This package takes care of the complexities of parsing incoming arguments, validating the arguments, and displaying help information.

Unknown said...

can some one give ideas for this problem....



The Apache Jakarta Commons CLI package is intended to assist in the creation of command-line applications. In conjunction with the String manipulation afforded by the Commons Lang package, aspiring developers could potentially create quite sophisticated console applications. This package takes care of the complexities of parsing incoming arguments, validating the arguments, and displaying help information.