Class ArgumentAcceptingOptionSpec<V>
- java.lang.Object
-
- joptsimple.AbstractOptionSpec<V>
-
- joptsimple.ArgumentAcceptingOptionSpec<V>
-
- Type Parameters:
V- represents the type of the arguments this option accepts
- All Implemented Interfaces:
OptionDescriptor,OptionSpec<V>
- Direct Known Subclasses:
AlternativeLongOptionSpec,OptionalArgumentOptionSpec,RequiredArgumentOptionSpec
public abstract class ArgumentAcceptingOptionSpec<V> extends AbstractOptionSpec<V>
Specification of an option that accepts an argument.
Instances are returned from
OptionSpecBuildermethods 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.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringargumentDescriptionprivate booleanargumentRequiredprivate ValueConverter<V>converterprivate java.util.List<V>defaultValuesprivate static charNIL_VALUE_SEPARATORprivate booleanoptionRequiredprivate java.lang.StringvalueSeparator
-
Constructor Summary
Constructors Constructor Description ArgumentAcceptingOptionSpec(java.lang.String option, boolean argumentRequired)ArgumentAcceptingOptionSpec(java.util.List<java.lang.String> options, boolean argumentRequired, java.lang.String description)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanacceptsArguments()Does this option accept arguments?protected voidaddArguments(OptionSet detectedOptions, java.lang.String detectedArgument)private voidaddDefaultValue(V value)java.lang.StringargumentDescription()Gives a short description of the option's argument.java.lang.StringargumentTypeIndicator()Gives an indication of the expected type of the option's argument.protected booleancanConvertArgument(java.lang.String argument)protected Vconvert(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 voiddetectOptionArgument(OptionParser parser, ArgumentList arguments, OptionSet detectedOptions)booleanequals(java.lang.Object that)(package private) voidhandleOption(OptionParser parser, ArgumentList arguments, OptionSet detectedOptions, java.lang.String detectedArgument)inthashCode()protected booleanisArgumentOfNumberType()booleanisRequired()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.booleanrequiresArgument()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.-
Methods inherited from class joptsimple.AbstractOptionSpec
argumentTypeIndicatorFrom, convertWith, description, forHelp, isForHelp, options, representsNonOptions, toString, value, values
-
-
-
-
Field Detail
-
NIL_VALUE_SEPARATOR
private static final char NIL_VALUE_SEPARATOR
- See Also:
- Constant Field Values
-
argumentRequired
private final boolean argumentRequired
-
defaultValues
private final java.util.List<V> defaultValues
-
optionRequired
private boolean optionRequired
-
converter
private ValueConverter<V> converter
-
argumentDescription
private java.lang.String argumentDescription
-
valueSeparator
private java.lang.String valueSeparator
-
-
Method Detail
-
ofType
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:
- 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:
java.lang.NullPointerException- if the type isnulljava.lang.IllegalArgumentException- if the type does not have the standard conversion methods
- a public static method called
-
withValuesConvertedBy
public 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).- 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:
java.lang.NullPointerException- if the converter isnull
-
describedAs
public 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.
- 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
public 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.
- Parameters:
separator- a character separator- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
- Throws:
java.lang.IllegalArgumentException- if the separator is Unicode U+0000
-
withValuesSeparatedBy
public 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.
- Parameters:
separator- a string separator- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
- Throws:
java.lang.IllegalArgumentException- if the separator contains Unicode U+0000
-
defaultsTo
@SafeVarargs public final ArgumentAcceptingOptionSpec<V> defaultsTo(V value, V... values)
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:
java.lang.NullPointerException- ifvalue,values, or any elements ofvaluesarenull
-
defaultsTo
public ArgumentAcceptingOptionSpec<V> defaultsTo(V[] values)
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:
java.lang.NullPointerException- ifvaluesor any elements ofvaluesarenull
-
required
public ArgumentAcceptingOptionSpec<V> 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
-
addDefaultValue
private void addDefaultValue(V value)
-
handleOption
final void handleOption(OptionParser parser, ArgumentList arguments, OptionSet detectedOptions, java.lang.String detectedArgument)
- Specified by:
handleOptionin classAbstractOptionSpec<V>
-
addArguments
protected void addArguments(OptionSet detectedOptions, java.lang.String detectedArgument)
-
detectOptionArgument
protected abstract void detectOptionArgument(OptionParser parser, ArgumentList arguments, OptionSet detectedOptions)
-
convert
protected final V convert(java.lang.String argument)
- Specified by:
convertin classAbstractOptionSpec<V>
-
canConvertArgument
protected boolean canConvertArgument(java.lang.String argument)
-
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
public java.lang.String argumentDescription()
Description copied from interface:OptionDescriptorGives a short description of the option's argument.- Returns:
- a description for the option's argument
-
argumentTypeIndicator
public java.lang.String 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
public java.util.List<V> 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
public boolean equals(java.lang.Object that)
- Overrides:
equalsin classAbstractOptionSpec<V>
-
hashCode
public int hashCode()
- Overrides:
hashCodein classAbstractOptionSpec<V>
-
-