Top Description Fields Constructors Methods
jdk.internal.joptsimple

public Class OptionParser

extends Object
implements OptionDeclarer
Class Inheritance
All Implemented Interfaces
jdk.internal.joptsimple.OptionDeclarer
Imports
java.io.IOException, .OutputStream, .OutputStreamWriter, .Writer, java.util.*, jdk.internal.joptsimple.internal.AbbreviationMap, .SimpleOptionNameMap, .OptionNameMap, jdk.internal.joptsimple.util.KeyValuePair

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.

There are two ways to tell the parser what options to recognize:

  1. A "fluent interface"-style API for specifying options, available since version 2. Sentences in this fluent interface language begin with a call to 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.
  2. Since version 1, a more concise way of specifying short options has been to use the special constructor. Arguments of options specified in this manner will be of type String. Here are the rules for the format of the specification strings this constructor accepts:
    • Any letter or digit is treated as an option character.
    • An option character can be immediately followed by an asterisk (*) to indicate that the option is a "help" option.
    • If an option character (with possible trailing asterisk) is followed by a single colon (":"), then the option requires an argument.
    • If an option character (with possible trailing asterisk) is followed by two colons ("::"), then the option accepts an optional argument.
    • Otherwise, the option character accepts no argument.
    • If the option specification string begins with a plus sign ("+" ), the parser will behave "POSIX-ly correct".
    • If the option specification string contains the sequence "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.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:

  1. using the method posixlyCorrect(boolean), or
  2. using the constructor with an argument whose first character is a plus sign ("+")
Author
Paul Holser
See Also
The GNU C Library

Field Summary

Modifier and TypeField and Description
private boolean
private final Map<List<String>, Set<OptionSpec<?>>>
private final Map<List<String>, Set<OptionSpec<?>>>
private HelpFormatter
private boolean
private final OptionNameMap<AbstractOptionSpec<?>>
private final Map<List<String>, Set<OptionSpec<?>>>
private final Map<List<String>, Set<OptionSpec<?>>>
private OptionParserState
private final ArrayList<AbstractOptionSpec<?>>

Constructor Summary

AccessConstructor 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.

Method Summary

Modifier and TypeMethod and Description
private Map<String, AbstractOptionSpec<?>>
public OptionSpecBuilder
accepts(String
the option to recognize
option
)

Implements jdk.internal.joptsimple.OptionDeclarer.accepts.

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.internal.joptsimple.OptionDeclarer.accepts.

Tells the parser to recognize the given option.

public OptionSpecBuilder
acceptsAll(List<String>
the options to recognize and treat as synonymous
options
)

Implements jdk.internal.joptsimple.OptionDeclarer.acceptsAll.

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.internal.joptsimple.OptionDeclarer.acceptsAll.

Tells the parser to recognize the given options, and treat them as synonymous.

public void
allowsUnrecognizedOptions()

Implements jdk.internal.joptsimple.OptionDeclarer.allowsUnrecognizedOptions.

Tells the parser to treat unrecognized options as non-option arguments.

pack-priv void
availableIf(List<String> precedentSynonyms, String available)

pack-priv void
availableIf(List<String> precedentSynonyms, OptionSpec<?> available)

pack-priv void
availableUnless(List<String> precedentSynonyms, String available)

pack-priv void
availableUnless(List<String> precedentSynonyms, OptionSpec<?> available)

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
handleLongOptionToken(String candidate, ArgumentList arguments, OptionSet detected)

pack-priv void
handleNonOptionArgument(String candidate, ArgumentList arguments, OptionSet detectedOptions)

private void
handleShortOptionCluster(String candidate, ArgumentList arguments, OptionSet detected)

pack-priv void
handleShortOptionToken(String candidate, ArgumentList arguments, OptionSet detected)

private boolean
pack-priv boolean
pack-priv boolean
private List<AbstractOptionSpec<?>>
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<String>
nonOptions()

Implements jdk.internal.joptsimple.OptionDeclarer.nonOptions.

Gives an object that represents an access point for non-option arguments on a command line.

public NonOptionArgumentSpec<String>
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.internal.joptsimple.OptionDeclarer.nonOptions.

Gives an object that represents an access point for non-option arguments on a command line.

private boolean
public OptionSet

Returns:

an OptionSet describing the parsed options, their arguments, and any non-option arguments found
parse
(String...
arguments to parse
arguments
)

Parses the given command line arguments according to the option specifications given to the parser.

private static KeyValuePair
private static KeyValuePair
public void
posixlyCorrect(boolean
true if the parser should behave "POSIX-ly correct"-ly
setting
)

Implements jdk.internal.joptsimple.OptionDeclarer.posixlyCorrect.

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 options
recognize
)

Implements jdk.internal.joptsimple.OptionDeclarer.recognizeAlternativeLongOptions.

Tells the parser either to recognize or ignore -W-style long options.

public Map<String, OptionSpec<?>>

Returns:

a map containing all the configured options and their corresponding OptionSpec
recognizedOptions
()

Retrieves all options-spec pairings which have been configured for the parser in the same order as declared during training.

pack-priv void
requiredIf(List<String> precedentSynonyms, String required)

pack-priv void
requiredIf(List<String> precedentSynonyms, OptionSpec<?> required)

pack-priv void
requiredUnless(List<String> precedentSynonyms, String required)

pack-priv void
requiredUnless(List<String> precedentSynonyms, OptionSpec<?> required)

private void
private AbstractOptionSpec<?>
specFor(char option)

private AbstractOptionSpec<?>
specFor(String option)

private List<AbstractOptionSpec<?>>
private void
validateOptionCharacters(char[] options)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allowsUnrecognizedOptionsback to summary
private boolean allowsUnrecognizedOptions
availableIfback to summary
private final Map<List<String>, Set<OptionSpec<?>>> availableIf
availableUnlessback to summary
private final Map<List<String>, Set<OptionSpec<?>>> availableUnless
helpFormatterback to summary
private HelpFormatter helpFormatter
posixlyCorrectback to summary
private boolean posixlyCorrect
recognizedOptionsback to summary
private final OptionNameMap<AbstractOptionSpec<?>> recognizedOptions
requiredIfback to summary
private final Map<List<String>, Set<OptionSpec<?>>> requiredIf
requiredUnlessback to summary
private final Map<List<String>, Set<OptionSpec<?>>> requiredUnless
stateback to summary
private OptionParserState state
trainingOrderback to summary
private final ArrayList<AbstractOptionSpec<?>> trainingOrder

Constructor Detail

OptionParserback to summary
public OptionParser()

Creates an option parser that initially recognizes no options, and does not exhibit "POSIX-ly correct" behavior.

OptionParserback to summary
public OptionParser(boolean allowAbbreviations)

Creates an option parser that initially recognizes no options, and does not exhibit "POSIX-ly correct" behavior.

Parameters
allowAbbreviations:boolean

whether unambiguous abbreviations of long options should be recognized by the parser

OptionParserback 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 String.

Parameters
optionSpecification:String

an option specification

Exceptions
NullPointerException:
if optionSpecification is null
OptionException:
if the option specification contains illegal characters or otherwise cannot be recognized

Method Detail

_recognizedOptionsback to summary
private Map<String, AbstractOptionSpec<?>> _recognizedOptions()
acceptsback to summary
public OptionSpecBuilder accepts(String option)

Implements jdk.internal.joptsimple.OptionDeclarer.accepts.

Doc from jdk.internal.joptsimple.OptionDeclarer.accepts.

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:


  OptionDeclarer 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.

Parameters
option:String

the option to recognize

Returns:OptionSpecBuilder

an object that can be used to flesh out more detail about the option

acceptsback to summary
public OptionSpecBuilder accepts(String option, String description)

Implements jdk.internal.joptsimple.OptionDeclarer.accepts.

Doc from jdk.internal.joptsimple.OptionDeclarer.accepts.

Tells the parser to recognize the given option.

Parameters
option:String

the option to recognize

description:String

a string that describes the purpose of the option. This is used when generating help information about the parser.

Returns:OptionSpecBuilder

an object that can be used to flesh out more detail about the option

acceptsAllback to summary
public OptionSpecBuilder acceptsAll(List<String> options)

Implements jdk.internal.joptsimple.OptionDeclarer.acceptsAll.

Doc from jdk.internal.joptsimple.OptionDeclarer.acceptsAll.

Tells the parser to recognize the given options, and treat them as synonymous.

Parameters
options:List<String>

the options to recognize and treat as synonymous

Returns:OptionSpecBuilder

an object that can be used to flesh out more detail about the options

acceptsAllback to summary
public OptionSpecBuilder acceptsAll(List<String> options, String description)

Implements jdk.internal.joptsimple.OptionDeclarer.acceptsAll.

Doc from jdk.internal.joptsimple.OptionDeclarer.acceptsAll.

Tells the parser to recognize the given options, and treat them as synonymous.

Parameters
options:List<String>

the options to recognize and treat as synonymous

description:String

a string that describes the purpose of the option. This is used when generating help information about the parser.

Returns:OptionSpecBuilder

an object that can be used to flesh out more detail about the options

allowsUnrecognizedOptionsback to summary
public void allowsUnrecognizedOptions()

Implements jdk.internal.joptsimple.OptionDeclarer.allowsUnrecognizedOptions.

Doc from jdk.internal.joptsimple.OptionDeclarer.allowsUnrecognizedOptions.

Tells the parser to treat unrecognized options as non-option arguments.

If not called, then the parser raises an OptionException when it encounters an unrecognized option.

availableIfback to summary
pack-priv void availableIf(List<String> precedentSynonyms, String available)
availableIfback to summary
pack-priv void availableIf(List<String> precedentSynonyms, OptionSpec<?> available)
availableUnlessback to summary
pack-priv void availableUnless(List<String> precedentSynonyms, String available)
availableUnlessback to summary
pack-priv void availableUnless(List<String> precedentSynonyms, OptionSpec<?> available)
doesAllowsUnrecognizedOptionsback to summary
pack-priv boolean doesAllowsUnrecognizedOptions()
ensureAllowedOptionsback to summary
private void ensureAllowedOptions(OptionSet options)
ensureRequiredOptionsback to summary
private void ensureRequiredOptions(OptionSet options)
extractShortOptionsFromback to summary
private static char[] extractShortOptionsFrom(String argument)
formatHelpWithback to summary
public void formatHelpWith(HelpFormatter formatter)

Tells the parser to use the given formatter when asked to print help.

Parameters
formatter:HelpFormatter

the formatter to use for printing help

Exceptions
NullPointerException:
if the formatter is null
handleLongOptionTokenback to summary
pack-priv void handleLongOptionToken(String candidate, ArgumentList arguments, OptionSet detected)
handleNonOptionArgumentback to summary
pack-priv void handleNonOptionArgument(String candidate, ArgumentList arguments, OptionSet detectedOptions)
handleShortOptionClusterback to summary
private void handleShortOptionCluster(String candidate, ArgumentList arguments, OptionSet detected)
handleShortOptionTokenback to summary
pack-priv void handleShortOptionToken(String candidate, ArgumentList arguments, OptionSet detected)
isHelpOptionPresentback to summary
private boolean isHelpOptionPresent(OptionSet options)
isRecognizedback to summary
pack-priv boolean isRecognized(String option)
looksLikeAnOptionback to summary
pack-priv boolean looksLikeAnOption(String argument)
missingRequiredOptionsback to summary
private List<AbstractOptionSpec<?>> missingRequiredOptions(OptionSet options)
mutuallyExclusiveback to summary
public void mutuallyExclusive(OptionSpecBuilder... specs)

Mandates mutual exclusiveness for the options built by the specified builders.

Parameters
specs:OptionSpecBuilder[]

descriptors for options that should be mutually exclusive on a command line.

Exceptions
NullPointerException:
if specs is null
noMoreOptionsback to summary
pack-priv void noMoreOptions()
nonOptionsback to summary
public NonOptionArgumentSpec<String> nonOptions()

Implements jdk.internal.joptsimple.OptionDeclarer.nonOptions.

Doc from jdk.internal.joptsimple.OptionDeclarer.nonOptions.

Gives an object that represents an access point for non-option arguments on a command line.

Returns:NonOptionArgumentSpec<String>

an object that can be used to flesh out more detail about the non-option arguments

nonOptionsback to summary
public NonOptionArgumentSpec<String> nonOptions(String description)

Implements jdk.internal.joptsimple.OptionDeclarer.nonOptions.

Doc from jdk.internal.joptsimple.OptionDeclarer.nonOptions.

Gives an object that represents an access point for non-option arguments on a command line.

Parameters
description:String

a string that describes the purpose of the non-option arguments. This is used when generating help information about the parser.

Returns:NonOptionArgumentSpec<String>

an object that can be used to flesh out more detail about the non-option arguments

optionsHasAnyOfback to summary
private boolean optionsHasAnyOf(OptionSet options, Collection<OptionSpec<?>> specs)
parseback to summary
public OptionSet parse(String... arguments)

Parses the given command line arguments according to the option specifications given to the parser.

Parameters
arguments:String[]

arguments to parse

Returns:OptionSet

an OptionSet describing the parsed options, their arguments, and any non-option arguments found

Exceptions
OptionException:
if problems are detected while parsing
NullPointerException:
if the argument list is null
parseLongOptionWithArgumentback to summary
private static KeyValuePair parseLongOptionWithArgument(String argument)
parseShortOptionWithArgumentback to summary
private static KeyValuePair parseShortOptionWithArgument(String argument)
posixlyCorrectback to summary
public void posixlyCorrect(boolean setting)

Implements jdk.internal.joptsimple.OptionDeclarer.posixlyCorrect.

Doc from jdk.internal.joptsimple.OptionDeclarer.posixlyCorrect.

Tells the parser whether or not to behave "POSIX-ly correct"-ly.

Parameters
setting:boolean

true if the parser should behave "POSIX-ly correct"-ly

posixlyCorrectback to summary
pack-priv boolean posixlyCorrect()
printHelpOnback 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.

Parameters
sink:OutputStream

the sink to write information to

Exceptions
IOException:
if there is a problem writing to the sink
NullPointerException:
if sink is null
See Also
printHelpOn(Writer)
printHelpOnback 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.

Parameters
sink:Writer

the sink to write information to

Exceptions
IOException:
if there is a problem writing to the sink
NullPointerException:
if sink is null
See Also
printHelpOn(OutputStream)
putDependentOptionback to summary
private void putDependentOption(List<String> precedentSynonyms, OptionSpec<?> required, Map<List<String>, Set<OptionSpec<?>>> target)
recognizeback to summary
pack-priv void recognize(AbstractOptionSpec<?> spec)
recognizeAlternativeLongOptionsback to summary
public void recognizeAlternativeLongOptions(boolean recognize)

Implements jdk.internal.joptsimple.OptionDeclarer.recognizeAlternativeLongOptions.

Doc from jdk.internal.joptsimple.OptionDeclarer.recognizeAlternativeLongOptions.

Tells the parser either to recognize or ignore -W-style long options.

Parameters
recognize:boolean

true if the parser is to recognize the special style of long options

recognizedOptionsback to summary
public Map<String, OptionSpec<?>> recognizedOptions()

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 OptionSpec#options(); only the order of the specs is preserved. (Note: prior to 4.7 the order was alphabetical across all options regardless of spec.)

Returns:Map<String, OptionSpec<?>>

a map containing all the configured options and their corresponding OptionSpec

Since
4.6
requiredIfback to summary
pack-priv void requiredIf(List<String> precedentSynonyms, String required)
requiredIfback to summary
pack-priv void requiredIf(List<String> precedentSynonyms, OptionSpec<?> required)
requiredUnlessback to summary
pack-priv void requiredUnless(List<String> precedentSynonyms, String required)
requiredUnlessback to summary
pack-priv void requiredUnless(List<String> precedentSynonyms, OptionSpec<?> required)
resetback to summary
private void reset()
specForback to summary
private AbstractOptionSpec<?> specFor(char option)
specForback to summary
private AbstractOptionSpec<?> specFor(String option)
unavailableOptionsback to summary
private List<AbstractOptionSpec<?>> unavailableOptions(OptionSet options)
validateOptionCharactersback to summary
private void validateOptionCharacters(char[] options)