public class DefaultParser extends java.lang.Object implements CommandLineParser
Modifier and Type | Field and Description |
---|---|
protected CommandLine |
cmd
The command-line instance.
|
protected Option |
currentOption
The last option parsed.
|
protected java.lang.String |
currentToken
The token currently processed.
|
protected java.util.List |
expectedOpts
The required options and groups expected to be found when parsing the command line.
|
protected Options |
options
The current options.
|
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 and Description |
---|
DefaultParser() |
Modifier and Type | Method and Description |
---|---|
private void |
checkRequiredArgs()
Throw a
MissingArgumentException if the current option
didn't receive the number of arguments expected. |
private void |
checkRequiredOptions()
Throws a
MissingOptionException if all of the required options
are not present. |
private java.lang.String |
getLongPrefix(java.lang.String token)
Search for a prefix that is the long name of an option (-Xmx512m)
|
protected void |
handleConcatenatedOptions(java.lang.String token)
Breaks
token into its constituent parts
using the following algorithm. |
private void |
handleLongOption(java.lang.String token)
Handles the following tokens:
--L
--L=V
--L V
--l
|
private void |
handleLongOptionWithEqual(java.lang.String token)
Handles the following tokens:
--L=V
-L=V
--l=V
-l=V
|
private void |
handleLongOptionWithoutEqual(java.lang.String token)
Handles the following tokens:
--L
-L
--l
-l
|
private void |
handleOption(Option option) |
private void |
handleProperties(java.util.Properties properties)
Sets the values of Options using the values in
properties . |
private void |
handleShortAndLongOption(java.lang.String token)
Handles the following tokens:
-S
-SV
-S V
-S=V
-S1S2
-S1S2 V
-SV1=V2
-L
-LV
-L V
-L=V
-l
|
private void |
handleToken(java.lang.String token)
Handle any command line token.
|
private void |
handleUnknownToken(java.lang.String token)
Handles an unknown token.
|
private boolean |
isArgument(java.lang.String token)
Returns true is the token is a valid argument.
|
private boolean |
isJavaProperty(java.lang.String token)
Check if the specified token is a Java-like property (-Dkey=value).
|
private boolean |
isLongOption(java.lang.String token)
Tells if the token looks like a long option.
|
private boolean |
isNegativeNumber(java.lang.String token)
Check if the token is a negative number.
|
private boolean |
isOption(java.lang.String token)
Tells if the token looks like an option.
|
private boolean |
isShortOption(java.lang.String token)
Tells if the token looks like a short option.
|
CommandLine |
parse(Options options,
java.lang.String[] arguments)
Parse the arguments according to the specified options.
|
CommandLine |
parse(Options options,
java.lang.String[] arguments,
boolean stopAtNonOption)
Parse the arguments according to the specified options.
|
CommandLine |
parse(Options options,
java.lang.String[] arguments,
java.util.Properties properties)
Parse the arguments according to the specified options and properties.
|
CommandLine |
parse(Options options,
java.lang.String[] arguments,
java.util.Properties properties,
boolean stopAtNonOption)
Parse the arguments according to the specified options and properties.
|
private void |
updateRequiredOptions(Option option)
Removes the option or its group from the list of expected elements.
|
protected CommandLine cmd
protected Options options
protected boolean stopAtNonOption
protected java.lang.String currentToken
protected Option currentOption
protected boolean skipParsing
protected java.util.List expectedOpts
public CommandLine parse(Options options, java.lang.String[] arguments) throws ParseException
CommandLineParser
parse
in interface CommandLineParser
options
- the specified Optionsarguments
- the command line argumentsParseException
- if there are any problems encountered
while parsing the command line tokens.public CommandLine parse(Options options, java.lang.String[] arguments, java.util.Properties properties) throws ParseException
options
- the specified Optionsarguments
- the command line argumentsproperties
- command line option name-value pairsParseException
- if there are any problems encountered
while parsing the command line tokens.public CommandLine parse(Options options, java.lang.String[] arguments, boolean stopAtNonOption) throws ParseException
CommandLineParser
parse
in interface CommandLineParser
options
- the specified Optionsarguments
- the command line argumentsstopAtNonOption
- if true an unrecognized argument stops
the parsing and the remaining arguments are added to the
CommandLine
s args list. If false an unrecognized
argument triggers a ParseException.ParseException
- if there are any problems encountered
while parsing the command line tokens.public CommandLine parse(Options options, java.lang.String[] arguments, java.util.Properties properties, boolean stopAtNonOption) throws ParseException
options
- 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
CommandLine
s args list. If false an unrecognized
argument triggers a ParseException.ParseException
- if there are any problems encountered
while parsing the command line tokens.private void handleProperties(java.util.Properties properties) throws ParseException
properties
.properties
- The value properties to be processed.ParseException
private void checkRequiredOptions() throws MissingOptionException
MissingOptionException
if all of the required options
are not present.MissingOptionException
- if any of the required Options
are not present.private void checkRequiredArgs() throws ParseException
MissingArgumentException
if the current option
didn't receive the number of arguments expected.ParseException
private void handleToken(java.lang.String token) throws ParseException
token
- the command line token to handleParseException
private boolean isArgument(java.lang.String token)
token
- private boolean isNegativeNumber(java.lang.String token)
token
- private boolean isOption(java.lang.String token)
token
- private boolean isShortOption(java.lang.String token)
token
- private boolean isLongOption(java.lang.String token)
token
- private void handleUnknownToken(java.lang.String token) throws ParseException
token
- the command line token to handleParseException
private void handleLongOption(java.lang.String token) throws ParseException
token
- the command line token to handleParseException
private void handleLongOptionWithoutEqual(java.lang.String token) throws ParseException
token
- the command line token to handleParseException
private void handleLongOptionWithEqual(java.lang.String token) throws ParseException
token
- the command line token to handleParseException
private void handleShortAndLongOption(java.lang.String token) throws ParseException
token
- the command line token to handleParseException
private java.lang.String getLongPrefix(java.lang.String token)
token
- private boolean isJavaProperty(java.lang.String token)
private void handleOption(Option option) throws ParseException
ParseException
private void updateRequiredOptions(Option option) throws AlreadySelectedException
option
- AlreadySelectedException
protected void handleConcatenatedOptions(java.lang.String token) throws ParseException
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 encountered.ParseException
- if there are any problems encountered
while parsing the command line token.