Package org.apache.commons.cli
Class DefaultParser
java.lang.Object
org.apache.commons.cli.DefaultParser
- All Implemented Interfaces:
CommandLineParser
Default parser.
- Since:
- 1.3
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA nested builder class to createDefaultParserinstances using descriptive methods. -
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
ConstructorsConstructorDescriptionCreates a new DefaultParser instance with partial matching enabled.DefaultParser(boolean allowPartialMatching) Create a new DefaultParser instance with the specified partial matching policy. -
Method Summary
Modifier and TypeMethodDescriptionstatic DefaultParser.Builderbuilder()Creates a newDefaultParser.Builderto create anDefaultParserusing descriptive methods.protected voidThrows aMissingOptionExceptionif all of the required options are not present.protected voidhandleConcatenatedOptions(String token) Breakstokeninto its constituent parts using the following algorithm.Parses the arguments according to the specified options.Parses the arguments according to the specified options.parse(Options options, String[] arguments, Properties properties) Parses the arguments according to the specified options and properties.parse(Options options, String[] arguments, Properties properties, boolean stopAtNonOption) Parses the arguments according to the specified options and properties.
-
Field Details
-
cmd
The command-line instance. -
options
The current options. -
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
The token currently processed. -
currentOption
The last option parsed. -
skipParsing
Flag 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()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
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
-
-
Method Details
-
builder
Creates a newDefaultParser.Builderto create anDefaultParserusing descriptive methods.- Returns:
- a new
DefaultParser.Builderinstance - Since:
- 1.5.0
-
checkRequiredOptions
Throws aMissingOptionExceptionif all of the required options are not present.- Throws:
MissingOptionException- if any of the required Options are not present.
-
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.
-
parse
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, 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, String[] arguments, 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, String[] arguments, 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.
-