V - represents the type of the arguments this option acceptspublic abstract class ArgumentAcceptingOptionSpec<V> extends AbstractOptionSpec<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.
| Modifier and Type | Field and Description |
|---|---|
private java.lang.String |
argumentDescription |
private boolean |
argumentRequired |
private ValueConverter<V> |
converter |
private java.util.List<V> |
defaultValues |
private static char |
NIL_VALUE_SEPARATOR |
private boolean |
optionRequired |
private java.lang.String |
valueSeparator |
| Constructor and Description |
|---|
ArgumentAcceptingOptionSpec(java.util.List<java.lang.String> options,
boolean argumentRequired,
java.lang.String description) |
ArgumentAcceptingOptionSpec(java.lang.String option,
boolean argumentRequired) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
acceptsArguments()
Does this option accept arguments?
|
protected void |
addArguments(OptionSet detectedOptions,
java.lang.String detectedArgument) |
private void |
addDefaultValue(V value) |
java.lang.String |
argumentDescription()
Gives a short description of the option's argument.
|
java.lang.String |
argumentTypeIndicator()
Gives an indication of the expected type of the option's
argument.
|
protected boolean |
canConvertArgument(java.lang.String argument) |
protected V |
convert(java.lang.String argument) |
ArgumentAcceptingOptionSpec<V> |
defaultsTo(V[] values)
Specifies a set of default values for the argument of the option that this spec represents.
|
ArgumentAcceptingOptionSpec<V> |
defaultsTo(V value,
V... values)
Specifies a set of default values for the argument of the option that this spec represents.
|
java.util.List<V> |
defaultValues()
What values will the option take if none are specified on the command line?
|
ArgumentAcceptingOptionSpec<V> |
describedAs(java.lang.String description)
Specifies a description for the argument of the option that this spec represents.
|
protected abstract void |
detectOptionArgument(OptionParser parser,
ArgumentList arguments,
OptionSet detectedOptions) |
boolean |
equals(java.lang.Object that) |
(package private) void |
handleOption(OptionParser parser,
ArgumentList arguments,
OptionSet detectedOptions,
java.lang.String detectedArgument) |
int |
hashCode() |
protected boolean |
isArgumentOfNumberType() |
boolean |
isRequired()
Is this option required on a command line?
|
<T> ArgumentAcceptingOptionSpec<T> |
ofType(java.lang.Class<T> argumentType)
Specifies a type to which arguments of this spec's option are to be converted.
|
ArgumentAcceptingOptionSpec<V> |
required()
Marks this option as required.
|
boolean |
requiresArgument()
Does this option require an argument?
|
<T> ArgumentAcceptingOptionSpec<T> |
withValuesConvertedBy(ValueConverter<T> aConverter)
Specifies a converter to use to translate arguments of this spec's option into Java objects.
|
ArgumentAcceptingOptionSpec<V> |
withValuesSeparatedBy(char separator)
Specifies a value separator for the argument of the option that this spec represents.
|
ArgumentAcceptingOptionSpec<V> |
withValuesSeparatedBy(java.lang.String separator)
Specifies a value separator for the argument of the option that this spec represents.
|
argumentTypeIndicatorFrom, convertWith, description, forHelp, isForHelp, options, representsNonOptions, toString, value, valuesprivate static final char NIL_VALUE_SEPARATOR
private final boolean argumentRequired
private final java.util.List<V> defaultValues
private boolean optionRequired
private ValueConverter<V> converter
private java.lang.String argumentDescription
private java.lang.String valueSeparator
ArgumentAcceptingOptionSpec(java.lang.String option,
boolean argumentRequired)
ArgumentAcceptingOptionSpec(java.util.List<java.lang.String> options,
boolean argumentRequired,
java.lang.String description)
public final <T> ArgumentAcceptingOptionSpec<T> ofType(java.lang.Class<T> argumentType)
Specifies a type to which arguments of this spec's option are to be converted.
JOpt Simple accepts types that have either:
valueOf which accepts a single argument of type String
and whose return type is the same as the class on which the method is declared. The java.lang
primitive wrapper classes have such methods.String.This class converts arguments using those methods in that order; that is, valueOf would be invoked
before a one-String-arg constructor would.
Invoking this method will trump any previous calls to this method or to
withValuesConvertedBy(ValueConverter).
T - represents the runtime class of the desired option argument typeargumentType - desired type of arguments to this spec's optionjava.lang.NullPointerException - if the type is nulljava.lang.IllegalArgumentException - if the type does not have the standard conversion methodspublic final <T> ArgumentAcceptingOptionSpec<T> withValuesConvertedBy(ValueConverter<T> aConverter)
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).
T - represents the runtime class of the desired option argument typeaConverter - the converter to usejava.lang.NullPointerException - if the converter is nullpublic final ArgumentAcceptingOptionSpec<V> describedAs(java.lang.String description)
Specifies a description for the argument of the option that this spec represents. This description is used when generating help information about the parser.
description - describes the nature of the argument of this spec's optionpublic final ArgumentAcceptingOptionSpec<V> withValuesSeparatedBy(char separator)
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.
separator - a character separatorjava.lang.IllegalArgumentException - if the separator is Unicode U+0000public final ArgumentAcceptingOptionSpec<V> withValuesSeparatedBy(java.lang.String separator)
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.
separator - a string separatorjava.lang.IllegalArgumentException - if the separator contains Unicode U+0000@SafeVarargs public final ArgumentAcceptingOptionSpec<V> defaultsTo(V value, V... values)
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 optionjava.lang.NullPointerException - if value, values, or any elements of values are
nullpublic ArgumentAcceptingOptionSpec<V> defaultsTo(V[] values)
values - the set of default argument values for this spec's optionjava.lang.NullPointerException - if values or any elements of values are nullpublic ArgumentAcceptingOptionSpec<V> required()
OptionException will be thrown when
OptionParser.parse(java.lang.String...) is called, if an option is marked as required and not specified
on the command line.public boolean isRequired()
OptionDescriptorprivate void addDefaultValue(V value)
final void handleOption(OptionParser parser, ArgumentList arguments, OptionSet detectedOptions, java.lang.String detectedArgument)
handleOption in class AbstractOptionSpec<V>protected void addArguments(OptionSet detectedOptions, java.lang.String detectedArgument)
protected abstract void detectOptionArgument(OptionParser parser, ArgumentList arguments, OptionSet detectedOptions)
protected final V convert(java.lang.String argument)
convert in class AbstractOptionSpec<V>protected boolean canConvertArgument(java.lang.String argument)
protected boolean isArgumentOfNumberType()
public boolean acceptsArguments()
OptionDescriptorpublic boolean requiresArgument()
OptionDescriptorpublic java.lang.String argumentDescription()
OptionDescriptorpublic java.lang.String argumentTypeIndicator()
OptionDescriptorpublic java.util.List<V> defaultValues()
OptionDescriptorpublic boolean equals(java.lang.Object that)
equals in class AbstractOptionSpec<V>public int hashCode()
hashCode in class AbstractOptionSpec<V>