Several examples from Jakarta Commons Cookbook by Timothy M. O’Brien, O’Reilly, Nov 2004
Example 1: Parsing a Simple Command Line
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.CommandLine;
public static void main(String[] args) throws Exception {
// Create a Parser
CommandLineParser parser = new BasicParser( );
Options options = new Options( );
options.addOption(”h”, “help”, false, “Print this usage information”);
options.addOption(”v”, “verbose”, false, “Print out VERBOSE information” );
options.addOption(”f”, “file”, true, “File to save program output to”);
// Parse the program arguments
CommandLine commandLine = parser.parse( options, args );
// Set the appropriate variables based on supplied options
boolean verbose = false;
String file = “”;
if( commandLine.hasOption(’h') ) {
System.out.println( “Help Message”)
System.exit(0);
}
if( commandLine.hasOption(’v') ) {
verbose = true;
}
if( commandLine.hasOption(’f') ) {
file = commandLine.getOptionValue(’f');
}
}
Example 2: Using OptionGroup
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.CommandLine;
public static void main(String[] args) throws Exception {
// Create a Parser
CommandLineParser parser = new BasicParser( );
Options options = new Options( );
options.addOption(”h”, “help”, false, “Print this usage information”);
options.addOption(”v”, “verbose”, false, “Print out VERBOSE information” );
OptionGroup optionGroup = new OptionGroup( );
optionGroup.addOption( OptionBuilder.hasArg(true).create(’f') );
optionGroup.addOption( OptionBuilder.hasArg(true).create(’m') );
options.addOptionGroup( optionGroup );
// Parse the program arguments
CommandLine commandLine = parser.parse( options, args );
// … do important stuff …
}
Example 3: Print Usage Info
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
public class SomeApp {
private static final String USAGE = “[-h] [-v] [-f <file> | -m <email>]”;
private static final String HEADER =
“SomeApp - A fancy and expensive program, Copyright 2010 Blah.”;
private static final String FOOTER =
“For more instructions, see our website at: http://www.blah123.org”;
public static void main(String[] args) throws Exception {
// Create a Parser
CommandLineParser parser = new BasicParser( );
Options options = new Options( );
options.addOption(”h”, “help”, false, “Print this usage
information”);
options.addOption(”v”, “verbose”, false, “Print out VERBOSE
information” );
OptionGroup optionGroup = new OptionGroup( );
optionGroup.addOption( OptionBuilder.hasArg(true).withArgName(”file”)
.withLongOpt(”file”).create(’f') );
optionGroup.addOption( OptionBuilder.hasArg(true).withArgName(”email”)
.withLongOpt(”email”).create(’m') );
options.addOptionGroup( optionGroup );
// Parse the program arguments
try {
CommandLine commandLine = parser.parse( options, args );
if( commandLine.hasOption(’h') ) {
printUsage( options );
System.exit(0);
}
// … do important stuff …
} catch( Exception e ) {
System.out.println( “You provided bad program arguments!” );
printUsage( options );
System.exit(1);
}
}
private static void printUsage(Options options) {
HelpFormatter helpFormatter = new HelpFormatter( );
helpFormatter.setWidth( 80 );
helpFormatter.printHelp( USAGE, HEADER, options, FOOTER );
}
}
No comments:
Post a Comment