Parses command line arguments, using a syntax that attempts to take from the best of POSIX getopt()
and GNU getopt_long()
.
This parser supports short options and long options.
-
") followed by a single letter or digit,
or question mark ("?
"), or dot (".
"), or underscore ("_
").-d /tmp
-d/tmp
"="
), as in -d=/tmp
-d /tmp -d /var -d /opt
; or, when using the
"separated values" clause of the "fluent
interface" (see below), give multiple values separated by a given character as a single argument to the
option.-abc
is treated as -a -b -c
. If a short option
in the cluster can accept an argument, the remaining characters are interpreted as the argument for that
option."--"
) signals that the remaining arguments are to be
treated as non-options."--"
), followed by multiple letters, digits,
hyphens, question marks, or dots. A hyphen cannot be the first character of a long option specification when
configuring the parser.--directory /tmp
"="
), as in
--directory=/tmp
"-"
) instead of a double hyphen ("--"
) for a long
option.-W
is reserved. If you tell the parser to recognize alternative long options, then it will treat, for example,
-W foo=bar
as the long option foo
with argument bar
, as though you had written
--foo=bar
.-W
as a valid short option, or use it as an abbreviation for a long option, but
recognizing alternative long options will always supersede
this behavior.Number
, then that argument is treated as the
negative number argument of the option, even if the parser recognizes the corresponding numeric option.
For example:
OptionParser parser = new OptionParser();
parser.accepts( "a" ).withOptionalArg().ofType( Integer.class );
parser.accepts( "2" );
OptionSet options = parser.parse( "-a", "-2" );
In this case, the option set contains "a"
with argument -2
, not both "a"
and
"2"
. Swapping the elements in the args array gives the latter.There are two ways to tell the parser what options to recognize:
accepts
or acceptsAll
methods; calls on the ensuing chain of objects describe whether the options can take an argument,
whether the argument is required or optional, to what type arguments of the options should be converted if any,
etc. Since version 3, these calls return an instance of OptionSpec
, which can subsequently be used to
retrieve the arguments of the associated option in a type-safe manner.String
.
Here are the rules for the format of the specification strings this constructor accepts:
*)
to indicate that
the option is a "help" option.":"
),
then the option requires an argument."::"
),
then the option accepts an optional argument."+"
), the parser will behave
"POSIX-ly correct"."W;"
(capital W followed by a
semicolon), the parser will recognize the alternative form of long options.Each of the options in a list of options given to acceptsAll
is treated as a
synonym of the others. For example:
OptionParser parser = new OptionParser();
parser.acceptsAll( asList( "w", "interactive", "confirmation" ) );
OptionSet options = parser.parse( "-w" );
In this case, options.
would answer has
true
when given arguments
"w"
, "interactive"
, and "confirmation"
. The OptionSet
would give the same
responses to these arguments for its other methods as well.
By default, as with GNU getopt()
, the parser allows intermixing of options and non-options. If, however,
the parser has been created to be "POSIX-ly correct", then the first argument that does not look lexically like an
option, and is not a required argument of a preceding option, signals the end of options. You can still bind
optional arguments to their options using the abutting (for short options) or =
syntax.
Unlike GNU getopt()
, this parser does not honor the environment variable POSIXLY_CORRECT
.
"POSIX-ly correct" parsers are configured by either:
posixlyCorrect(boolean)
, or"+"
)Modifier and Type | Field and Description |
---|---|
private boolean | |
private final Map | |
private final Map | |
private HelpFormatter | |
private boolean | |
private final OptionNameMap | |
private final Map | |
private final Map | |
private OptionParserState | |
private final ArrayList |
Access | Constructor and Description |
---|---|
public | OptionParser()
Creates an option parser that initially recognizes no options, and does not exhibit "POSIX-ly correct" behavior. |
public | OptionParser(boolean
whether unambiguous abbreviations of long options should be recognized
by the parser allowAbbreviations)Creates an option parser that initially recognizes no options, and does not exhibit "POSIX-ly correct" behavior. |
public | OptionParser(String
an option specification optionSpecification)Creates an option parser and configures it to recognize the short options specified in the given string. |
Modifier and Type | Method and Description |
---|---|
private Map | |
public OptionSpecBuilder | accepts(String
the option to recognize option)Implements jdk. Tells the parser to recognize the given option. |
public OptionSpecBuilder | accepts(String
the option to recognize option, String a string that describes the purpose of the option. This is used when generating help
information about the parser. description)Implements jdk. Tells the parser to recognize the given option. |
public OptionSpecBuilder | acceptsAll(List<String>
the options to recognize and treat as synonymous options)Implements jdk. Tells the parser to recognize the given options, and treat them as synonymous. |
public OptionSpecBuilder | acceptsAll(List<String>
the options to recognize and treat as synonymous options, String a string that describes the purpose of the option. This is used when generating help
information about the parser. description)Implements jdk. Tells the parser to recognize the given options, and treat them as synonymous. |
public void | allowsUnrecognizedOptions()
Implements jdk. Tells the parser to treat unrecognized options as non-option arguments. |
pack-priv void | |
pack-priv void | |
pack-priv void | |
pack-priv void | |
pack-priv boolean | |
private void | |
private void | |
private static char[] | |
public void | formatHelpWith(HelpFormatter
the formatter to use for printing help formatter)Tells the parser to use the given formatter when asked to print help. |
pack-priv void | |
pack-priv void | |
private void | |
pack-priv void | |
private boolean | |
pack-priv boolean | |
pack-priv boolean | |
private List | |
public void | mutuallyExclusive(OptionSpecBuilder...
descriptors for options that should be mutually exclusive on a command line. specs)Mandates mutual exclusiveness for the options built by the specified builders. |
pack-priv void | |
public NonOptionArgumentSpec | nonOptions()
Implements jdk. Gives an object that represents an access point for non-option arguments on a command line. |
public NonOptionArgumentSpec | nonOptions(String
a string that describes the purpose of the non-option arguments. This is used when generating
help information about the parser. description)Implements jdk. Gives an object that represents an access point for non-option arguments on a command line. |
private boolean | |
public OptionSet | |
private static KeyValuePair | |
private static KeyValuePair | |
public void | posixlyCorrect(boolean
true if the parser should behave "POSIX-ly correct"-lyImplements jdk. Tells the parser whether or not to behave "POSIX-ly correct"-ly. |
pack-priv boolean | |
public void | printHelpOn(OutputStream
the sink to write information to sink)Writes information about the options this parser recognizes to the given output sink. |
public void | printHelpOn(Writer
the sink to write information to sink)Writes information about the options this parser recognizes to the given output sink. |
private void | putDependentOption(List<String> precedentSynonyms, OptionSpec<?> required, Map<List<String>, Set<OptionSpec<?>>> target)
|
pack-priv void | |
public void | recognizeAlternativeLongOptions(boolean
true if the parser is to recognize the special style of long optionsImplements jdk. Tells the parser either to recognize or ignore |
public Map | Returns: a map containing all the configured options and their correspondingOptionSpec Retrieves all options-spec pairings which have been configured for the parser in the same order as declared during training. |
pack-priv void | |
pack-priv void | |
pack-priv void | |
pack-priv void | |
private void | |
private AbstractOptionSpec | |
private AbstractOptionSpec | |
private List | |
private void |
allowsUnrecognizedOptions | back to summary |
---|---|
private boolean allowsUnrecognizedOptions |
availableIf | back to summary |
---|---|
private final Map<List<String>, Set<OptionSpec<?>>> availableIf |
availableUnless | back to summary |
---|---|
private final Map<List<String>, Set<OptionSpec<?>>> availableUnless |
helpFormatter | back to summary |
---|---|
private HelpFormatter helpFormatter |
posixlyCorrect | back to summary |
---|---|
private boolean posixlyCorrect |
recognizedOptions | back to summary |
---|---|
private final OptionNameMap<AbstractOptionSpec<?>> recognizedOptions |
requiredIf | back to summary |
---|---|
private final Map<List<String>, Set<OptionSpec<?>>> requiredIf |
requiredUnless | back to summary |
---|---|
private final Map<List<String>, Set<OptionSpec<?>>> requiredUnless |
state | back to summary |
---|---|
private OptionParserState state |
trainingOrder | back to summary |
---|---|
private final ArrayList<AbstractOptionSpec<?>> trainingOrder |
OptionParser | back to summary |
---|---|
public OptionParser() Creates an option parser that initially recognizes no options, and does not exhibit "POSIX-ly correct" behavior. |
OptionParser | back to summary |
---|---|
public OptionParser(boolean allowAbbreviations) Creates an option parser that initially recognizes no options, and does not exhibit "POSIX-ly correct" behavior.
|
OptionParser | back to summary |
---|---|
public OptionParser(String optionSpecification) Creates an option parser and configures it to recognize the short options specified in the given string.
Arguments of options specified this way will be of type
|
_recognizedOptions | back to summary |
---|---|
private Map |
accepts | back to summary |
---|---|
public OptionSpecBuilder accepts(String option) Implements jdk. Doc from jdk. Tells the parser to recognize the given option. This method returns an instance of
If no methods are invoked on the returned
|
accepts | back to summary |
---|---|
public OptionSpecBuilder accepts(String option, String description) Implements jdk. Doc from jdk. Tells the parser to recognize the given option.
|
acceptsAll | back to summary |
---|---|
public OptionSpecBuilder acceptsAll(List<String> options) Implements jdk. Doc from jdk. Tells the parser to recognize the given options, and treat them as synonymous.
|
acceptsAll | back to summary |
---|---|
public OptionSpecBuilder acceptsAll(List<String> options, String description) Implements jdk. Doc from jdk. Tells the parser to recognize the given options, and treat them as synonymous.
|
allowsUnrecognizedOptions | back to summary |
---|---|
public void allowsUnrecognizedOptions() Implements jdk. Doc from jdk. Tells the parser to treat unrecognized options as non-option arguments. If not called, then the parser raises an |
availableIf | back to summary |
---|---|
pack-priv void availableIf(List<String> precedentSynonyms, String available) |
availableIf | back to summary |
---|---|
pack-priv void availableIf(List<String> precedentSynonyms, OptionSpec<?> available) |
availableUnless | back to summary |
---|---|
pack-priv void availableUnless(List<String> precedentSynonyms, String available) |
availableUnless | back to summary |
---|---|
pack-priv void availableUnless(List<String> precedentSynonyms, OptionSpec<?> available) |
doesAllowsUnrecognizedOptions | back to summary |
---|---|
pack-priv boolean doesAllowsUnrecognizedOptions() |
ensureAllowedOptions | back to summary |
---|---|
private void ensureAllowedOptions(OptionSet options) |
ensureRequiredOptions | back to summary |
---|---|
private void ensureRequiredOptions(OptionSet options) |
extractShortOptionsFrom | back to summary |
---|---|
private static char[] extractShortOptionsFrom(String argument) |
formatHelpWith | back to summary |
---|---|
public void formatHelpWith(HelpFormatter formatter) Tells the parser to use the given formatter when asked to print help.
|
handleLongOptionToken | back to summary |
---|---|
pack-priv void handleLongOptionToken(String candidate, ArgumentList arguments, OptionSet detected) |
handleNonOptionArgument | back to summary |
---|---|
pack-priv void handleNonOptionArgument(String candidate, ArgumentList arguments, OptionSet detectedOptions) |
handleShortOptionCluster | back to summary |
---|---|
private void handleShortOptionCluster(String candidate, ArgumentList arguments, OptionSet detected) |
handleShortOptionToken | back to summary |
---|---|
pack-priv void handleShortOptionToken(String candidate, ArgumentList arguments, OptionSet detected) |
isHelpOptionPresent | back to summary |
---|---|
private boolean isHelpOptionPresent(OptionSet options) |
isRecognized | back to summary |
---|---|
pack-priv boolean isRecognized(String option) |
looksLikeAnOption | back to summary |
---|---|
pack-priv boolean looksLikeAnOption(String argument) |
missingRequiredOptions | back to summary |
---|---|
private List |
mutuallyExclusive | back to summary |
---|---|
public void mutuallyExclusive(OptionSpecBuilder... specs) Mandates mutual exclusiveness for the options built by the specified builders.
|
noMoreOptions | back to summary |
---|---|
pack-priv void noMoreOptions() |
nonOptions | back to summary |
---|---|
public NonOptionArgumentSpec Implements jdk. Doc from jdk. Gives an object that represents an access point for non-option arguments on a command line.
|
nonOptions | back to summary |
---|---|
public NonOptionArgumentSpec Implements jdk. Doc from jdk. Gives an object that represents an access point for non-option arguments on a command line.
|
optionsHasAnyOf | back to summary |
---|---|
private boolean optionsHasAnyOf(OptionSet options, Collection<OptionSpec<?>> specs) |
parse | back to summary |
---|---|
public OptionSet parse(String... arguments) Parses the given command line arguments according to the option specifications given to the parser.
|
parseLongOptionWithArgument | back to summary |
---|---|
private static KeyValuePair parseLongOptionWithArgument(String argument) |
parseShortOptionWithArgument | back to summary |
---|---|
private static KeyValuePair parseShortOptionWithArgument(String argument) |
posixlyCorrect | back to summary |
---|---|
public void posixlyCorrect(boolean setting) Implements jdk. Doc from jdk. Tells the parser whether or not to behave "POSIX-ly correct"-ly.
|
posixlyCorrect | back to summary |
---|---|
pack-priv boolean posixlyCorrect() |
printHelpOn | back to summary |
---|---|
public void printHelpOn(OutputStream sink) throws IOException Writes information about the options this parser recognizes to the given output sink. The output sink is flushed, but not closed.
|
printHelpOn | back to summary |
---|---|
public void printHelpOn(Writer sink) throws IOException Writes information about the options this parser recognizes to the given output sink. The output sink is flushed, but not closed.
|
putDependentOption | back to summary |
---|---|
private void putDependentOption(List<String> precedentSynonyms, OptionSpec<?> required, Map<List<String>, Set<OptionSpec<?>>> target) |
recognize | back to summary |
---|---|
pack-priv void recognize(AbstractOptionSpec<?> spec) |
recognizeAlternativeLongOptions | back to summary |
---|---|
public void recognizeAlternativeLongOptions(boolean recognize) Implements jdk. Doc from jdk. Tells the parser either to recognize or ignore
|
recognizedOptions | back to summary |
---|---|
public Map Retrieves all options-spec pairings which have been configured for the parser in the same order as declared
during training. Option flags for specs are alphabetized by
|
requiredIf | back to summary |
---|---|
pack-priv void requiredIf(List<String> precedentSynonyms, String required) |
requiredIf | back to summary |
---|---|
pack-priv void requiredIf(List<String> precedentSynonyms, OptionSpec<?> required) |
requiredUnless | back to summary |
---|---|
pack-priv void requiredUnless(List<String> precedentSynonyms, String required) |
requiredUnless | back to summary |
---|---|
pack-priv void requiredUnless(List<String> precedentSynonyms, OptionSpec<?> required) |
reset | back to summary |
---|---|
private void reset() |
specFor | back to summary |
---|---|
private AbstractOptionSpec |
specFor | back to summary |
---|---|
private AbstractOptionSpec |
unavailableOptions | back to summary |
---|---|
private List |
validateOptionCharacters | back to summary |
---|---|
private void validateOptionCharacters(char[] options) |