Package joptsimple

Class OptionSpecBuilder

java.lang.Object
joptsimple.OptionSpecBuilder
All Implemented Interfaces:
OptionDescriptor, OptionSpec<java.lang.Void>

public class OptionSpecBuilder
extends java.lang.Object
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
 
  • Method Summary

    Modifier and Type Method Description
    boolean acceptsArguments()
    Does this option accept arguments?
    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 java.lang.Void convert​(java.lang.String argument)  
    java.util.List<java.lang.Void> defaultValues()
    What values will the option take if none are specified on the command line?
    java.lang.String description()
    Description of this option's purpose.
    boolean equals​(java.lang.Object that)  
    joptsimple.AbstractOptionSpec<java.lang.Void> forHelp()  
    int hashCode()  
    boolean isForHelp()
    Tells whether this option is designated as a "help" option.
    boolean isRequired()
    Is this option required on a command line?
    java.util.Collection<java.lang.String> options()
    A set of options that are mutually synonymous.
    OptionSpecBuilder requiredIf​(java.lang.String dependent, java.lang.String... otherDependents)
    Informs an option parser that this builder's option is required if the given option is present on the command line.
    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.
    boolean requiresArgument()
    Does this option require an argument?
    java.lang.String toString()  
    java.lang.Void value​(OptionSet detectedOptions)
    Gives the argument associated with the given option in the given set of detected options.
    java.util.List<java.lang.Void> values​(OptionSet detectedOptions)
    Gives any arguments associated with the given option in the given set of detected options.
    ArgumentAcceptingOptionSpec<java.lang.String> withOptionalArg()
    Informs an option parser that this builder's option accepts an optional argument.
    ArgumentAcceptingOptionSpec<java.lang.String> withRequiredArg()
    Informs an option parser that this builder's option requires an argument.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • withRequiredArg

      public ArgumentAcceptingOptionSpec<java.lang.String> withRequiredArg()
      Informs an option parser that this builder's option requires an argument.
      Returns:
      a specification for the option
    • withOptionalArg

      public ArgumentAcceptingOptionSpec<java.lang.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​(java.lang.String dependent, java.lang.String... otherDependents)
      Informs an option parser that this builder's option is required if the given option is present on the command line.
      Parameters:
      dependent - an option whose presence on a command line makes this builder's option required
      otherDependents - 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.

      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 required
      otherDependents - 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
    • acceptsArguments

      public boolean acceptsArguments()
      Description copied from interface: OptionDescriptor
      Does this option accept arguments?
      Returns:
      whether the option accepts arguments
    • requiresArgument

      public boolean requiresArgument()
      Description copied from interface: OptionDescriptor
      Does this option require an argument?
      Returns:
      whether the option requires an argument
    • isRequired

      public boolean isRequired()
      Description copied from interface: OptionDescriptor
      Is this option required on a command line?
      Returns:
      whether the option is required
    • argumentDescription

      public java.lang.String argumentDescription()
      Description copied from interface: OptionDescriptor
      Gives 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: OptionDescriptor
      Gives an indication of the expected type of the option's argument.
      Returns:
      a description for the option's argument type
    • convert

      protected java.lang.Void convert​(java.lang.String argument)
    • defaultValues

      public java.util.List<java.lang.Void> defaultValues()
      Description copied from interface: OptionDescriptor
      What values will the option take if none are specified on the command line?
      Returns:
      any default values for the option
    • options

      public final java.util.Collection<java.lang.String> options()
      Description copied from interface: OptionDescriptor
      A set of options that are mutually synonymous.
      Specified by:
      options in interface OptionDescriptor
      Specified by:
      options in interface OptionSpec<V>
      Returns:
      the string representations of this option
    • values

      public final java.util.List<java.lang.Void> values​(OptionSet detectedOptions)
      Description copied from interface: OptionSpec
      Gives 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:
      values in interface OptionSpec<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:
      OptionSet.valuesOf(OptionSpec)
    • value

      public final java.lang.Void value​(OptionSet detectedOptions)
      Description copied from interface: OptionSpec
      Gives 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:
      value in interface OptionSpec<V>
      Parameters:
      detectedOptions - the detected options to search in
      Returns:
      the argument of the this option; null if no argument is present, or that option was not detected
      See Also:
      OptionSet.valueOf(OptionSpec)
    • description

      public java.lang.String description()
      Description copied from interface: OptionDescriptor
      Description of this option's purpose.
      Specified by:
      description in interface OptionDescriptor
      Returns:
      a description for the option
    • forHelp

      public final joptsimple.AbstractOptionSpec<java.lang.Void> forHelp()
    • isForHelp

      public final boolean isForHelp()
      Description copied from interface: OptionSpec
      Tells 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:
      isForHelp in interface OptionSpec<V>
      Returns:
      whether this option is designated as a "help" option
    • equals

      public boolean equals​(java.lang.Object that)
      Overrides:
      equals in class java.lang.Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class java.lang.Object
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object