Process the command line with CLI in Java
Takeaway: Writing code to parse command line arguments is still necessary sometimes. When you need to examine command line arguments, see how and why you should use your open source Java toolkit and use Command Line Interface.
Writing code to parse command line arguments isn't the most exciting job, but sometimes it's still necessary. The next time you need to examine command line arguments and things get a little complex, whip out your trusty open source Java toolkit, and use Command Line Interface (CLI).
Jakarta Commons hosts the CLI project. 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 you can see from 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.
Delivered each Thursday, our free Java newsletter provides insight and hands-on tips you need to unlock the full potential of this programming language. Automatically sign up today!
Print/View all Posts Comments on this article
|
|
|
|
|
|
|
|
|
|
White Papers, Webcasts, and Downloads
- Tom Davenport Study: Linking decisions and information for organizational performance IBM Tom Davenport's new client study looks at approaches to linking ... Download Now
- Building the Virtualized Enterprise with VMware Iinfrastructure VMware VMware virtualization software has been adopted by over 120,000 enterprise ... Download Now
- Five Steps to Determine When to Virtualize YourServers VMware Thinking of virtualizing the servers at your company? Use this step-by-step guide to determine when's the best time to make your big move. Download Now
- Why Isn't Server Virtualization Saving Us More? A Few Small Changes May Dramatically Increase Your Efficiency VMware Ever wonder why your company isn't saving more from its server virtualization? Making a few small changes could dramatically increase your efficiency. Download Now
- VMware Infrastructure: A Guide to Bottom-Line Benefits VMware Frustrated by the high cost of maintaining or building ever-larger data centers? Get the facts you need to formulate your Virtualization Action Plan. Download Now
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET

