Class OptionSpecBuilder
- All Implemented Interfaces:
OptionDescriptor
,OptionSpec<Void>
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
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionOptionSpecBuilder
(OptionParser parser, List<String> options, String description) -
Method Summary
Modifier and TypeMethodDescriptionprivate void
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.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.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.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.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.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.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.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.validatedDependents
(String dependent, String... otherDependents) Informs an option parser that this builder's option accepts an optional argument.Informs an option parser that this builder's option requires an argument.Methods inherited from class joptsimple.NoArgumentOptionSpec
acceptsArguments, argumentDescription, argumentTypeIndicator, convert, defaultValues, handleOption, isRequired, requiresArgument
Methods inherited from class joptsimple.AbstractOptionSpec
argumentTypeIndicatorFrom, convertWith, description, equals, forHelp, hashCode, isForHelp, options, representsNonOptions, toString, value, values
-
Field Details
-
parser
-
-
Constructor Details
-
OptionSpecBuilder
OptionSpecBuilder(OptionParser parser, List<String> options, String description)
-
-
Method Details
-
attachToParser
private void attachToParser() -
withRequiredArg
Informs an option parser that this builder's option requires an argument.- Returns:
- a specification for the option
-
withOptionalArg
Informs an option parser that this builder's option accepts an optional argument.- Returns:
- a specification for the option
-
requiredIf
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
requiredUnless
to 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
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
requiredUnless
to 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
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
requiredIf
to 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
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
requiredIf
to 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
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
availableUnless
to 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
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
requiredUnless
to 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
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
requiredIf
to 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
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
requiredIf
to 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
-
validatedDependents
-