joptsimple
Class OptionSpecBuilder

java.lang.Object
  extended by joptsimple.OptionSpecBuilder
All Implemented Interfaces:
OptionSpec<Void>

public class OptionSpecBuilder
extends 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
 

Version:
$Id: OptionSpecBuilder.java,v 1.16 2009/04/07 00:21:24 pholser Exp $
Author:
Paul Holser

Method Summary
 boolean equals(Object that)
          
 int hashCode()
          
 Collection<String> options()
           
 String toString()
          
 V value(OptionSet detectedOptions)
          Gives the argument associated with the given option in the given set of detected options.
 List<V> values(OptionSet detectedOptions)
          Gives any arguments associated with the given option in the given set of detected options.
 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 java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

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

options

public final Collection<String> options()
Specified by:
options in interface OptionSpec<V>
Returns:
the string representations of this option

values

public final List<V> values(OptionSet detectedOptions)
Description copied from interface: OptionSpec

Gives any arguments associated with the given option in the given set of detected options.

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

value

public final V value(OptionSet detectedOptions)
Description copied from interface: OptionSpec

Gives the argument associated with the given option in the given set of detected options.

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

equals

public boolean equals(Object that)

Overrides:
equals in class Object

hashCode

public int hashCode()

Overrides:
hashCode in class Object

toString

public String toString()

Overrides:
toString in class Object


© Copyright 2004-2009 Paul R. Holser, Jr. All rights reserved. Licensed under The MIT License. pholser@alumni.rice.edu