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
- Version:
- $Id: DefaultParser.java 1783175 2017-02-16 07:52:05Z britter $
-
-
Field Summary
Fields Modifier and Type Field Description 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.
-
Constructor Summary
Constructors Constructor Description DefaultParser()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidhandleConcatenatedOptions(java.lang.String token)Breakstokeninto its constituent parts using the following algorithm.CommandLineparse(Options options, java.lang.String[] arguments)Parse the arguments according to the specified options.CommandLineparse(Options options, java.lang.String[] arguments, boolean stopAtNonOption)Parse the arguments according to the specified options.CommandLineparse(Options options, java.lang.String[] arguments, java.util.Properties properties)Parse the arguments according to the specified options and properties.CommandLineparse(Options options, java.lang.String[] arguments, java.util.Properties properties, boolean stopAtNonOption)Parse the arguments according to the specified options and properties.
-
-
-
Field Detail
-
cmd
protected CommandLine cmd
The command-line instance.
-
stopAtNonOption
protected boolean stopAtNonOption
Flag 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
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.
-
-
Constructor Detail
-
DefaultParser
public DefaultParser()
-
-
Method Detail
-
parse
public CommandLine parse(Options options, java.lang.String[] arguments) 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 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, java.util.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, java.lang.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, java.lang.String[] arguments, java.util.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.
-
handleConcatenatedOptions
protected void handleConcatenatedOptions(java.lang.String token) throws ParseException
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.
-
-