joptsimple
Class ArgumentAcceptingOptionSpec

java.lang.Object
  extended by joptsimple.OptionSpec
      extended by joptsimple.ArgumentAcceptingOptionSpec
Direct Known Subclasses:
AlternativeLongOptionSpec, OptionalArgumentOptionSpec, RequiredArgumentOptionSpec

public abstract class ArgumentAcceptingOptionSpec
extends OptionSpec

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 domain-specific 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.

Since:
1.0
Version:
$Id: ArgumentAcceptingOptionSpec.java,v 1.7 2008/05/01 15:38:45 pholser Exp $
Author:
Paul Holser

Field Summary
private  java.lang.String argumentDescription
           
private  boolean argumentRequired
           
private  java.lang.reflect.Member converter
           
private static char NIL_VALUE_SEPARATOR
           
private  java.lang.String separator
           
 
Constructor Summary
ArgumentAcceptingOptionSpec(java.util.List options, boolean argumentRequired, java.lang.String description)
           
ArgumentAcceptingOptionSpec(java.lang.String option, boolean argumentRequired)
           
 
Method Summary
(package private)  boolean acceptsArguments()
           
protected  void addArguments(OptionSet detectedOptions, java.lang.String detectedArgument)
           
(package private)  java.lang.String argumentDescription()
           
(package private)  java.lang.Class argumentType()
           
protected  boolean canConvertArgument(java.lang.String argument)
           
protected  java.lang.Object convert(java.lang.String argument)
           
 ArgumentAcceptingOptionSpec 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()
           
 ArgumentAcceptingOptionSpec ofType(java.lang.Class argumentType)
          Specifies a type to which arguments of this spec's option are to be converted.
(package private)  boolean requiresArgument()
           
 ArgumentAcceptingOptionSpec withValuesSeparatedBy(char separator)
          Specifies a value separator for the argument of the option that this spec represents.
 
Methods inherited from class joptsimple.OptionSpec
accept, description, options
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NIL_VALUE_SEPARATOR

private static final char NIL_VALUE_SEPARATOR
See Also:
Constant Field Values

argumentRequired

private final boolean argumentRequired

converter

private java.lang.reflect.Member converter

argumentDescription

private java.lang.String argumentDescription

separator

private java.lang.String separator
Constructor Detail

ArgumentAcceptingOptionSpec

ArgumentAcceptingOptionSpec(java.lang.String option,
                            boolean argumentRequired)

ArgumentAcceptingOptionSpec

ArgumentAcceptingOptionSpec(java.util.List options,
                            boolean argumentRequired,
                            java.lang.String description)
Method Detail

ofType

public ArgumentAcceptingOptionSpec ofType(java.lang.Class argumentType)

Specifies a type to which arguments of this spec's option are to be converted.

JOpt Simple accepts types that have either:

  1. a public static method called 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.
  2. a public constructor which accepts a single argument of type String.

This class converts arguments using those methods in that order; that is, valueOf would be invoked before a one-String-arg constructor would.

Parameters:
argumentType - desired type of arguments to this spec's option
Returns:
self, so that the caller can add clauses to the domain-specific sentence
Throws:
java.lang.NullPointerException - if the type is null
java.lang.IllegalArgumentException - if the type does not have the standard conversion methods

describedAs

public final ArgumentAcceptingOptionSpec 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 domain-specific sentence

withValuesSeparatedBy

public final ArgumentAcceptingOptionSpec 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", "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 domain-specific sentence
Throws:
java.lang.IllegalArgumentException - if the separator is Unicode U+0000

handleOption

final void handleOption(OptionParser parser,
                        ArgumentList arguments,
                        OptionSet detectedOptions,
                        java.lang.String detectedArgument)
Specified by:
handleOption in class OptionSpec

addArguments

protected void addArguments(OptionSet detectedOptions,
                            java.lang.String detectedArgument)

detectOptionArgument

protected abstract void detectOptionArgument(OptionParser parser,
                                             ArgumentList arguments,
                                             OptionSet detectedOptions)

convert

protected final java.lang.Object convert(java.lang.String argument)

canConvertArgument

protected boolean canConvertArgument(java.lang.String argument)

isArgumentOfNumberType

protected boolean isArgumentOfNumberType()

acceptsArguments

boolean acceptsArguments()
Specified by:
acceptsArguments in class OptionSpec

requiresArgument

boolean requiresArgument()
Specified by:
requiresArgument in class OptionSpec

argumentDescription

java.lang.String argumentDescription()

argumentType

java.lang.Class argumentType()

equals

public boolean equals(java.lang.Object that)

Overrides:
equals in class OptionSpec

hashCode

public int hashCode()

Overrides:
hashCode in class OptionSpec