Presents your JAVA E-NEWSLETTER for December 4, 2003 <-------------------------------------------> PROCESS THE COMMAND LINE WITH CLI Writing code to parse command line arguments isn't the most exciting job, but even in these times of application containers and Web services, sometimes it's still necessary. But the next time you need to examine command line arguments and things start to get a little complex, whip out your trusty open source Java toolkit, and use Command Line Interface (CLI). CLI is a project that's hosted by Jakarta Commons. While it's overkill if you only have one or two arguments, it's essential if your application takes most of its settings from the command line. To use CLI, you need to create an instance of the Options class: Options opt = new Options(); With this instance of Options, you define the command line arguments that your application will accept. One way to do this is by using the addOption() method of the Options class. Call this method once for each option that your application can accept. opt.addOption("h", false, "Print help for this application"); opt.addOption("u", true, "The username to use"); opt.addOption("dsn", true, "The data source to use"); Once you define your classes' arguments, create a CommandLineParser, and parse the String array that was passed to your main method. BasicParser parser = new BasicParser(); CommandLine cl = parser.parse(opt, args); Now that all of the arguments are parsed, you can examine the CommandLine instance returned by the parser to determine what arguments and values were supplied by the user. if ( cl.hasOption('h') ) { HelpFormatter f = new HelpFormatter(); f.printHelp("OptionsTip", opt); } else { System.out.println(cl.getOptionValue("u")); System.out.println(cl.getOptionValue("dsn")); } As shown above, you can use the HelpFormatter class to automatically generate usage information for your program. Here's the entire code: // OptionsTip.java import org.apache.commons.cli.BasicParser; import org.apache.commons.cli.Options; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.ParseException; public class OptionsTip { public static void main(String args[]) { try { Options opt = new Options(); opt.addOption("h", false, "Print help for this application"); opt.addOption("u", true, "The username to use"); opt.addOption("dsn", true, "The data source to use"); BasicParser parser = new BasicParser(); CommandLine cl = parser.parse(opt, args); if ( cl.hasOption('h') ) { HelpFormatter f = new HelpFormatter(); f.printHelp("OptionsTip", opt); } else { System.out.println(cl.getOptionValue("u")); System.out.println(cl.getOptionValue("dsn")); } } catch (ParseException e) { e.printStackTrace(); } } } CLI takes a tedious chore off your hands and makes parsing command line arguments a simple task. For more information, check out the documentation. http://jakarta.apache.org/commons/cli/ David Petersheim is the Director of Application Development with Genscape, Inc. He designs and develops server-side applications to acquire and process real-time energy data. ----------------------------------------