OptionParserpublic class DefaultOptionParser extends java.lang.Object implements OptionParser
| Modifier and Type | Field | Description |
|---|---|---|
protected Option |
currentOption |
The last option parsed.
|
protected java.lang.String |
currentToken |
The token currently processed.
|
protected java.util.List<java.lang.Object> |
expectedOpts |
The required options and groups expected to be found when parsing the command line.
|
protected Options |
options |
The current options.
|
protected ParsedOptions |
parsedOptions |
The parsed options instance.
|
protected boolean |
skipParsing |
Flag indicating if tokens should no longer be analyzed and simply added as arguments of the command line.
|
protected boolean |
stopAtNonOption |
Flag indicating how unrecognized tokens are handled.
|
| Constructor | Description |
|---|---|
DefaultOptionParser() |
Creates a new DefaultParser instance with partial matching enabled.
|
DefaultOptionParser(boolean allowPartialMatching) |
Create a new DefaultParser instance with the specified partial matching policy.
|
| Modifier and Type | Method | Description |
|---|---|---|
protected void |
checkRequiredOptions() |
Throws a
MissingOptionException if all of the required options
are not present. |
protected void |
handleConcatenatedOptions(java.lang.String token) |
Breaks
token into its constituent parts
using the following algorithm. |
ParsedOptions |
parse(Options options,
java.lang.String[] arguments) |
Parse the arguments according to the specified options.
|
ParsedOptions |
parse(Options options,
java.lang.String[] arguments,
boolean stopAtNonOption) |
Parse the arguments according to the specified options.
|
ParsedOptions |
parse(Options options,
java.lang.String[] arguments,
java.util.Properties properties) |
Parse the arguments according to the specified options and properties.
|
ParsedOptions |
parse(Options options,
java.lang.String[] arguments,
java.util.Properties properties,
boolean stopAtNonOption) |
Parse the arguments according to the specified options and properties.
|
protected ParsedOptions parsedOptions
protected Options options
protected boolean stopAtNonOption
true to stop
the parsing and add the remaining tokens to the args list.
false to throw an exception.protected java.lang.String currentToken
protected Option currentOption
protected boolean skipParsing
protected java.util.List<java.lang.Object> expectedOpts
public DefaultOptionParser()
Options options = new Options();
options.addOption(new Option("d", "debug", false, "Turn on debug."));
options.addOption(new Option("e", "extract", false, "Turn on extract."));
options.addOption(new Option("o", "option", true, "Turn on option with argument."));
with "partial matching" turned on, -de only matches the
"debug" option. However, with "partial matching" disabled,
-de would enable both debug as well as
extract options.public DefaultOptionParser(boolean allowPartialMatching)
Options options = new Options();
options.addOption(new Option("d", "debug", false, "Turn on debug."));
options.addOption(new Option("e", "extract", false, "Turn on extract."));
options.addOption(new Option("o", "option", true, "Turn on option with argument."));
with "partial matching" turned on, -de only matches the
"debug" option. However, with "partial matching" disabled,
-de would enable both debug as well as
extract options.allowPartialMatching - if partial matching of long options shall be enabledpublic ParsedOptions parse(Options options, java.lang.String[] arguments) throws OptionParserException
OptionParserparse in interface OptionParseroptions - the specified Optionsarguments - the command line argumentsOptionParserException - if there are any problems encountered
while parsing the command line tokenspublic ParsedOptions parse(Options options, java.lang.String[] arguments, java.util.Properties properties) throws OptionParserException
parse in interface OptionParseroptions - the specified Optionsarguments - the command line argumentsproperties - command line option name-value pairsOptionParserException - if there are any problems encountered
while parsing the command line tokenspublic ParsedOptions parse(Options options, java.lang.String[] arguments, boolean stopAtNonOption) throws OptionParserException
OptionParserparse in interface OptionParseroptions - the specified Optionsarguments - the command line argumentsstopAtNonOption - if true an unrecognized argument stops
the parsing and the remaining arguments are added to the
ParsedOptionss args list. If false an unrecognized
argument triggers a ParseException.OptionParserException - if there are any problems encountered
while parsing the command line tokenspublic ParsedOptions parse(Options options, java.lang.String[] arguments, java.util.Properties properties, boolean stopAtNonOption) throws OptionParserException
parse in interface OptionParseroptions - the specified Optionsarguments - the command line argumentsproperties - command line option name-value pairsstopAtNonOption - if true an unrecognized argument stops
the parsing and the remaining arguments are added to the
ParsedOptionss args list. If false an unrecognized
argument triggers a ParseException.OptionParserException - if there are any problems encountered
while parsing the command line tokensprotected void checkRequiredOptions()
throws MissingOptionException
MissingOptionException if all of the required options
are not present.MissingOptionException - if any of the required Options are not presentprotected void handleConcatenatedOptions(java.lang.String token)
throws OptionParserException
token into its constituent parts
using the following algorithm.
Option
exists with that id.Option does exist then add that character
prepended with "-" to the list of processed tokens.Option can have an argument value and there
are remaining characters in the token then add the remaining
characters as a token to the list of processed tokens.Option does NOT exist AND
stopAtNonOption IS set then add the special token
"--" followed by the remaining characters and also
the remaining tokens directly to the processed tokens list.Option does NOT exist AND
stopAtNonOption IS NOT set then add that
character prepended with "-".token - the current token to be burst
at the first non-Option encounteredOptionParserException - if there are any problems encountered
while parsing the command line tokenCopyright © 2008–2018 The Aspectran Project. All rights reserved.