Class DefaultOptionParser
- java.lang.Object
-
- com.aspectran.shell.command.option.DefaultOptionParser
-
- All Implemented Interfaces:
OptionParser
public class DefaultOptionParser extends java.lang.Object implements OptionParser
The default command option parser.
-
-
Field Summary
Fields Modifier and Type Field Description protected OptioncurrentOptionThe last option parsed.protected java.lang.StringcurrentTokenThe token currently processed.protected java.util.List<java.lang.Object>expectedOptsThe required options and groups expected to be found when parsing the command line.protected OptionsoptionsThe current options.protected ParsedOptionsparsedOptionsThe parsed options instance.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.
-
Constructor Summary
Constructors 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcheckRequiredOptions()Throws aMissingOptionExceptionif all of the required options are not present.protected voidhandleConcatenatedOptions(java.lang.String token)Breakstokeninto its constituent parts using the following algorithm.ParsedOptionsparse(Options options, java.lang.String[] arguments)Parse the arguments according to the specified options.ParsedOptionsparse(Options options, java.lang.String[] arguments, boolean stopAtNonOption)Parse the arguments according to the specified options.ParsedOptionsparse(Options options, java.lang.String[] arguments, java.util.Properties properties)Parse the arguments according to the specified options and properties.ParsedOptionsparse(Options options, java.lang.String[] arguments, java.util.Properties properties, boolean stopAtNonOption)Parse the arguments according to the specified options and properties.
-
-
-
Field Detail
-
parsedOptions
protected ParsedOptions parsedOptions
The parsed options 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<java.lang.Object> expectedOpts
The required options and groups expected to be found when parsing the command line.
-
-
Constructor Detail
-
DefaultOptionParser
public DefaultOptionParser()
Creates a new DefaultParser instance with partial matching enabled. By "partial matching" we mean that given the following code:with "partial matching" turned on,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."));-deonly matches the"debug"option. However, with "partial matching" disabled,-dewould enable bothdebugas well asextractoptions.
-
DefaultOptionParser
public DefaultOptionParser(boolean allowPartialMatching)
Create a new DefaultParser instance with the specified partial matching policy. By "partial matching" we mean that given the following code:with "partial matching" turned on,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."));-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
-
-
Method Detail
-
parse
public ParsedOptions parse(Options options, java.lang.String[] arguments) throws OptionParserException
Description copied from interface:OptionParserParse the arguments according to the specified options.- Specified by:
parsein interfaceOptionParser- Parameters:
options- the specified Optionsarguments- the command line arguments- Returns:
- the list of atomic option and value tokens
- Throws:
OptionParserException- if there are any problems encountered while parsing the command line tokens
-
parse
public ParsedOptions parse(Options options, java.lang.String[] arguments, java.util.Properties properties) throws OptionParserException
Parse the arguments according to the specified options and properties.- Specified by:
parsein interfaceOptionParser- 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:
OptionParserException- if there are any problems encountered while parsing the command line tokens
-
parse
public ParsedOptions parse(Options options, java.lang.String[] arguments, boolean stopAtNonOption) throws OptionParserException
Description copied from interface:OptionParserParse the arguments according to the specified options.- Specified by:
parsein interfaceOptionParser- Parameters:
options- the specified Optionsarguments- the command line argumentsstopAtNonOption- iftruean unrecognized argument stops the parsing and the remaining arguments are added to theParsedOptionss args list. Iffalsean unrecognized argument triggers a ParseException.- Returns:
- the list of atomic option and value tokens
- Throws:
OptionParserException- if there are any problems encountered while parsing the command line tokens
-
parse
public ParsedOptions parse(Options options, java.lang.String[] arguments, java.util.Properties properties, boolean stopAtNonOption) throws OptionParserException
Parse the arguments according to the specified options and properties.- Specified by:
parsein interfaceOptionParser- 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 theParsedOptionss args list. Iffalsean unrecognized argument triggers a ParseException.- Returns:
- the list of atomic option and value tokens
- Throws:
OptionParserException- if there are any problems encountered while parsing the command line tokens
-
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
-
handleConcatenatedOptions
protected void handleConcatenatedOptions(java.lang.String token) throws OptionParserExceptionBreakstokeninto 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:
OptionParserException- if there are any problems encountered while parsing the command line token
-
-