Class OptionSet
- java.lang.Object
-
- joptsimple.OptionSet
-
public class OptionSet extends java.lang.Object
Representation of a group of detected command line options, their arguments, and non-option arguments.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.String,java.util.List<?>>
defaultValues
private java.util.Map<java.lang.String,AbstractOptionSpec<?>>
detectedOptions
private java.util.List<OptionSpec<?>>
detectedSpecs
private java.util.Map<AbstractOptionSpec<?>,java.util.List<java.lang.String>>
optionsToArguments
private java.util.Map<java.lang.String,AbstractOptionSpec<?>>
recognizedSpecs
-
Constructor Summary
Constructors Constructor Description OptionSet(java.util.Map<java.lang.String,AbstractOptionSpec<?>> recognizedSpecs)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) void
add(AbstractOptionSpec<?> spec)
(package private) void
addWithArgument(AbstractOptionSpec<?> spec, java.lang.String argument)
java.util.Map<OptionSpec<?>,java.util.List<?>>
asMap()
Gives all declared options as a map of string to OptionSpec.private <V> java.util.List<V>
defaultValueFor(OptionSpec<V> option)
private static java.util.Map<java.lang.String,java.util.List<?>>
defaultValues(java.util.Map<java.lang.String,AbstractOptionSpec<?>> recognizedSpecs)
private <V> java.util.List<V>
defaultValuesFor(java.lang.String option)
boolean
equals(java.lang.Object that)
boolean
has(java.lang.String option)
Tells whether the given option was detected.boolean
has(OptionSpec<?> option)
Tells whether the given option was detected.boolean
hasArgument(java.lang.String option)
Tells whether there are any arguments associated with the given option.boolean
hasArgument(OptionSpec<?> option)
Tells whether there are any arguments associated with the given option.int
hashCode()
boolean
hasOptions()
Tells whether any options were detected.java.util.List<?>
nonOptionArguments()
java.util.List<OptionSpec<?>>
specs()
Gives the set of options that were detected, in the form of OptionSpecs, in the order in which the options were found on the command line.java.lang.Object
valueOf(java.lang.String option)
Gives the argument associated with the given option.<V> V
valueOf(OptionSpec<V> option)
Gives the argument associated with the given option.java.util.List<?>
valuesOf(java.lang.String option)
Gives any arguments associated with the given option.<V> java.util.List<V>
valuesOf(OptionSpec<V> option)
Gives any arguments associated with the given option.
-
-
-
Field Detail
-
detectedSpecs
private final java.util.List<OptionSpec<?>> detectedSpecs
-
detectedOptions
private final java.util.Map<java.lang.String,AbstractOptionSpec<?>> detectedOptions
-
optionsToArguments
private final java.util.Map<AbstractOptionSpec<?>,java.util.List<java.lang.String>> optionsToArguments
-
recognizedSpecs
private final java.util.Map<java.lang.String,AbstractOptionSpec<?>> recognizedSpecs
-
defaultValues
private final java.util.Map<java.lang.String,java.util.List<?>> defaultValues
-
-
Constructor Detail
-
OptionSet
OptionSet(java.util.Map<java.lang.String,AbstractOptionSpec<?>> recognizedSpecs)
-
-
Method Detail
-
hasOptions
public boolean hasOptions()
Tells whether any options were detected.- Returns:
true
if any options were detected
-
has
public boolean has(java.lang.String option)
Tells whether the given option was detected.- Parameters:
option
- the option to search for- Returns:
true
if the option was detected- See Also:
has(OptionSpec)
-
has
public boolean has(OptionSpec<?> option)
Tells whether the given option was detected.This method recognizes only instances of options returned from the fluent interface methods.
Specifying a ArgumentAcceptingOptionSpec.defaultsTo(Object, Object[]) default argument value} for an option does not cause this method to return
true
if the option was not detected on the command line.- Parameters:
option
- the option to search for- Returns:
true
if the option was detected- See Also:
has(String)
-
hasArgument
public boolean hasArgument(java.lang.String option)
Tells whether there are any arguments associated with the given option.- Parameters:
option
- the option to search for- Returns:
true
if the option was detected and at least one argument was detected for the option- See Also:
hasArgument(OptionSpec)
-
hasArgument
public boolean hasArgument(OptionSpec<?> option)
Tells whether there are any arguments associated with the given option.This method recognizes only instances of options returned from the fluent interface methods.
Specifying a default argument value for an option does not cause this method to return
true
if the option was not detected on the command line, or if the option can take an optional argument but did not have one on the command line.- Parameters:
option
- the option to search for- Returns:
true
if the option was detected and at least one argument was detected for the option- Throws:
java.lang.NullPointerException
- ifoption
isnull
- See Also:
hasArgument(String)
-
valueOf
public java.lang.Object valueOf(java.lang.String option)
Gives the argument associated with the given option. If the option was given an argument type, the argument will take on that type; otherwise, it will be aString
.Specifying a default argument value for an option will cause this method to return that default value even if the option was not detected on the command line, or if the option can take an optional argument but did not have one on the command line.
- Parameters:
option
- the option to search for- Returns:
- the argument of the given option;
null
if no argument is present, or that option was not detected - Throws:
java.lang.NullPointerException
- ifoption
isnull
OptionException
- if more than one argument was detected for the option
-
valueOf
public <V> V valueOf(OptionSpec<V> option)
Gives the argument associated with the given option.This method recognizes only instances of options returned from the fluent interface methods.
- Type Parameters:
V
- represents the type of the arguments the given option accepts- Parameters:
option
- the option to search for- Returns:
- the argument of the given option;
null
if no argument is present, or that option was not detected - Throws:
OptionException
- if more than one argument was detected for the optionjava.lang.NullPointerException
- ifoption
isnull
java.lang.ClassCastException
- if the arguments of this option are not of the expected type
-
valuesOf
public java.util.List<?> valuesOf(java.lang.String option)
Gives any arguments associated with the given option. If the option was given an argument type, the arguments will take on that type; otherwise, they will be
String
s.- Parameters:
option
- the option to search for- Returns:
- the arguments associated with the option, as a list of objects of the type given to the arguments; an empty list if no such arguments are present, or if the option was not detected
- Throws:
java.lang.NullPointerException
- ifoption
isnull
-
valuesOf
public <V> java.util.List<V> valuesOf(OptionSpec<V> option)
Gives any arguments associated with the given option. If the option was given an argument type, the arguments will take on that type; otherwise, they will be
String
s.This method recognizes only instances of options returned from the fluent interface methods.
- Type Parameters:
V
- represents the type of the arguments the given option accepts- Parameters:
option
- the option to search for- Returns:
- the arguments associated with the option; an empty list if no such arguments are present, or if the option was not detected
- Throws:
java.lang.NullPointerException
- ifoption
isnull
OptionException
- if there is a problem converting the option's arguments to the desired type; for example, if the type does not implement a correct conversion constructor or method
-
specs
public java.util.List<OptionSpec<?>> specs()
Gives the set of options that were detected, in the form of OptionSpecs, in the order in which the options were found on the command line.- Returns:
- the set of detected command line options
-
asMap
public java.util.Map<OptionSpec<?>,java.util.List<?>> asMap()
Gives all declared options as a map of string to OptionSpec.- Returns:
- the declared options as a map
-
nonOptionArguments
public java.util.List<?> nonOptionArguments()
- Returns:
- the detected non-option arguments
-
add
void add(AbstractOptionSpec<?> spec)
-
addWithArgument
void addWithArgument(AbstractOptionSpec<?> spec, java.lang.String argument)
-
equals
public boolean equals(java.lang.Object that)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
defaultValuesFor
private <V> java.util.List<V> defaultValuesFor(java.lang.String option)
-
defaultValueFor
private <V> java.util.List<V> defaultValueFor(OptionSpec<V> option)
-
defaultValues
private static java.util.Map<java.lang.String,java.util.List<?>> defaultValues(java.util.Map<java.lang.String,AbstractOptionSpec<?>> recognizedSpecs)
-
-