Package org.apache.commons.cli
Class DefaultParser
java.lang.Object
org.apache.commons.cli.DefaultParser
- All Implemented Interfaces:
CommandLineParser
Default parser.
- Since:
- 1.3
- Version:
- $Id: DefaultParser.java 1783175 2017-02-16 07:52:05Z britter $
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected CommandLineThe command-line instance.protected OptionThe last option parsed.protected StringThe token currently processed.protected ListThe required options and groups expected to be found when parsing the command line.protected OptionsThe current options.protected booleanFlag indicating if tokens should no longer be analyzed and simply added as arguments of the command line.protected booleanFlag indicating how unrecognized tokens are handled. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidThrow aMissingArgumentExceptionif the current option didn't receive the number of arguments expected.private voidThrows aMissingOptionExceptionif all of the required options are not present.private StringgetLongPrefix(String token) Search for a prefix that is the long name of an option (-Xmx512m)protected voidhandleConcatenatedOptions(String token) Breakstokeninto its constituent parts using the following algorithm.private voidhandleLongOption(String token) Handles the following tokens: --L --L=V --L V --lprivate voidhandleLongOptionWithEqual(String token) Handles the following tokens: --L=V -L=V --l=V -l=Vprivate voidHandles the following tokens: --L -L --l -lprivate voidhandleOption(Option option) private voidhandleProperties(Properties properties) Sets the values of Options using the values inproperties.private voidhandleShortAndLongOption(String token) Handles the following tokens: -S -SV -S V -S=V -S1S2 -S1S2 V -SV1=V2 -L -LV -L V -L=V -lprivate voidhandleToken(String token) Handle any command line token.private voidhandleUnknownToken(String token) Handles an unknown token.private booleanisArgument(String token) Returns true is the token is a valid argument.private booleanisJavaProperty(String token) Check if the specified token is a Java-like property (-Dkey=value).private booleanisLongOption(String token) Tells if the token looks like a long option.private booleanisNegativeNumber(String token) Check if the token is a negative number.private booleanTells if the token looks like an option.private booleanisShortOption(String token) Tells if the token looks like a short option.Parse the arguments according to the specified options.Parse the arguments according to the specified options.parse(Options options, String[] arguments, Properties properties) Parse the arguments according to the specified options and properties.parse(Options options, String[] arguments, Properties properties, boolean stopAtNonOption) Parse the arguments according to the specified options and properties.private voidupdateRequiredOptions(Option option) Removes the option or its group from the list of expected elements.
-
Field Details
-
cmd
The command-line instance. -
options
The current options. -
stopAtNonOption
protected boolean stopAtNonOptionFlag indicating how unrecognized tokens are handled. true to stop the parsing and add the remaining tokens to the args list. false to throw an exception. -
currentToken
The token currently processed. -
currentOption
The last option parsed. -
skipParsing
protected boolean skipParsingFlag indicating if tokens should no longer be analyzed and simply added as arguments of the command line. -
expectedOpts
The required options and groups expected to be found when parsing the command line.
-
-
Constructor Details
-
DefaultParser
public DefaultParser()
-
-
Method Details
-
parse
Description copied from interface:CommandLineParserParse the arguments according to the specified options.- Specified by:
parsein interfaceCommandLineParser- Parameters:
options- the specified Optionsarguments- the command line arguments- Returns:
- the list of atomic option and value tokens
- Throws:
ParseException- if there are any problems encountered while parsing the command line tokens.
-
parse
public CommandLine parse(Options options, String[] arguments, Properties properties) throws ParseException Parse the arguments according to the specified options and properties.- Parameters:
options- the specified Optionsarguments- the command line argumentsproperties- command line option name-value pairs- Returns:
- the list of atomic option and value tokens
- Throws:
ParseException- if there are any problems encountered while parsing the command line tokens.
-
parse
public CommandLine parse(Options options, String[] arguments, boolean stopAtNonOption) throws ParseException Description copied from interface:CommandLineParserParse the arguments according to the specified options.- Specified by:
parsein interfaceCommandLineParser- Parameters:
options- the specified Optionsarguments- the command line argumentsstopAtNonOption- if true an unrecognized argument stops the parsing and the remaining arguments are added to theCommandLines args list. If false an unrecognized argument triggers a ParseException.- Returns:
- the list of atomic option and value tokens
- Throws:
ParseException- if there are any problems encountered while parsing the command line tokens.
-
parse
public CommandLine parse(Options options, String[] arguments, Properties properties, boolean stopAtNonOption) throws ParseException Parse the arguments according to the specified options and properties.- Parameters:
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 theCommandLines args list. If false an unrecognized argument triggers a ParseException.- Returns:
- the list of atomic option and value tokens
- Throws:
ParseException- if there are any problems encountered while parsing the command line tokens.
-
handleProperties
Sets the values of Options using the values inproperties.- Parameters:
properties- The value properties to be processed.- Throws:
ParseException
-
checkRequiredOptions
Throws aMissingOptionExceptionif all of the required options are not present.- Throws:
MissingOptionException- if any of the required Options are not present.
-
checkRequiredArgs
Throw aMissingArgumentExceptionif the current option didn't receive the number of arguments expected.- Throws:
ParseException
-
handleToken
Handle any command line token.- Parameters:
token- the command line token to handle- Throws:
ParseException
-
isArgument
Returns true is the token is a valid argument.- Parameters:
token-
-
isNegativeNumber
Check if the token is a negative number.- Parameters:
token-
-
isOption
Tells if the token looks like an option.- Parameters:
token-
-
isShortOption
Tells if the token looks like a short option.- Parameters:
token-
-
isLongOption
Tells if the token looks like a long option.- Parameters:
token-
-
handleUnknownToken
Handles an unknown token. If the token starts with a dash an UnrecognizedOptionException is thrown. Otherwise the token is added to the arguments of the command line. If the stopAtNonOption flag is set, this stops the parsing and the remaining tokens are added as-is in the arguments of the command line.- Parameters:
token- the command line token to handle- Throws:
ParseException
-
handleLongOption
Handles the following tokens: --L --L=V --L V --l- Parameters:
token- the command line token to handle- Throws:
ParseException
-
handleLongOptionWithoutEqual
Handles the following tokens: --L -L --l -l- Parameters:
token- the command line token to handle- Throws:
ParseException
-
handleLongOptionWithEqual
Handles the following tokens: --L=V -L=V --l=V -l=V- Parameters:
token- the command line token to handle- Throws:
ParseException
-
handleShortAndLongOption
Handles the following tokens: -S -SV -S V -S=V -S1S2 -S1S2 V -SV1=V2 -L -LV -L V -L=V -l- Parameters:
token- the command line token to handle- Throws:
ParseException
-
getLongPrefix
Search for a prefix that is the long name of an option (-Xmx512m)- Parameters:
token-
-
isJavaProperty
Check if the specified token is a Java-like property (-Dkey=value). -
handleOption
- Throws:
ParseException
-
updateRequiredOptions
Removes the option or its group from the list of expected elements.- Parameters:
option-- Throws:
AlreadySelectedException
-
handleConcatenatedOptions
Breakstokeninto its constituent parts using the following algorithm.- ignore the first character ("-")
- for each remaining character check if an
Optionexists with that id. - if an
Optiondoes exist then add that character prepended with "-" to the list of processed tokens. - if the
Optioncan 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. - if an
Optiondoes NOT exist ANDstopAtNonOptionIS set then add the special token "--" followed by the remaining characters and also the remaining tokens directly to the processed tokens list. - if an
Optiondoes NOT exist ANDstopAtNonOptionIS NOT set then add that character prepended with "-".
- Parameters:
token- The current token to be burst at the first non-Option encountered.- Throws:
ParseException- if there are any problems encountered while parsing the command line token.
-