org.apache.commons.cli

Class OptionBuilder

public class OptionBuilder extends Object

OptionBuilder allows the user to create Options using descriptive methods.

Details on the Builder pattern can be found at http://c2.com/cgi-bin/wiki?BuilderPattern.

Since: 1.0

Author: John Keyes (john at integralsource.com)

Field Summary
static StringargName
argument name
static Stringdescription
option description
static OptionBuilderinstance
option builder instance
static Stringlongopt
long option
static intnumberOfArgs
the number of arguments
static booleanoptionalArg
option can have an optional argument value
static booleanrequired
is required?
static Objecttype
option type
static charvaluesep
value separator for argument value
Constructor Summary
OptionBuilder()
private constructor to prevent instances being created
Method Summary
static Optioncreate(char opt)
Create an Option using the current settings and with the specified Option char.
static Optioncreate()
Create an Option using the current settings
static Optioncreate(String opt)
Create an Option using the current settings and with the specified Option char.
static OptionBuilderhasArg()
The next Option created will require an argument value.
static OptionBuilderhasArg(boolean hasArg)
The next Option created will require an argument value if hasArg is true.
static OptionBuilderhasArgs()
The next Option created can have unlimited argument values.
static OptionBuilderhasArgs(int num)
The next Option created can have num argument values.
static OptionBuilderhasOptionalArg()
The next Option can have an optional argument.
static OptionBuilderhasOptionalArgs()
The next Option can have an unlimited number of optional arguments.
static OptionBuilderhasOptionalArgs(int numArgs)
The next Option can have the specified number of optional arguments.
static OptionBuilderisRequired()
The next Option created will be required.
static OptionBuilderisRequired(boolean newRequired)
The next Option created will be required if required is true.
static voidreset()
Resets the member variables to their default values.
static OptionBuilderwithArgName(String name)
The next Option created will have the specified argument value name.
static OptionBuilderwithDescription(String newDescription)
The next Option created will have the specified description
static OptionBuilderwithLongOpt(String newLongopt)
The next Option created will have the following long option value.
static OptionBuilderwithType(Object newType)
The next Option created will have a value that will be an instance of type.
static OptionBuilderwithValueSeparator(char sep)
The next Option created uses sep as a means to separate argument values.
static OptionBuilderwithValueSeparator()
The next Option created uses '=' as a means to separate argument values.

Field Detail

argName

private static String argName
argument name

description

private static String description
option description

instance

private static OptionBuilder instance
option builder instance

longopt

private static String longopt
long option

numberOfArgs

private static int numberOfArgs
the number of arguments

optionalArg

private static boolean optionalArg
option can have an optional argument value

required

private static boolean required
is required?

type

private static Object type
option type

valuesep

private static char valuesep
value separator for argument value

Constructor Detail

OptionBuilder

private OptionBuilder()
private constructor to prevent instances being created

Method Detail

create

public static Option create(char opt)
Create an Option using the current settings and with the specified Option char.

Parameters: opt the character representation of the Option

Returns: the Option instance

Throws: IllegalArgumentException if opt is not a valid character. See Option.

create

public static Option create()
Create an Option using the current settings

Returns: the Option instance

Throws: IllegalArgumentException if longOpt has not been set.

create

public static Option create(String opt)
Create an Option using the current settings and with the specified Option char.

Parameters: opt the java.lang.String representation of the Option

Returns: the Option instance

Throws: IllegalArgumentException if opt is not a valid character. See Option.

hasArg

public static OptionBuilder hasArg()
The next Option created will require an argument value.

Returns: the OptionBuilder instance

hasArg

public static OptionBuilder hasArg(boolean hasArg)
The next Option created will require an argument value if hasArg is true.

Parameters: hasArg if true then the Option has an argument value

Returns: the OptionBuilder instance

hasArgs

public static OptionBuilder hasArgs()
The next Option created can have unlimited argument values.

Returns: the OptionBuilder instance

hasArgs

public static OptionBuilder hasArgs(int num)
The next Option created can have num argument values.

Parameters: num the number of args that the option can have

Returns: the OptionBuilder instance

hasOptionalArg

public static OptionBuilder hasOptionalArg()
The next Option can have an optional argument.

Returns: the OptionBuilder instance

hasOptionalArgs

public static OptionBuilder hasOptionalArgs()
The next Option can have an unlimited number of optional arguments.

Returns: the OptionBuilder instance

hasOptionalArgs

public static OptionBuilder hasOptionalArgs(int numArgs)
The next Option can have the specified number of optional arguments.

Parameters: numArgs - the maximum number of optional arguments the next Option created can have.

Returns: the OptionBuilder instance

isRequired

public static OptionBuilder isRequired()
The next Option created will be required.

Returns: the OptionBuilder instance

isRequired

public static OptionBuilder isRequired(boolean newRequired)
The next Option created will be required if required is true.

Parameters: newRequired if true then the Option is required

Returns: the OptionBuilder instance

reset

private static void reset()
Resets the member variables to their default values.

withArgName

public static OptionBuilder withArgName(String name)
The next Option created will have the specified argument value name.

Parameters: name the name for the argument value

Returns: the OptionBuilder instance

withDescription

public static OptionBuilder withDescription(String newDescription)
The next Option created will have the specified description

Parameters: newDescription a description of the Option's purpose

Returns: the OptionBuilder instance

withLongOpt

public static OptionBuilder withLongOpt(String newLongopt)
The next Option created will have the following long option value.

Parameters: newLongopt the long option value

Returns: the OptionBuilder instance

withType

public static OptionBuilder withType(Object newType)
The next Option created will have a value that will be an instance of type.

Parameters: newType the type of the Options argument value

Returns: the OptionBuilder instance

withValueSeparator

public static OptionBuilder withValueSeparator(char sep)
The next Option created uses sep as a means to separate argument values. Example:
 Option opt = OptionBuilder.withValueSeparator(':')
                           .create('D');

 CommandLine line = parser.parse(args);
 String propertyName = opt.getValue(0);
 String propertyValue = opt.getValue(1);
 

Parameters: sep The value separator to be used for the argument values.

Returns: the OptionBuilder instance

withValueSeparator

public static OptionBuilder withValueSeparator()
The next Option created uses '=' as a means to separate argument values. Example:
 Option opt = OptionBuilder.withValueSeparator()
                           .create('D');

 CommandLine line = parser.parse(args);
 String propertyName = opt.getValue(0);
 String propertyValue = opt.getValue(1);
 

Returns: the OptionBuilder instance