November 2004
Intermediate to advanced
400 pages
10h 31m
English
You need to provide the user with a formatted list of available options.
Pass an Options object to a
HelpFormatter
and print a usage message. Example 7-1 creates the
same Options object from Recipe 7.3. If the help option is specified, or if there
is a problem parsing the program arguments, the printUsage()
method is called to print usage information
to System.out.
Example 7-1. Printing usage information with HelpFormatter
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";publicstaticvoidmain(String[] args)throwsException { // Create a Parser CommandLineParser parser =newBasicParser( ); Options options =newOptions( ); 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") ...
Read now
Unlock full access