public class DefaultParser extends Object implements CommandLineParser
| Modifier and Type | Class | Description |
|---|---|---|
static class |
DefaultParser.Builder |
A nested builder class to create
DefaultParser instances
using descriptive methods. |
static class |
DefaultParser.NonOptionAction |
Enum representing possible actions that may be done when "non option" is discovered during parsing.
|
| Modifier and Type | Field | 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 DefaultParser.NonOptionAction |
nonOptionAction |
Action to happen when "non option" token is discovered.
|
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 |
Deprecated.
Use
nonOptionAction instead. This field is unused, and left for binary compatibility reasons. |
| 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.
|
| Modifier and Type | Method | Description |
|---|---|---|
protected void |
addArg(String token) |
Adds token to command line
CommandLine.addArg(String). |
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. |
protected void |
handleUnknownToken(String token) |
Handles an unknown token.
|
CommandLine |
parse(Options options,
Properties properties,
DefaultParser.NonOptionAction nonOptionAction,
String... arguments) |
Parses the arguments according to the specified options and properties.
|
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
@Deprecated protected boolean stopAtNonOption
nonOptionAction instead. This field is unused, and left for binary compatibility reasons.true to stop the parsing and add the remaining
tokens to the args list. false to throw an exception.protected DefaultParser.NonOptionAction nonOptionAction
protected String currentToken
protected Option currentOption
protected boolean skipParsing
protected List expectedOpts
public DefaultParser()
By "partial matching" we mean that given the following 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)
By "partial matching" we mean that given the following 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 addArg(String token)
CommandLine.addArg(String).token - the unrecognized option/argument.protected 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.protected void handleUnknownToken(String token) throws ParseException
token - the command line token to handle.ParseException - if parsing should fail.public CommandLine parse(Options options, Properties properties, DefaultParser.NonOptionAction nonOptionAction, String... arguments) throws ParseException
options - the specified Optionsproperties - command line option name-value pairsnonOptionAction - see DefaultParser.NonOptionAction.arguments - the command line argumentsParseException - if there are any problems encountered while parsing the command line tokens.public CommandLine parse(Options options, String[] arguments) throws ParseException
CommandLineParserparse in interface CommandLineParseroptions - the specified Options.arguments - the command line arguments.ParseException - if there are any problems encountered while parsing the command line tokens.public CommandLine parse(Options options, String[] arguments, boolean stopAtNonOption) throws ParseException
CommandLineParserparse in interface CommandLineParseroptions - the specified Options.arguments - the command line arguments.stopAtNonOption - if true an unrecognized argument stops the parsing and the remaining arguments
are added to the CommandLines args list. If false an unrecognized argument triggers a
ParseException.ParseException - if there are any problems encountered while parsing the command line tokens.parse(Options, Properties, NonOptionAction, String[])public CommandLine parse(Options options, String[] arguments, Properties properties) throws ParseException
options - the specified Options.arguments - the command line arguments.properties - command line option name-value pairs.ParseException - 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 Options.arguments - the command line arguments.properties - command line option name-value pairs.stopAtNonOption - if true an unrecognized argument stops the parsing and the remaining arguments
are added to the CommandLines args list. If false an unrecognized argument triggers a
ParseException.ParseException - if there are any problems encountered while parsing the command line tokens.parse(Options, Properties, NonOptionAction, String[])Copyright © 2002-2025 The Apache Software Foundation. All rights reserved.Apache Commons CLI | Issue management | Source repository