net.sf.saffron.util
Class OptionsList.Option

java.lang.Object
  |
  +--net.sf.saffron.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
(package private) 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 OptionHandler, or overridden the set method, this field is not assigned.

Several derived classes have typesafe methods to access this field: see BooleanOption#booleanValue, StringOption#stringValue, NumberOption#intValue, 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 a 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(net.sf.saffron.util.OptionsList.Option, java.lang.Object, boolean) or OptionsList.OptionHandler.invalidValue(net.sf.saffron.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(net.sf.saffron.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.

SourceForge.net_Logo