org.eigenbase.util
Class OptionsList.Option

java.lang.Object
  extended by org.eigenbase.util.OptionsList.Option
Direct Known Subclasses:
OptionsList.BooleanOption, OptionsList.EnumeratedOption, OptionsList.NumberOption, OptionsList.StringOption
Enclosing class:
OptionsList

public abstract static class OptionsList.Option
extends Object

Definition of a command-line option, including its short and long names, description, default value, and whether it is mandatory.

You can optionally provide a handler to handle events such as the option receiving a value, or a value being of the wrong format. If you do not provide a handler, the value is stored inside the option, and can be retrieved via


Field Summary
private  Object defaultValue
          Default value of option, or null if there is no default value.
private  String description
           
private  String flag
          Short name of option, used as a flag, e.g.
private  OptionsList.OptionHandler handler
           
private  String name
          Long name of option, e.g.
private  boolean required
           
protected  Object value
          Holds the runtime value of this option.
 
Constructor Summary
OptionsList.Option(String flag, String option, String description, boolean required, boolean anonymous, Object defaultValue, OptionsList.OptionHandler handler)
           
 
Method Summary
 String getDescription()
           
 String getName()
           
 Object getValue()
          Returns the value of this option for the most recent call to OptionsList.parse(java.lang.String[]).
 int match(String[] args, int i)
          Tries to apply this option to the ith member of args.
protected abstract  void readArg(String arg)
          Converts an argument to the correct value type, and acts on the value.
 void set(Object value, boolean isExplicit)
           
 void setHandler(OptionsList.OptionHandler handler)
           
protected  void valueError(String arg)
          Called by the parser when an argument is not a valid value for this type of argument.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

value

protected Object value
Holds the runtime value of this option. Set by the default implementation set(java.lang.Object, boolean). If the user has supplied an OptionsList.OptionHandler, or overridden the set method, this field is not assigned.

Several derived classes have typesafe methods to access this field: see OptionsList.BooleanOption.booleanValue(), OptionsList.StringOption.stringValue(), OptionsList.NumberOption.intValue(), OptionsList.NumberOption.doubleValue().


defaultValue

private final Object defaultValue
Default value of option, or null if there is no default value.


description

private final String description

flag

private final String flag
Short name of option, used as a flag, e.g. "c".


name

private final String name
Long name of option, e.g. "color".


required

private final boolean required

handler

private OptionsList.OptionHandler handler
Constructor Detail

OptionsList.Option

OptionsList.Option(String flag,
                   String option,
                   String description,
                   boolean required,
                   boolean anonymous,
                   Object defaultValue,
                   OptionsList.OptionHandler handler)
Method Detail

getDescription

public String getDescription()

setHandler

public void setHandler(OptionsList.OptionHandler handler)

getName

public String getName()

getValue

public Object getValue()
Returns the value of this option for the most recent call to OptionsList.parse(java.lang.String[]).

If you specified an OptionsList.OptionHandler, this value will not be set. Also note that this method is unsafe if the same options are shared between multiple threads.

Some derived classes have methods which return the same information in a typesafe manner. For example: OptionsList.BooleanOption.booleanValue(), OptionsList.NumberOption.intValue().


match

public int match(String[] args,
                 int i)
Tries to apply this option to the ith member of args.

Parameters:
args - Argument list
i - Offset of argument in argument list
Returns:
If matched, the offset of the argument after the last one matched, otherwise i.

set

public void set(Object value,
                boolean isExplicit)

readArg

protected abstract void readArg(String arg)
Converts an argument to the correct value type, and acts on the value.

What action is taken depends upon whether the value is valid for this argument type, and whether there is a handler. If there is a handler, this method calls either OptionsList.OptionHandler.set(org.eigenbase.util.OptionsList.Option, java.lang.Object, boolean) or OptionsList.OptionHandler.invalidValue(org.eigenbase.util.OptionsList.Option, java.lang.String). If there is no handler, the method should execute a reasonable default action like assigning to a field via reflection.

Parameters:
arg -

valueError

protected void valueError(String arg)
Called by the parser when an argument is not a valid value for this type of argument.

For example, if "flag" is a boolean argument and they specify "flag=oui" on the command-line, the parser will call valueError("oui").

The default implementation calls OptionsList.OptionHandler.invalidValue(org.eigenbase.util.OptionsList.Option, java.lang.String) if there is a handler, or prints a message to System.out if there is not. Derived option classes can override this method.

Parameters:
arg - String value which is supposed to match the parameter, but doesn't.