Class ArgumentAcceptingOptionSpec<V>
- Type Parameters:
V- represents the type of the arguments this option accepts
- All Implemented Interfaces:
OptionDescriptor, OptionSpec<V>
Specification of an option that accepts an argument.
Instances are returned from OptionSpecBuilder methods 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 an instance of this class, then that instance's option will treat its argument as
a String.
-
Method Summary
Modifier and TypeMethodDescriptionbooleanDoes this option accept arguments?protected voidaddArguments(OptionSet detectedOptions, String detectedArgument) Gives a short description of the option's argument.Gives an indication of the expected type of the option's argument.protected booleancanConvertArgument(String argument) protected final VdefaultsTo(V[] values) Specifies a set of default values for the argument of the option that this spec represents.defaultsTo(V value, V... values) Specifies a set of default values for the argument of the option that this spec represents.What values will the option take if none are specified on the command line?final ArgumentAcceptingOptionSpec<V> describedAs(String description) Specifies a description for the argument of the option that this spec represents.Description of this option's purpose.protected abstract voiddetectOptionArgument(OptionParser parser, joptsimple.ArgumentList arguments, OptionSet detectedOptions) booleanfinal joptsimple.AbstractOptionSpec<V> forHelp()inthashCode()protected booleanfinal booleanTells whether this option is designated as a "help" option.booleanIs this option required on a command line?final <T> ArgumentAcceptingOptionSpec<T> Specifies a type to which arguments of this spec's option are to be converted.final Collection<String> options()A set of options that are mutually synonymous.required()Marks this option as required.booleanDoes this option require an argument?toString()final VGives the argument associated with the given option in the given set of detected options.Gives any arguments associated with the given option in the given set of detected options.final <T> ArgumentAcceptingOptionSpec<T> withValuesConvertedBy(ValueConverter<T> aConverter) Specifies a converter to use to translate arguments of this spec's option into Java objects.final ArgumentAcceptingOptionSpec<V> withValuesSeparatedBy(char separator) Specifies a value separator for the argument of the option that this spec represents.final ArgumentAcceptingOptionSpec<V> withValuesSeparatedBy(String separator) Specifies a value separator for the argument of the option that this spec represents.
-
Method Details
-
ofType
Specifies a type to which arguments of this spec's option are to be converted.
JOpt Simple accepts types that have either:
- a public static method called
valueOfwhich accepts a single argument of typeStringand whose return type is the same as the class on which the method is declared. Thejava.langprimitive wrapper classes have such methods. - a public constructor which accepts a single argument of type
String.
This class converts arguments using those methods in that order; that is,
valueOfwould be invoked before a one-String-arg constructor would.Invoking this method will trump any previous calls to this method or to
withValuesConvertedBy(ValueConverter).- Type Parameters:
T- represents the runtime class of the desired option argument type- Parameters:
argumentType- desired type of arguments to this spec's option- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
- Throws:
NullPointerException- if the type isnullIllegalArgumentException- if the type does not have the standard conversion methods
- a public static method called
-
withValuesConvertedBy
Specifies a converter to use to translate arguments of this spec's option into Java objects. This is useful when converting to types that do not have the requisite factory method or constructor for
ofType(Class).Invoking this method will trump any previous calls to this method or to
ofType(Class).- Type Parameters:
T- represents the runtime class of the desired option argument type- Parameters:
aConverter- the converter to use- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
- Throws:
NullPointerException- if the converter isnull
-
describedAs
Specifies a description for the argument of the option that this spec represents. This description is used when generating help information about the parser.
- Parameters:
description- describes the nature of the argument of this spec's option- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
-
withValuesSeparatedBy
Specifies a value separator for the argument of the option that this spec represents. This allows a single option argument to represent multiple values for the option. For example:
parser.accepts( "z" ).withRequiredArg() .withValuesSeparatedBy( ',' ); OptionSet options = parser.parse( new String[] { "-z", "foo,bar,baz", "-z", "fizz", "-z", "buzz" } );Then
options.valuesOf( "z" )would yield the list[foo, bar, baz, fizz, buzz].You cannot use Unicode U+0000 as the separator.
- Parameters:
separator- a character separator- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
- Throws:
IllegalArgumentException- if the separator is Unicode U+0000
-
withValuesSeparatedBy
Specifies a value separator for the argument of the option that this spec represents. This allows a single option argument to represent multiple values for the option. For example:
parser.accepts( "z" ).withRequiredArg() .withValuesSeparatedBy( ":::" ); OptionSet options = parser.parse( new String[] { "-z", "foo:::bar:::baz", "-z", "fizz", "-z", "buzz" } );Then
options.valuesOf( "z" )would yield the list[foo, bar, baz, fizz, buzz].You cannot use Unicode U+0000 in the separator.
- Parameters:
separator- a string separator- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
- Throws:
IllegalArgumentException- if the separator contains Unicode U+0000
-
defaultsTo
Specifies a set of default values for the argument of the option that this spec represents.- Parameters:
value- the first in the set of default argument values for this spec's optionvalues- the (optional) remainder of the set of default argument values for this spec's option- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
- Throws:
NullPointerException- ifvalue,values, or any elements ofvaluesarenull
-
defaultsTo
Specifies a set of default values for the argument of the option that this spec represents.- Parameters:
values- the set of default argument values for this spec's option- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
- Throws:
NullPointerException- ifvaluesor any elements ofvaluesarenull
-
required
Marks this option as required. AnOptionExceptionwill be thrown whenOptionParser.parse(java.lang.String...)is called, if an option is marked as required and not specified on the command line.- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
-
isRequired
public boolean isRequired()Description copied from interface:OptionDescriptorIs this option required on a command line?- Returns:
- whether the option is required
-
addArguments
-
detectOptionArgument
protected abstract void detectOptionArgument(OptionParser parser, joptsimple.ArgumentList arguments, OptionSet detectedOptions) -
convert
-
canConvertArgument
-
isArgumentOfNumberType
protected boolean isArgumentOfNumberType() -
acceptsArguments
public boolean acceptsArguments()Description copied from interface:OptionDescriptorDoes this option accept arguments?- Returns:
- whether the option accepts arguments
-
requiresArgument
public boolean requiresArgument()Description copied from interface:OptionDescriptorDoes this option require an argument?- Returns:
- whether the option requires an argument
-
argumentDescription
Description copied from interface:OptionDescriptorGives a short description of the option's argument.- Returns:
- a description for the option's argument
-
argumentTypeIndicator
Description copied from interface:OptionDescriptorGives an indication of the expected type of the option's argument.- Returns:
- a description for the option's argument type
-
defaultValues
Description copied from interface:OptionDescriptorWhat values will the option take if none are specified on the command line?- Returns:
- any default values for the option
-
equals
-
hashCode
public int hashCode() -
options
Description copied from interface:OptionDescriptorA set of options that are mutually synonymous.- Specified by:
optionsin interfaceOptionDescriptor- Specified by:
optionsin interfaceOptionSpec<V>- Returns:
- the string representations of this option
-
values
Description copied from interface:OptionSpecGives any arguments associated with the given option in the given set of detected options.Specifying a default argument value for this option will cause this method to return that default value even if this option was not detected on the command line, or if this option can take an optional argument but did not have one on the command line.
- Specified by:
valuesin interfaceOptionSpec<V>- Parameters:
detectedOptions- the detected options to search in- Returns:
- the arguments associated with this option; an empty list if no such arguments are present, or if this option was not detected
- See Also:
-
value
Description copied from interface:OptionSpecGives the argument associated with the given option in the given set of detected options.Specifying a default argument value for this option will cause this method to return that default value even if this option was not detected on the command line, or if this option can take an optional argument but did not have one on the command line.
- Specified by:
valuein interfaceOptionSpec<V>- Parameters:
detectedOptions- the detected options to search in- Returns:
- the argument of the this option;
nullif no argument is present, or that option was not detected - See Also:
-
description
Description copied from interface:OptionDescriptorDescription of this option's purpose.- Specified by:
descriptionin interfaceOptionDescriptor- Returns:
- a description for the option
-
forHelp
-
isForHelp
public final boolean isForHelp()Description copied from interface:OptionSpecTells whether this option is designated as a "help" option. The presence of a "help" option on a command line means that missing "required" options will not cause parsing to fail.- Specified by:
isForHelpin interfaceOptionSpec<V>- Returns:
- whether this option is designated as a "help" option
-
toString
-