- java.lang.Object
-
- Apache.CLI.DefaultParser
-
- All Implemented Interfaces:
CommandLineParser
public class DefaultParser extends java.lang.Object implements CommandLineParser
This Class-File has been copied, directly from the Apache Commons Software Foundation. Other than the minor, cosmetic changes (mentioned in the second-list below), this file is perfectly identical to the original that may be found on the Apache Website:apache.org.- The suggested license for using this file may be read here:
Apache License - The Source-Code Header for this file may be viewed here:
Source-File Header - Partial contents of the
'site/resources/images'directory:Resource Images
Notes about changes that have been made as a result of the import process:- Within all files, the original Package-Name declaration has been changed:
Original: package org.apache.commons.cli;Updated: package Apache.CLI;
- All Classes and Class-Members (Methods, Fields, etc...) that were previously Annotated
with the
@DeprecatedAnnotation have been summarily removed.
- Code Formattings and Styling has been heavily modified to conform to the Standard Java-HTML
Indentation & Styling Choices. The code itself remains identical, with only a few
squiggly-braces
'{' and '}'being removed, for cosmetic reasons (and for lowering "Developer Stress" Levels)..
Default parser.
Hi-Lited Source-Code:- View Here: Apache/CLI/DefaultParser.java
- Open New Browser-Tab: Apache/CLI/DefaultParser.java
File Size: 29,131 Bytes Line Count: 814 '\n' Characters Found
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDefaultParser.BuilderA nested builder class to createDefaultParserinstances using descriptive methods.
-
Field Summary
Fields Modifier and Type Field Description protected CommandLinecmdThe command-line instance.protected OptioncurrentOptionThe last option parsed.protected StringcurrentTokenThe token currently processed.protected 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()Creates a new DefaultParser instance with partial matching enabled.DefaultParser(boolean allowPartialMatching)Create a new DefaultParser instance with the specified partial matching policy.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static DefaultParser.Builderbuilder()Creates a newDefaultParser.Builderto create anDefaultParserusing descriptive methods.protected voidcheckRequiredOptions()Throws aMissingOptionExceptionif all of the required options are not present.protected voidhandleConcatenatedOptions(String token)Breakstokeninto its constituent parts using the following algorithm.CommandLineparse(Options options, String[] arguments)Parses the arguments according to the specified options.CommandLineparse(Options options, String[] arguments, boolean stopAtNonOption)Parses the arguments according to the specified options.CommandLineparse(Options options, String[] arguments, Properties properties)Parses the arguments according to the specified options and properties.CommandLineparse(Options options, String[] arguments, Properties properties, boolean stopAtNonOption)Parses the arguments according to the specified options and properties.
-
-
-
Field Detail
-
cmd
protected CommandLine cmd
The command-line instance.- Code:
- Exact Field Declaration Expression:
protected CommandLine cmd;
-
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.- Code:
- Exact Field Declaration Expression:
protected boolean stopAtNonOption;
-
currentToken
protected java.lang.String currentToken
The token currently processed.- Code:
- Exact Field Declaration Expression:
protected String currentToken;
-
currentOption
protected Option currentOption
The last option parsed.- Code:
- Exact Field Declaration Expression:
protected Option currentOption;
-
skipParsing
protected boolean skipParsing
Flag indicating if tokens should no longer be analyzed and simply added as arguments of the command line.- Code:
- Exact Field Declaration Expression:
protected boolean skipParsing;
-
expectedOpts
protected java.util.List expectedOpts
The required options and groups expected to be found when parsing the command line.- Code:
- Exact Field Declaration Expression:
protected List expectedOpts;
-
-
Constructor Detail
-
DefaultParser
public DefaultParser()
Creates a new DefaultParser instance with partial matching enabled. By "partial matching" we mean that given the following code:
Example:
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
public DefaultParser(boolean allowPartialMatching)
Create a new DefaultParser instance with the specified partial matching policy.
By "partial matching" we mean that given the following code:
Example:
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 Detail
-
builder
public static DefaultParser.Builder builder()
Creates a newDefaultParser.Builderto create anDefaultParserusing descriptive methods.- Returns:
- a new
DefaultParser.Builderinstance - Code:
- Exact Method Body:
return new Builder();
-
checkRequiredOptions
protected void checkRequiredOptions() throws MissingOptionException
Throws aMissingOptionExceptionif all of the required options are not present.- Throws:
MissingOptionException- if any of the required Options are not present.- Code:
- Exact Method Body:
// if there are required options that have not been processed if (!expectedOpts.isEmpty()) throw new MissingOptionException(expectedOpts);
-
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.- Code:
- Exact Method Body:
for (int i = 1; i < token.length(); i++) { final String ch = String.valueOf(token.charAt(i)); if (!options.hasOption(ch)) { handleUnknownToken(stopAtNonOption && i > 1 ? token.substring(i) : token); break; } handleOption(options.getOption(ch)); if (currentOption != null && token.length() != i + 1) { // add the trail as an argument of the option currentOption.addValueForProcessing(stripLeadingAndTrailingQuotesDefaultOff(token.substring(i + 1))); break; } }
-
parse
public CommandLine parse(Options options, java.lang.String[] arguments) 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 arguments- Returns:
- the list of atomic option and value tokens
- Throws:
ParseException- if there are any problems encountered while parsing the command line tokens.- Code:
- Exact Method Body:
return parse(options, arguments, null);
-
parse
public CommandLine parse(Options options, java.lang.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.- Code:
- Exact Method Body:
return parse(options, arguments, null, stopAtNonOption);
-
parse
public CommandLine parse(Options options, java.lang.String[] arguments, java.util.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.- Code:
- Exact Method Body:
return parse(options, arguments, properties, false);
-
parse
public CommandLine parse(Options options, java.lang.String[] arguments, java.util.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.- Code:
- Exact Method Body:
this.options = options; this.stopAtNonOption = stopAtNonOption; skipParsing = false; currentOption = null; expectedOpts = new ArrayList<>(options.getRequiredOptions()); // clear the data from the groups for (final OptionGroup group : options.getOptionGroups()) group.setSelected(null); cmd = new CommandLine(); if (arguments != null) for (final String argument : arguments) handleToken(argument); // check the arguments of the last option checkRequiredArgs(); // add the default options handleProperties(properties); checkRequiredOptions(); return cmd;
-
-