public class DefaultParser extends Object implements CommandLineParser
Modifier and Type | Class and Description |
---|---|
static class |
DefaultParser.Builder
A nested builder class to create
DefaultParser instances
using descriptive methods. |
Modifier and Type | Field and Description |
---|---|
protected CommandLine |
cmd
The command-line instance.
|
protected Option |
currentOption
The last option parsed.
|
protected String |
currentToken
The token currently processed.
|
protected List |
expectedOpts
The required options and groups expected to be found when parsing the command line.
|
protected Options |
options
The current options.
|
protected boolean |
skipParsing
Flag indicating if tokens should no longer be analyzed and simply added as arguments of the command line.
|
protected boolean |
stopAtNonOption
Flag indicating how unrecognized tokens are handled.
|
Constructor and 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.
|
Modifier and Type | Method and Description |
---|---|
static DefaultParser.Builder |
builder()
Creates a new
DefaultParser.Builder to create an DefaultParser using descriptive
methods. |
protected void |
checkRequiredOptions()
Throws a
MissingOptionException if all of the required options are not present. |
protected void |
handleConcatenatedOptions(String token)
Breaks
token into its constituent parts using the following algorithm. |
CommandLine |
parse(Options options,
String[] arguments)
Parses the arguments according to the specified options.
|
CommandLine |
parse(Options options,
String[] arguments,
boolean stopAtNonOption)
Parses the arguments according to the specified options.
|
CommandLine |
parse(Options options,
String[] arguments,
Properties properties)
Parses the arguments according to the specified options and properties.
|
CommandLine |
parse(Options options,
String[] arguments,
Properties properties,
boolean stopAtNonOption)
Parses the arguments according to the specified options and properties.
|
protected CommandLine cmd
protected boolean stopAtNonOption
true
to stop the parsing and add the remaining
tokens to the args list. false
to throw an exception.protected String currentToken
protected Option currentOption
protected boolean skipParsing
protected List expectedOpts
public DefaultParser()
{ @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,
-de
only matches the "debug"
option. However, with
"partial matching" disabled, -de
would enable both debug
as well as extract
options.public DefaultParser(boolean allowPartialMatching)
{ @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,
-de
only matches the "debug"
option. However, with
"partial matching" disabled, -de
would enable both debug
as well as extract
options.allowPartialMatching
- if partial matching of long options shall be enabledpublic static DefaultParser.Builder builder()
DefaultParser.Builder
to create an DefaultParser
using descriptive
methods.DefaultParser.Builder
instanceprotected void checkRequiredOptions() throws MissingOptionException
MissingOptionException
if all of the required options are not present.MissingOptionException
- if any of the required Options are not present.protected void handleConcatenatedOptions(String token) throws ParseException
token
into its constituent parts using the following algorithm.
Option
exists with that id.Option
does exist then add that character prepended with "-" to the list of processed
tokens.Option
can 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.Option
does NOT exist AND stopAtNonOption
IS set then add the
special token "--" followed by the remaining characters and also the remaining tokens directly to the
processed tokens list.Option
does NOT exist AND stopAtNonOption
IS NOT set then add
that character prepended with "-".token
- The current token to be burst at the first non-Option encountered.ParseException
- if there are any problems encountered while parsing the command line token.public CommandLine parse(Options options, String[] arguments) throws ParseException
CommandLineParser
parse
in interface CommandLineParser
options
- the specified Optionsarguments
- the command line argumentsParseException
- if there are any problems encountered while parsing the command line tokens.public CommandLine parse(Options options, String[] arguments, boolean stopAtNonOption) throws ParseException
CommandLineParser
parse
in interface CommandLineParser
options
- the specified Optionsarguments
- the command line argumentsstopAtNonOption
- if true
an unrecognized argument stops the parsing and the remaining arguments
are added to the CommandLine
s args list. If false
an unrecognized argument triggers a
ParseException.ParseException
- if there are any problems encountered while parsing the command line tokens.public CommandLine parse(Options options, String[] arguments, Properties properties) throws ParseException
options
- the specified Optionsarguments
- the command line argumentsproperties
- command line option name-value pairsParseException
- if there are any problems encountered while parsing the command line tokens.public CommandLine parse(Options options, String[] arguments, Properties properties, boolean stopAtNonOption) throws ParseException
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 the CommandLine
s args list. If false
an unrecognized argument triggers a
ParseException.ParseException
- if there are any problems encountered while parsing the command line tokens.Copyright © 2002–2020 The Apache Software Foundation. All rights reserved.