Class OptionSpecBuilder
- java.lang.Object
-
- joptsimple.AbstractOptionSpec<Void>
-
- joptsimple.OptionSpecBuilder
-
- All Implemented Interfaces:
OptionDescriptor,OptionSpec<Void>
public class OptionSpecBuilder extends AbstractOptionSpec<Void>
Allows callers to specify whether a given option accepts arguments (required or optional).Instances are returned from
OptionParser.accepts(String)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 accept no argument.
Note that you should not use the fluent interface clauses in a way that would defeat the typing of option arguments:
OptionParser parser = new OptionParser(); ArgumentAcceptingOptionSpec<String> optionC = parser.accepts( "c" ).withRequiredArg(); optionC.ofType( Integer.class ); // DON'T THROW AWAY THE TYPE! String value = parser.parse( "-c", "2" ).valueOf( optionC ); // ClassCastException- Author:
- Paul Holser
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanacceptsArguments()Does this option accept arguments?StringargumentDescription()Gives a short description of the option's argument.StringargumentTypeIndicator()Gives an indication of the expected type of the option's argument.OptionSpecBuilderavailableIf(String dependent, String... otherDependents)Informs an option parser that this builder's option is allowed if the given option is present on the command line.OptionSpecBuilderavailableIf(OptionSpec<?> dependent, OptionSpec<?>... otherDependents)Informs an option parser that this builder's option is allowed if the given option is present on the command line.OptionSpecBuilderavailableUnless(String dependent, String... otherDependents)Informs an option parser that this builder's option is allowed if the given option is absent on the command line.OptionSpecBuilderavailableUnless(OptionSpec<?> dependent, OptionSpec<?>... otherDependents)Informs an option parser that this builder's option is allowed if the given option is absent on the command line.protected Voidconvert(String argument)List<Void>defaultValues()What values will the option take if none are specified on the command line?booleanisRequired()Is this option required on a command line?OptionSpecBuilderrequiredIf(String dependent, String... otherDependents)Informs an option parser that this builder's option is required if the given option is present on the command line.OptionSpecBuilderrequiredIf(OptionSpec<?> dependent, OptionSpec<?>... otherDependents)Informs an option parser that this builder's option is required if the given option is present on the command line.OptionSpecBuilderrequiredUnless(String dependent, String... otherDependents)Informs an option parser that this builder's option is required if the given option is absent on the command line.OptionSpecBuilderrequiredUnless(OptionSpec<?> dependent, OptionSpec<?>... otherDependents)Informs an option parser that this builder's option is required if the given option is absent on the command line.booleanrequiresArgument()Does this option require an argument?ArgumentAcceptingOptionSpec<String>withOptionalArg()Informs an option parser that this builder's option accepts an optional argument.ArgumentAcceptingOptionSpec<String>withRequiredArg()Informs an option parser that this builder's option requires an argument.-
Methods inherited from class joptsimple.AbstractOptionSpec
argumentTypeIndicatorFrom, convertWith, description, equals, forHelp, hashCode, isForHelp, options, representsNonOptions, toString, value, values
-
-
-
-
Method Detail
-
withRequiredArg
public ArgumentAcceptingOptionSpec<String> withRequiredArg()
Informs an option parser that this builder's option requires an argument.- Returns:
- a specification for the option
-
withOptionalArg
public ArgumentAcceptingOptionSpec<String> withOptionalArg()
Informs an option parser that this builder's option accepts an optional argument.- Returns:
- a specification for the option
-
requiredIf
public OptionSpecBuilder requiredIf(String dependent, String... otherDependents)
Informs an option parser that this builder's option is required if the given option is present on the command line.
For a given option, you should not mix this with
requiredUnlessto avoid conflicts.- Parameters:
dependent- an option whose presence on a command line makes this builder's option requiredotherDependents- other options whose presence on a command line makes this builder's option required- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
- Throws:
OptionException- if any of the dependent options haven't been configured in the parser yet
-
requiredIf
public OptionSpecBuilder requiredIf(OptionSpec<?> dependent, OptionSpec<?>... otherDependents)
Informs an option parser that this builder's option is required if the given option is present on the command line.
For a given option, you should not mix this with
requiredUnlessto avoid conflicts.This method recognizes only instances of options returned from the fluent interface methods.
- Parameters:
dependent- the option whose presence on a command line makes this builder's option requiredotherDependents- other options whose presence on a command line makes this builder's option required- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
-
requiredUnless
public OptionSpecBuilder requiredUnless(String dependent, String... otherDependents)
Informs an option parser that this builder's option is required if the given option is absent on the command line.
For a given option, you should not mix this with
requiredIfto avoid conflicts.- Parameters:
dependent- an option whose absence on a command line makes this builder's option requiredotherDependents- other options whose absence on a command line makes this builder's option required- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
- Throws:
OptionException- if any of the dependent options haven't been configured in the parser yet
-
requiredUnless
public OptionSpecBuilder requiredUnless(OptionSpec<?> dependent, OptionSpec<?>... otherDependents)
Informs an option parser that this builder's option is required if the given option is absent on the command line.
For a given option, you should not mix this with
requiredIfto avoid conflicts.This method recognizes only instances of options returned from the fluent interface methods.
- Parameters:
dependent- the option whose absence on a command line makes this builder's option requiredotherDependents- other options whose absence on a command line makes this builder's option required- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
-
availableIf
public OptionSpecBuilder availableIf(String dependent, String... otherDependents)
Informs an option parser that this builder's option is allowed if the given option is present on the command line.
For a given option, you should not mix this with
availableUnlessto avoid conflicts.- Parameters:
dependent- an option whose presence on a command line makes this builder's option allowedotherDependents- other options whose presence on a command line makes this builder's option allowed- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
- Throws:
OptionException- if any of the dependent options haven't been configured in the parser yet
-
availableIf
public OptionSpecBuilder availableIf(OptionSpec<?> dependent, OptionSpec<?>... otherDependents)
Informs an option parser that this builder's option is allowed if the given option is present on the command line.
For a given option, you should not mix this with
requiredUnlessto avoid conflicts.This method recognizes only instances of options returned from the fluent interface methods.
- Parameters:
dependent- the option whose presence on a command line makes this builder's option allowedotherDependents- other options whose presence on a command line makes this builder's option allowed- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
-
availableUnless
public OptionSpecBuilder availableUnless(String dependent, String... otherDependents)
Informs an option parser that this builder's option is allowed if the given option is absent on the command line.
For a given option, you should not mix this with
requiredIfto avoid conflicts.- Parameters:
dependent- an option whose absence on a command line makes this builder's option allowedotherDependents- other options whose absence on a command line makes this builder's option allowed- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
- Throws:
OptionException- if any of the dependent options haven't been configured in the parser yet
-
availableUnless
public OptionSpecBuilder availableUnless(OptionSpec<?> dependent, OptionSpec<?>... otherDependents)
Informs an option parser that this builder's option is allowed if the given option is absent on the command line.
For a given option, you should not mix this with
requiredIfto avoid conflicts.This method recognizes only instances of options returned from the fluent interface methods.
- Parameters:
dependent- the option whose absence on a command line makes this builder's option allowedotherDependents- other options whose absence on a command line makes this builder's option allowed- Returns:
- self, so that the caller can add clauses to the fluent interface sentence
-
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
-
isRequired
public boolean isRequired()
Description copied from interface:OptionDescriptorIs this option required on a command line?- Returns:
- whether the option is required
-
argumentDescription
public 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 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
-
convert
protected Void convert(String argument)
- Specified by:
convertin classAbstractOptionSpec<Void>
-
defaultValues
public List<Void> 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
-
-