Package org.apache.commons.cli
Class DefaultParser
- java.lang.Object
-
- org.apache.commons.cli.DefaultParser
-
- All Implemented Interfaces:
CommandLineParser
public class DefaultParser extends java.lang.Object implements CommandLineParser
Default parser.- Since:
- 1.3
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDefaultParser.BuilderA nested builder class to createDefaultParserinstances using descriptive methods.
-
Field Summary
Fields Modifier and Type Field Description private booleanallowPartialMatchingFlag indicating if partial matching of long options is supported.protected CommandLinecmdThe command-line instance.protected OptioncurrentOptionThe last option parsed.protected java.lang.StringcurrentTokenThe token currently processed.protected java.util.ListexpectedOptsThe required options and groups expected to be found when parsing the command line.protected OptionsoptionsThe current options.protected booleanskipParsingFlag indicating if tokens should no longer be analyzed and simply added as arguments of the command line.protected booleanstopAtNonOptionFlag indicating how unrecognized tokens are handled.private java.lang.BooleanstripLeadingAndTrailingQuotesFlag indicating if balanced leading and trailing double quotes should be stripped from option arguments.
-
Constructor Summary
Constructors Modifier Constructor Description DefaultParser()Creates a new DefaultParser instance with partial matching enabled.DefaultParser(boolean allowPartialMatching)Create a new DefaultParser instance with the specified partial matching policy.privateDefaultParser(boolean allowPartialMatching, java.lang.Boolean stripLeadingAndTrailingQuotes)Creates a new DefaultParser instance with the specified partial matching and quote stripping policy.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static DefaultParser.Builderbuilder()Creates a newDefaultParser.Builderto create anDefaultParserusing descriptive methods.private voidcheckRequiredArgs()Throws aMissingArgumentExceptionif the current option didn't receive the number of arguments expected.protected voidcheckRequiredOptions()Throws aMissingOptionExceptionif all of the required options are not present.private java.lang.StringgetLongPrefix(java.lang.String token)Searches for a prefix that is the long name of an option (-Xmx512m)private java.util.List<java.lang.String>getMatchingLongOptions(java.lang.String token)Gets a list of matching option strings for the given token, depending on the selected partial matching policy.protected voidhandleConcatenatedOptions(java.lang.String token)Breakstokeninto its constituent parts using the following algorithm.private voidhandleLongOption(java.lang.String token)Handles the following tokens: --L --L=V --L V --lprivate voidhandleLongOptionWithEqual(java.lang.String token)Handles the following tokens: --L=V -L=V --l=V -l=Vprivate voidhandleLongOptionWithoutEqual(java.lang.String token)Handles the following tokens: --L -L --l -lprivate voidhandleOption(Option option)private voidhandleProperties(java.util.Properties properties)Sets the values of Options using the values inproperties.private voidhandleShortAndLongOption(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 -lprivate voidhandleToken(java.lang.String token)Handles any command line token.private voidhandleUnknownToken(java.lang.String token)Handles an unknown token.private booleanisArgument(java.lang.String token)Tests if the token is a valid argument.private booleanisJavaProperty(java.lang.String token)Tests if the specified token is a Java-like property (-Dkey=value).private booleanisLongOption(java.lang.String token)Tests if the token looks like a long option.private booleanisNegativeNumber(java.lang.String token)Tests if the token is a negative number.private booleanisOption(java.lang.String token)Tests if the token looks like an option.private booleanisShortOption(java.lang.String token)Tests if the token looks like a short option.CommandLineparse(Options options, java.lang.String[] arguments)Parses the arguments according to the specified options.CommandLineparse(Options options, java.lang.String[] arguments, boolean stopAtNonOption)Parses the arguments according to the specified options.CommandLineparse(Options options, java.lang.String[] arguments, java.util.Properties properties)Parses the arguments according to the specified options and properties.CommandLineparse(Options options, java.lang.String[] arguments, java.util.Properties properties, boolean stopAtNonOption)Parses the arguments according to the specified options and properties.private java.lang.StringstripLeadingAndTrailingQuotesDefaultOff(java.lang.String token)Strips balanced leading and trailing quotes if the stripLeadingAndTrailingQuotes is set If stripLeadingAndTrailingQuotes is null, then do not stripprivate java.lang.StringstripLeadingAndTrailingQuotesDefaultOn(java.lang.String token)Strips balanced leading and trailing quotes if the stripLeadingAndTrailingQuotes is set If stripLeadingAndTrailingQuotes is null, then do not stripprivate voidupdateRequiredOptions(Option option)Removes the option or its group from the list of expected elements.
-
-
-
Field Detail
-
cmd
protected CommandLine cmd
The command-line instance.
-
options
protected Options options
The current options.
-
stopAtNonOption
protected boolean stopAtNonOption
Flag indicating how unrecognized tokens are handled.trueto stop the parsing and add the remaining tokens to the args list.falseto throw an exception.
-
currentToken
protected java.lang.String currentToken
The token currently processed.
-
currentOption
protected Option currentOption
The last option parsed.
-
skipParsing
protected boolean skipParsing
Flag indicating if tokens should no longer be analyzed and simply added as arguments of the command line.
-
expectedOpts
protected java.util.List expectedOpts
The required options and groups expected to be found when parsing the command line.
-
allowPartialMatching
private final boolean allowPartialMatching
Flag indicating if partial matching of long options is supported.
-
stripLeadingAndTrailingQuotes
private final java.lang.Boolean stripLeadingAndTrailingQuotes
Flag indicating if balanced leading and trailing double quotes should be stripped from option arguments. null represents the historic arbitrary behaviour
-
-
Constructor Detail
-
DefaultParser
public DefaultParser()
Creates a new DefaultParser instance with partial matching enabled. By "partial matching" we mean that given the following code:{ @code final 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,-deonly matches the"debug"option. However, with "partial matching" disabled,-dewould enable bothdebugas well asextractoptions.
-
DefaultParser
public DefaultParser(boolean allowPartialMatching)
Create a new DefaultParser instance with the specified partial matching policy. By "partial matching" we mean that given the following code:{ @code final 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,-deonly matches the"debug"option. However, with "partial matching" disabled,-dewould enable bothdebugas well asextractoptions.- Parameters:
allowPartialMatching- if partial matching of long options shall be enabled
-
DefaultParser
private DefaultParser(boolean allowPartialMatching, java.lang.Boolean stripLeadingAndTrailingQuotes)Creates a new DefaultParser instance with the specified partial matching and quote stripping policy.- Parameters:
allowPartialMatching- if partial matching of long options shall be enabledstripLeadingAndTrailingQuotes- if balanced outer double quoutes should be stripped
-
-
Method Detail
-
builder
public static DefaultParser.Builder builder()
Creates a newDefaultParser.Builderto create anDefaultParserusing descriptive methods.- Returns:
- a new
DefaultParser.Builderinstance - Since:
- 1.5.0
-
checkRequiredArgs
private void checkRequiredArgs() throws ParseExceptionThrows aMissingArgumentExceptionif the current option didn't receive the number of arguments expected.- Throws:
ParseException
-
checkRequiredOptions
protected void checkRequiredOptions() throws MissingOptionExceptionThrows aMissingOptionExceptionif all of the required options are not present.- Throws:
MissingOptionException- if any of the required Options are not present.
-
getLongPrefix
private java.lang.String getLongPrefix(java.lang.String token)
Searches for a prefix that is the long name of an option (-Xmx512m)- Parameters:
token-
-
getMatchingLongOptions
private java.util.List<java.lang.String> getMatchingLongOptions(java.lang.String token)
Gets a list of matching option strings for the given token, depending on the selected partial matching policy.- Parameters:
token- the token (may contain leading dashes)- Returns:
- the list of matching option strings or an empty list if no matching option could be found
-
handleConcatenatedOptions
protected void handleConcatenatedOptions(java.lang.String token) throws ParseExceptionBreakstokeninto 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.
-
handleLongOption
private void handleLongOption(java.lang.String token) throws ParseExceptionHandles the following tokens: --L --L=V --L V --l- Parameters:
token- the command line token to handle- Throws:
ParseException
-
handleLongOptionWithEqual
private void handleLongOptionWithEqual(java.lang.String token) throws ParseExceptionHandles the following tokens: --L=V -L=V --l=V -l=V- Parameters:
token- the command line token to handle- Throws:
ParseException
-
handleLongOptionWithoutEqual
private void handleLongOptionWithoutEqual(java.lang.String token) throws ParseExceptionHandles the following tokens: --L -L --l -l- Parameters:
token- the command line token to handle- Throws:
ParseException
-
handleOption
private void handleOption(Option option) throws ParseException
- Throws:
ParseException
-
handleProperties
private void handleProperties(java.util.Properties properties) throws ParseExceptionSets the values of Options using the values inproperties.- Parameters:
properties- The value properties to be processed.- Throws:
ParseException
-
handleShortAndLongOption
private void handleShortAndLongOption(java.lang.String token) throws ParseExceptionHandles 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
-
handleToken
private void handleToken(java.lang.String token) throws ParseExceptionHandles any command line token.- Parameters:
token- the command line token to handle- Throws:
ParseException
-
handleUnknownToken
private void handleUnknownToken(java.lang.String token) throws ParseExceptionHandles 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
-
isArgument
private boolean isArgument(java.lang.String token)
Tests if the token is a valid argument.- Parameters:
token-
-
isJavaProperty
private boolean isJavaProperty(java.lang.String token)
Tests if the specified token is a Java-like property (-Dkey=value).
-
isLongOption
private boolean isLongOption(java.lang.String token)
Tests if the token looks like a long option.- Parameters:
token-
-
isNegativeNumber
private boolean isNegativeNumber(java.lang.String token)
Tests if the token is a negative number.- Parameters:
token-
-
isOption
private boolean isOption(java.lang.String token)
Tests if the token looks like an option.- Parameters:
token-
-
isShortOption
private boolean isShortOption(java.lang.String token)
Tests if the token looks like a short option.- Parameters:
token-
-
parse
public CommandLine parse(Options options, java.lang.String[] arguments) throws ParseException
Description copied from interface:CommandLineParserParses 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, java.lang.String[] arguments, boolean stopAtNonOption) throws ParseException
Description copied from interface:CommandLineParserParses the arguments according to the specified options.- Specified by:
parsein interfaceCommandLineParser- Parameters:
options- the specified Optionsarguments- the command line argumentsstopAtNonOption- iftruean unrecognized argument stops the parsing and the remaining arguments are added to theCommandLines args list. Iffalsean 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, java.lang.String[] arguments, java.util.Properties properties) throws ParseException
Parses 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, java.lang.String[] arguments, java.util.Properties properties, boolean stopAtNonOption) throws ParseException
Parses the arguments according to the specified options and properties.- Parameters:
options- the specified Optionsarguments- the command line argumentsproperties- command line option name-value pairsstopAtNonOption- iftruean unrecognized argument stops the parsing and the remaining arguments are added to theCommandLines args list. Iffalsean 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.
-
stripLeadingAndTrailingQuotesDefaultOff
private java.lang.String stripLeadingAndTrailingQuotesDefaultOff(java.lang.String token)
Strips balanced leading and trailing quotes if the stripLeadingAndTrailingQuotes is set If stripLeadingAndTrailingQuotes is null, then do not strip- Parameters:
token- a string- Returns:
- token with the quotes stripped (if set)
-
stripLeadingAndTrailingQuotesDefaultOn
private java.lang.String stripLeadingAndTrailingQuotesDefaultOn(java.lang.String token)
Strips balanced leading and trailing quotes if the stripLeadingAndTrailingQuotes is set If stripLeadingAndTrailingQuotes is null, then do not strip- Parameters:
token- a string- Returns:
- token with the quotes stripped (if set)
-
updateRequiredOptions
private void updateRequiredOptions(Option option) throws AlreadySelectedException
Removes the option or its group from the list of expected elements.- Parameters:
option-- Throws:
AlreadySelectedException
-
-