joptsimple
public class OptionParser extends java.lang.Object
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.
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:
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.has
would answer
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)
, orConstructor and Description |
---|
OptionParser()
Creates an option parser that initially recognizes no options, and does not
exhibit "POSIX-ly correct" behavior.
|
OptionParser(java.lang.String optionSpecification)
Creates an option parser and configures it to recognize the short options
specified in the given string.
|
Modifier and Type | Method and Description |
---|---|
OptionSpecBuilder |
accepts(java.lang.String option)
Tells the parser to recognize the given option.
|
OptionSpecBuilder |
accepts(java.lang.String option,
java.lang.String description)
Tells the parser to recognize the given option.
|
OptionSpecBuilder |
acceptsAll(java.util.Collection<java.lang.String> options)
Tells the parser to recognize the given options, and treat them as
synonymous.
|
OptionSpecBuilder |
acceptsAll(java.util.Collection<java.lang.String> options,
java.lang.String description)
Tells the parser to recognize the given options, and treat them as
synonymous.
|
OptionSet |
parse(java.lang.String... arguments)
Parses the given command line arguments according to the option specifications
given to the parser.
|
void |
posixlyCorrect(boolean setting)
Tells the parser whether or not to behave "POSIX-ly correct"-ly.
|
void |
printHelpOn(java.io.OutputStream sink)
Writes information about the options this parser recognizes to the given output
sink.
|
void |
printHelpOn(java.io.Writer sink)
Writes information about the options this parser recognizes to the given output
sink.
|
void |
recognizeAlternativeLongOptions(boolean recognize)
Tells the parser either to recognize or ignore "-W"-style long
options.
|
public OptionParser()
Creates an option parser that initially recognizes no options, and does not exhibit "POSIX-ly correct" behavior.
public OptionParser(java.lang.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 String
.
optionSpecification
- an option specificationjava.lang.NullPointerException
- if optionSpecification is
null
OptionException
- if the option specification contains illegal characters
or otherwise cannot be recognizedpublic OptionSpecBuilder accepts(java.lang.String option)
Tells the parser to recognize the given option.
This method returns an instance of OptionSpecBuilder
to allow the
formation of parser directives as sentences in a fluent interface language.
For example:
OptionParser parser = new OptionParser();
parser.accepts( "c" ).withRequiredArg().ofType( Integer.class );
If no methods are invoked on the returned OptionSpecBuilder
, then the
parser treats the option as accepting no argument.
option
- the option to recognizeOptionException
- if the option contains illegal charactersjava.lang.NullPointerException
- if the option is null
public OptionSpecBuilder accepts(java.lang.String option, java.lang.String description)
Tells the parser to recognize the given option.
option
- the option to recognizedescription
- a string that describes the purpose of the option. This is
used when generating help information about the parser.OptionException
- if the option contains illegal charactersjava.lang.NullPointerException
- if the option is null
accepts(String)
public OptionSpecBuilder acceptsAll(java.util.Collection<java.lang.String> options)
Tells the parser to recognize the given options, and treat them as synonymous.
options
- the options to recognize and treat as synonymousOptionException
- if any of the options contain illegal charactersjava.lang.NullPointerException
- if the option list or any of its elements are
null
accepts(String)
public OptionSpecBuilder acceptsAll(java.util.Collection<java.lang.String> options, java.lang.String description)
Tells the parser to recognize the given options, and treat them as synonymous.
options
- the options to recognize and treat as synonymousdescription
- a string that describes the purpose of the option. This is
used when generating help information about the parser.OptionException
- if any of the options contain illegal charactersjava.lang.NullPointerException
- if the option list or any of its elements are
null
java.lang.IllegalArgumentException
- if the option list is emptyacceptsAll(Collection)
public void posixlyCorrect(boolean setting)
Tells the parser whether or not to behave "POSIX-ly correct"-ly.
setting
- true
if the parser should behave "POSIX-ly correct"-lypublic void recognizeAlternativeLongOptions(boolean recognize)
Tells the parser either to recognize or ignore "-W"-style long options.
recognize
- true
if the parser is to recognize the special style
of long optionspublic void printHelpOn(java.io.OutputStream sink) throws java.io.IOException
Writes information about the options this parser recognizes to the given output sink.
The output sink is flushed, but not closed.
sink
- the sink to write information tojava.io.IOException
- if there is a problem writing to the sinkjava.lang.NullPointerException
- if sink is null
printHelpOn(Writer)
public void printHelpOn(java.io.Writer sink) throws java.io.IOException
Writes information about the options this parser recognizes to the given output sink.
The output sink is flushed, but not closed.
sink
- the sink to write information tojava.io.IOException
- if there is a problem writing to the sinkjava.lang.NullPointerException
- if sink is null
printHelpOn(OutputStream)
public OptionSet parse(java.lang.String... arguments)
Parses the given command line arguments according to the option specifications given to the parser.
arguments
- arguments to parseOptionSet
describing the parsed options, their arguments, and
any non-option arguments foundOptionException
- if problems are detected while parsingjava.lang.NullPointerException
- if the argument list is null