November 2004
Intermediate to advanced
400 pages
10h 31m
English
You need to parse a command line with two exclusive options.
Store mutually exclusive Option objects in an
OptionGroup, and add this
OptionGroup to an Options
object using the addOptionGroup( ) method. Assume
you are working with the following program argument specification:
-h, -v,
and -f
<filename>
|
-m <email>. -h and
-v are both optional and only one of
-f or -m can be specified. If
both -m and -f are supplied as
program arguments, an exception is thrown. In the following example,
the -f and -m options are added
to an OptionGroup, which is then added to the
Options object used to parse the program
arguments:
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;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 =newOptionGroup( ); optionGroup.addOption( OptionBuilder.hasArg(true).create('f') ); optionGroup.addOption( OptionBuilder.hasArg(true).create('m') ); options.addOptionGroup( optionGroup ); // Parse the program arguments CommandLine commandLine ...
Read now
Unlock full access