public abstract class Options extends CommandLineParser
CommandLineParser
class and is used
to impose the following syntactical restrictions on the command line
arguments.
The options defined by this class are:
-h | If this option is present then the user wants to see the help for the command line program. |
To support the options that are unique to your program you will need to
extend this class to support those options. The bulk of the extension can
be achieved by adding option definitions for your
new options in the constructor of your derived class. The
option definitions tell the CommandLineParser.parse
method
which options are valid and what type
of values (if any) they require. If the
option definiton classes defined in the Options
class don't meet your needs then you can extend OptionDef
(or one of it's derived classes) to create an option defintion that
will meet your needs. The following example
creates two flag options and an integer option.
// Declare the names or the options.
final public static String VERBOSE = "-v";
final public static String DEBUG = "-d";
final public static String THREADS = "-threads";
// Add option definitions for this class.
addOptionDef(new FlagOptionDef(VERBOSE, getStrFromBundle("option.desc.verbose")));
addOptionDef(new FlagOptionDef(DEBUG, getStrFromBundle("option.desc.debug")));
addOptionDef(new IntOptionDef(THREADS, 5, getStrFromBundle("option.desc.threads")));
Modifier and Type | Class and Description |
---|---|
protected class |
Options.DateOptionDef
Definition of a date option.
|
protected class |
Options.DecimalOptionDef
Definition of a BigDecimal option.
|
protected class |
Options.FlagOptionDef
Definition of a flag option.
|
protected class |
Options.IntOptionDef
Definition of a integer option.
|
protected class |
Options.OptionDef
Abstract class used to define an option.
|
protected class |
Options.StringOptionDef
Definition of a string option.
|
CommandLineParser.InvalidOptionsException, CommandLineParser.NoSuchOptionException, CommandLineParser.OptionDataException, CommandLineParser.OptionException, CommandLineParser.ValueOutOfBoundsException
Modifier and Type | Field and Description |
---|---|
static String |
HELP
The help option.
|
ALL
Modifier | Constructor and Description |
---|---|
protected |
Options(String[] args)
This constructor will default to no leading tokens and an
indefinite number of trailing tokens.
You cannot all this constructor directly because you must derive from this class. |
protected |
Options(String[] args,
int numLeadToks,
int numTrailToks)
Allows the caller to specifiy the number of leading tokens
and the number of trailing tokens>.
You cannot all this constructor directly because you must derive from this class. |
Modifier and Type | Method and Description |
---|---|
protected void |
addOptionDef(Options.OptionDef optDef)
Use this method to add option defintions for all of the
options that will be supported in your derived class.
|
BigDecimal |
getBigDecimal(String optName)
Return the 1st value for the option as a BigDecimal.
|
boolean |
getBoolean(String optName)
Return the 1st value for the option as a boolean.
|
Date |
getDate(String optName)
Return the 1st value for the option as a date.
|
String |
getHelp()
Get the help string that describes the syntax required by your options
class and a desciption of each option used by your class.
|
Integer |
getInteger(String optName)
Return the 1st value for the option as an integer.
|
protected Options.OptionDef |
getOptionDef(String optName)
Helper method used to retrieve an option definition for a
particular option.
|
protected String[] |
getOptValues(String optName)
Helper method used to get the values that were specified on the
command line for the option.
|
String |
getString(String optName)
Return the 1st value for the option as a string.
|
String[] |
getStrings(String optName)
Return the values for the option as an array of strings.
|
protected abstract String |
getSyntaxHelp()
Get the help string that describes the syntax required by your options
class.
|
boolean |
isHelp()
Return true if the help option was contained in the command
line or the command line is empty.
|
protected boolean |
storeAllTokensAsLeadingTokens()
Override this method if you want to change the default behavior that
when a command line only contains tokens (no options) then the tokens
will be considered leading tokens and there will be no trailing tokens.
|
String |
toString()
Convert this class to a string that will contain the following:
The arguments passed to the constructor
The leading tokens
For each option, the values associated with that option
The trailing tokens
The option definitions
|
protected void |
validateLeadingTokens(ArrayList tokens)
Validate the number of leading tokens passed in the command
line.
|
protected void |
validateOption(String option,
ArrayList values,
boolean lastOption)
Validate that the option has the correct number of values and that
values are of the correct data type.
|
protected void |
validateRequiredParameters()
Ensure that all required options were present on the command line.
|
protected void |
validateTrailingTokens(ArrayList tokens,
int numLastOptionArgs)
Validate the number of trailing tokens passed in the command
line.
|
constructErrMsg, constructErrMsg, containsOption, createParser, getArgs, getLeadingToken, getLeadingTokens, getNumLeadingTokens, getNumOptions, getNumTrailingTokens, getNumValues, getStrFromBundle, getStrFromBundle, getTrailingToken, getTrailingTokens, getValue, getValues, getValues, parse, postParse, strArrayToString
public static final String HELP
protected Options(String[] args) throws CommandLineParser.InvalidOptionsException
CommandLineParse.parse
method after your
constructor has finished all initialization. This is because
the CommandLineParser.parse
method calls the methods
overriden in the Options class which depend on the
option definitions defined in the constructor of your
derived class. All constructor initialization must be complete for
the CommandLineParser.parse
method to work correctly.
args
- The array of command line arguments passed to the program.InvalidOptionsException
- An error was encountered when
parsing the command line arguments. The error could result from one
of the conditions.
CommandLineParser.InvalidOptionsException
protected Options(String[] args, int numLeadToks, int numTrailToks) throws CommandLineParser.InvalidOptionsException
CommandLineParse.parse
method after your
constructor has finished all initialization. This is because
the CommandLineParser.parse
method calls the methods
overriden in the Options class which depend on the
option definitions defined in the constructor of your
derived class. All constructor initialization must be complete for
the CommandLineParser.parse
method to work correctly.
args
- The array of command line arguments passed to the program.numLeadToks
- The number of tokens that can exist at the
beginning (before any options) of the command line. A value
of ALL implies there can be an indefinite number of leading tokens.numTrailToks
- The number of tokens that can exist at the end
(after any options) of the command line. A value of ALL
implies there can be an indefinite number of trailing tokens.InvalidOptionsException
- An error was encountered when
parsing the command line arguments. The error could result from one
of the conditions.
CommandLineParser.InvalidOptionsException
public String getString(String optName) throws CommandLineParser.NoSuchOptionException, CommandLineParser.OptionDataException
optName
- The option to return the string value for.NoSuchOptionException
- There is no option defintion for the
specified option.OptionDataException
- There is no value or default value
for the option.CommandLineParser.NoSuchOptionException
CommandLineParser.OptionDataException
public boolean getBoolean(String optName) throws CommandLineParser.NoSuchOptionException, CommandLineParser.OptionDataException
optName
- The option to return the boolean value for.NoSuchOptionException
- There is no option defintion for the
specified option.OptionDataException
- There is no value or default value
for the option or the value (or default value) could not be converted
to a boolean value.CommandLineParser.NoSuchOptionException
CommandLineParser.OptionDataException
public Integer getInteger(String optName) throws CommandLineParser.NoSuchOptionException, CommandLineParser.OptionDataException
optName
- The option to return the boolean value for.NoSuchOptionException
- There is no option defintion for the
specified option.OptionDataException
- There is no value or default value
for the option or the value (or default value) could not be converted
to an integer value.CommandLineParser.NoSuchOptionException
CommandLineParser.OptionDataException
public BigDecimal getBigDecimal(String optName) throws CommandLineParser.NoSuchOptionException, CommandLineParser.OptionDataException
optName
- The option to return the BigDecimal value for.NoSuchOptionException
- There is no option defintion for the
specified option.OptionDataException
- There is no value or default value
for the option or the value (or default value) could not be converted
to a BigDecimal value.CommandLineParser.NoSuchOptionException
CommandLineParser.OptionDataException
public Date getDate(String optName) throws CommandLineParser.NoSuchOptionException, CommandLineParser.OptionDataException
optName
- The option to return the date value for.NoSuchOptionException
- There is no option defintion for the
specified option.OptionDataException
- There is no value or default value
for the option or the value (or default value) could not be converted
to a date value.CommandLineParser.NoSuchOptionException
CommandLineParser.OptionDataException
public String[] getStrings(String optName) throws CommandLineParser.NoSuchOptionException, CommandLineParser.OptionDataException
values
- The array of values retrieved from the command
line for the option.NoSuchOptionException
- There is no option defintion for the
specified option.OptionDataException
- There are no values or default values
for the option.CommandLineParser.NoSuchOptionException
CommandLineParser.OptionDataException
public String toString()
toString
in class CommandLineParser
public boolean isHelp()
isHelp
in class CommandLineParser
public String getHelp()
getHelp
in class CommandLineParser
protected abstract String getSyntaxHelp()
protected void validateLeadingTokens(ArrayList tokens) throws CommandLineParser.InvalidOptionsException
validateLeadingTokens
in class CommandLineParser
tokens
- The list of tokens that appear at the beginning
of the command line. The list could be empty.InvalidOptionsException
- There is an error with the number
of leading tokens.CommandLineParser.InvalidOptionsException
protected void validateOption(String option, ArrayList values, boolean lastOption) throws CommandLineParser.InvalidOptionsException
validateOption
in class CommandLineParser
option
- The name of the option to validate. This name will
include the leading "-" (i.e. "-verbose").values
- The list of values associated with this option. The
list could be empty.lastOption
- This flag will be true if this is the "last" option
contained in the command line.InvalidOptionsException
- The option has not been defined or
there is an error with the number of "values" or a particular value
is invalid.CommandLineParser.InvalidOptionsException
protected void validateTrailingTokens(ArrayList tokens, int numLastOptionArgs) throws CommandLineParser.InvalidOptionsException
validateTrailingTokens
in class CommandLineParser
tokens
- The list of tokens that appear at the end
of the command line. The list could be empty.numLastOptionArgs
- The number of arguments "used" by the
last option.InvalidOptionsException
- There is an error with the number
of trailing tokens.CommandLineParser.InvalidOptionsException
protected void validateRequiredParameters() throws CommandLineParser.InvalidOptionsException
validateRequiredParameters
in class CommandLineParser
InvalidOptionsException
- A required option was not present on
the command line.CommandLineParser.InvalidOptionsException
protected boolean storeAllTokensAsLeadingTokens()
storeAllTokensAsLeadingTokens
in class CommandLineParser
protected final void addOptionDef(Options.OptionDef optDef) throws CommandLineParser.InvalidOptionsException
optDef
- The option definiton of an option that will
supported in your derived class.InvalidOptionsException
- The value(s) specified for the
option are invalid.CommandLineParser.InvalidOptionsException
protected final Options.OptionDef getOptionDef(String optName) throws CommandLineParser.NoSuchOptionException
optName
- The option to retieve the option defintion for.
This name must include the leading "-".
NoSuchOptionException
- There is no option defintion for the
specified option.CommandLineParser.NoSuchOptionException
protected String[] getOptValues(String optName)
optName
- The name of the option to return the values for.Copyright © 2003, 2023, Oracle and/or its affiliates.