com.townleyenterprises.command
Class CommandOption

java.lang.Object
  extended bycom.townleyenterprises.command.CommandOption
Direct Known Subclasses:
PosixCommandOption, RepeatableCommandOption

public class CommandOption
extends java.lang.Object

This class provides support for defining command-line arguments.

Since:
2.0
Version:
$Id: CommandOption.java,v 1.6 2004/07/30 16:24:14 atownley Exp $
Author:
Andrew S. Townley

Constructor Summary
CommandOption(java.lang.String longName, char shortName, boolean hasArg, java.lang.String argHelp, java.lang.String argDesc)
          The class is fully initialized by the constructor and each argument is immutable once it has been set.
CommandOption(java.lang.String longName, char shortName, boolean hasArg, java.lang.String argHelp, java.lang.String argDesc, boolean show, java.lang.String def)
          This version of the constructor allows specifying if the argument is to be shown to the user and if the argument has a default value if it is not specified on the command line.
CommandOption(java.lang.String longName, char shortName, boolean hasArg, java.lang.String argHelp, java.lang.String argDesc, java.lang.String def)
          This version of the constructor allows specifying if the argument has a default value.
 
Method Summary
 void execute()
          This method is used to allow the class to implement the GoF Command pattern fully.
 java.lang.String getArg()
          This method is used to retrieve the argument (if any) which was given to the option.
 java.lang.String getArgumentDefault()
           
 java.lang.Object getArgValue()
          This method is used to provide the argument parsed as the appropriate type.
 java.lang.String getDescription()
           
 boolean getExpectsArgument()
           
 java.lang.String getHelp()
           
 java.lang.String getLongName()
           
 boolean getMatched()
          Indicates if this option has been matched by the command parser or not.
 java.lang.String getName()
          This method is used to return some sort of normailzed name for the option.
 java.lang.Character getShortName()
           
 boolean getShowArgInHelp()
           
 int hashCode()
           
 void optionMatched(java.lang.String arg)
          This method is called by the command parser to indicate that the option has been matched.
static java.lang.String[] parseOption(java.lang.String str)
          This method is used to break up a key=val string into a 2 element array of arr[0] = key and arr[1] = val.
static java.util.Map parseOptions(java.util.List list)
          This method is used to parse a list options of the form key=val into a map which is easier to manipulate.
 void reset()
          This method is used to support multiple parses by a CommandParser instance using different sets of arguments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommandOption

public CommandOption(java.lang.String longName,
                     char shortName,
                     boolean hasArg,
                     java.lang.String argHelp,
                     java.lang.String argDesc)
The class is fully initialized by the constructor and each argument is immutable once it has been set.

Parameters:
longName - the long name to be checked
shortName - the short, single character name
hasArg - true if this option expects an argument; false if it is a switch
argHelp - the help string for the argument
argDesc - the long description of what the argument does

CommandOption

public CommandOption(java.lang.String longName,
                     char shortName,
                     boolean hasArg,
                     java.lang.String argHelp,
                     java.lang.String argDesc,
                     boolean show,
                     java.lang.String def)
This version of the constructor allows specifying if the argument is to be shown to the user and if the argument has a default value if it is not specified on the command line.

Parameters:
longName - the long name to be checked
shortName - the short, single character name
hasArg - true if this option expects an argument; false if it is a switch
argHelp - the help string for the argument
argDesc - the long description of what the argument does
show - true if should be shown in autohelp
def - the default value of the argument if it is not specified.

CommandOption

public CommandOption(java.lang.String longName,
                     char shortName,
                     boolean hasArg,
                     java.lang.String argHelp,
                     java.lang.String argDesc,
                     java.lang.String def)
This version of the constructor allows specifying if the argument has a default value.

Parameters:
longName - the long name to be checked
shortName - the short, single character name
hasArg - true if this option expects an argument; false if it is a switch
argHelp - the help string for the argument
argDesc - the long description of what the argument does
def - the default value of the argument if it is not specified.
Since:
2.1
Method Detail

parseOption

public static java.lang.String[] parseOption(java.lang.String str)
This method is used to break up a key=val string into a 2 element array of arr[0] = key and arr[1] = val.

Parameters:
str - the string to process
Returns:
an array containing the values or an empty array (still with 2 elements) if the string does not have an equals.
Since:
3.0

parseOptions

public static java.util.Map parseOptions(java.util.List list)
This method is used to parse a list options of the form key=val into a map which is easier to manipulate.

Parameters:
list - the list of options
Returns:
a map of the options
Since:
3.0

getLongName

public java.lang.String getLongName()

getShortName

public java.lang.Character getShortName()

getExpectsArgument

public boolean getExpectsArgument()

getHelp

public java.lang.String getHelp()

getDescription

public java.lang.String getDescription()

getShowArgInHelp

public boolean getShowArgInHelp()

getArgumentDefault

public java.lang.String getArgumentDefault()

hashCode

public int hashCode()

getArg

public java.lang.String getArg()
This method is used to retrieve the argument (if any) which was given to the option. If no argument was specified and the option has a default value, the default value will be returned instead.

Returns:
the argument or null if no argument was specified

getMatched

public boolean getMatched()
Indicates if this option has been matched by the command parser or not.

Returns:
true if matched; false otherwise

optionMatched

public void optionMatched(java.lang.String arg)
This method is called by the command parser to indicate that the option has been matched.

This method may be overridden by derived classes to provide object-oriented command-line argument handling. The default implementation simply sets the value returned by getMatched() to true and stores the argument.

Parameters:
arg - the argument (if expected)

reset

public void reset()
This method is used to support multiple parses by a CommandParser instance using different sets of arguments. Derived classes should override this method to reset any state stored using the @{link optionMatched} method.

Since:
2.1

getArgValue

public java.lang.Object getArgValue()
This method is used to provide the argument parsed as the appropriate type. By default, all arguments are treated as string values.

Returns:
the argument as a String object
Since:
2.1

execute

public void execute()
             throws java.lang.Exception
This method is used to allow the class to implement the GoF Command pattern fully. Derived classes should override this method to perform any specific actions. The default implementation does nothing.

Throws:
java.lang.Exception - if something fails.

getName

public java.lang.String getName()
This method is used to return some sort of normailzed name for the option.

Returns:
a name
Since:
3.0


Copyright © 2002-2004, Andrew S. Townley and Townley Enterprises. All Rights Reserved.
This project is hosted on http://te-code.sourceforge.net.