getopt-0.3

jrowe.getopt
Class DefaultGetOptions

java.lang.Object
  |
  +--jrowe.getopt.DefaultGetOptions

public class DefaultGetOptions
extends Object
implements GetOptions

This is the default implementation of a GetOptions object.

Version:
1.5 JUN 23 2000 Added negation support for long options.
Author:
Joshua T. Rowe

Inner Class Summary
protected  class DefaultGetOptions.OptionRecord
          Used to encapuslate Option objects.
 
Field Summary
static String DEFAULT_ASSIGNMENT_STRING
          This is string that signifies an assignment of a value to an option.
static String DEFAULT_END_OF_OPTIONS_TOKEN
          This is the default end of options token.
static String DEFAULT_LIST_DELIMITER
          This is string that delimits items in a list.
static String DEFAULT_LONG_OPTION_PREFIX
          This is the default long option prefix.
static String DEFAULT_NEGATION_PREFIX
          the Default Negation Prefix property.
static String DEFAULT_SHORT_OPTION_PREFIX
          This is the default option prefix.
static String DEFAULT_STDIO_TOKEN
          Deprecated.  
 
Fields inherited from interface jrowe.getopt.GetOptions
OPTION_SETUP_DOS, OPTION_SETUP_GNU, OPTION_SETUP_POSIX
 
Constructor Summary
DefaultGetOptions()
          Creates a DefaultGetOptions object with the default attributes.
 
Method Summary
 void add(Option option)
          Adds an Option to the list of parseable options.
 Option get(char optionChar)
          Returns an Option from the list of parseable options.
 Option get(String optionName)
          Returns an Option from the list of parseable options.
protected  Object getArg(Option opt, String optID, String token)
          Parses and returns the argument that ought to be assigned to the given option.
 String getEndOfOptionsToken()
          Returns the End Of Options Token property.
 String getLongOptionPrefix()
          Returns the Long Option Prefix property.
 String getNegationPrefix()
          Returns the Negation Prefix property.
protected  List getOption(String token)
          Returns the appropriate Option object(s) for the given token.
protected  DefaultGetOptions.OptionRecord getOptionRecord(String optionID)
          Returns the correct OptionRecord given an option id.
protected  List getParsedOptions()
          Returns the usage string for this set of options.
 List getProgramArgs()
          Returns the program arguments.
 String getShortOptionPrefix()
          Returns the Short Option Prefix property.
 String getStdIOString()
          Deprecated.  
 List getTokenList()
          Returns the Token List property.
protected  ListIterator getTokenListIterator()
          An Iterator over the tokens passed in via the setArgs method.
protected static Map getUniqueMap(Map map)
          Returns a Map object containing only the unique values from the given Map object.
 String getUsage()
          Returns the usage string for this set of options.
protected  void handleOptionException(OptionException e)
          Handles an OptionException.
 boolean isAllowExplicitAssignments()
          Returns the Allow Explicit Assignments property.
protected  boolean isEndOfOptionsToken(String s)
          Returns whether the specified string indicates that only program arguments follow it.
protected  boolean isLongOption(String s)
           
 boolean isLongOptionsEnabled()
          Returns the Long Options Enabled property.
protected  boolean isOption(String s)
          Returns whether the specified string is an option.
 boolean isPreferLongOptions()
          Returns the Prefer Long Options property.
protected  boolean isShortOption(String s)
           
protected  boolean isStdIn(String s)
          Returns whether the given string matches the identifier used to represent the standard input.
 Iterator iterator()
          Returns an Iterator object which will iterate over all of the Options contained by this object.
protected  void parseArgs()
          Parses the arguments specified by the setArgs method.
 void remove(Option option)
          Removes an Option from the list of parseable options.
 Option remove(String optionName)
          Removes an Option from the list of parseable options.
 void setAllowExplicitAssignments(boolean newAllowExplicitAssignments)
          Sets the Allow Explicit Assignments property.
 void setArgs(String[] args)
          Sets the arguments from the command line.
 void setEndOfOptionsToken(String newEndOfOptionsToken)
          Sets the End Of Options Token property.
 void setLongOptionPrefix(String newLongOptionPrefix)
          Sets the Long Option Prefix property.
 void setLongOptionsEnabled(boolean longOptionsEnabled)
          Sets the Long Options Enabled property.
 void setNegationPrefix(String newNegationPrefix)
          Sets the Negation Prefix property.
 void setPreferLongOptions(boolean newpreferLongOptions)
          Sets the Prefer Long Options property.
 void setShortOptionPrefix(String newShortOptionPrefix)
          Sets the Short Option Prefix property.
 void setStdIOString(String newStdIOString)
          Deprecated.  
 void setTokenList(List newtokenList)
          Sets the Token List property.
protected  String stripAssignment(String s)
          Returns the argument from a token.
protected  String stripOption(String s)
          Splits a token into an option name.
protected  String stripPrefix(String s)
          Strips the short or long option prefix off of the token string.
protected  void throwOptionException()
          Throws an OptionException.
protected  void throwOptionException(String errorMsg, int errorCode)
          Throws an OptionException.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SHORT_OPTION_PREFIX

public static final String DEFAULT_SHORT_OPTION_PREFIX
This is the default option prefix.

DEFAULT_LONG_OPTION_PREFIX

public static final String DEFAULT_LONG_OPTION_PREFIX
This is the default long option prefix.

DEFAULT_END_OF_OPTIONS_TOKEN

public static final String DEFAULT_END_OF_OPTIONS_TOKEN
This is the default end of options token.

DEFAULT_ASSIGNMENT_STRING

public static final String DEFAULT_ASSIGNMENT_STRING
This is string that signifies an assignment of a value to an option.


DEFAULT_STDIO_TOKEN

public static final String DEFAULT_STDIO_TOKEN
Deprecated.  

This is string that signifies standard input or standard output.


DEFAULT_LIST_DELIMITER

public static final String DEFAULT_LIST_DELIMITER
This is string that delimits items in a list.


DEFAULT_NEGATION_PREFIX

public static final String DEFAULT_NEGATION_PREFIX
the Default Negation Prefix property.
See Also:
#gegationPrefix, setNegationPrefix(java.lang.String), getNegationPrefix()
Constructor Detail

DefaultGetOptions

public DefaultGetOptions()
Creates a DefaultGetOptions object with the default attributes.
Method Detail

setTokenList

public void setTokenList(List newtokenList)
Sets the Token List property.
Parameters:
newtokenList - the new value for the Token List property
See Also:
getTokenList()

getTokenList

public List getTokenList()
Returns the Token List property.
See Also:
setTokenList(List)

setPreferLongOptions

public void setPreferLongOptions(boolean newpreferLongOptions)
Sets the Prefer Long Options property.
Parameters:
newpreferLongOptions - the new value for the Prefer Long Options property
See Also:
isPreferLongOptions()

isPreferLongOptions

public boolean isPreferLongOptions()
Returns the Prefer Long Options property.
See Also:
setPreferLongOptions(boolean)

setLongOptionsEnabled

public void setLongOptionsEnabled(boolean longOptionsEnabled)
Sets the Long Options Enabled property.
Parameters:
longOptionsEnabled - the new value for the Long Options Enabled property

isLongOptionsEnabled

public boolean isLongOptionsEnabled()
Returns the Long Options Enabled property.

setShortOptionPrefix

public void setShortOptionPrefix(String newShortOptionPrefix)
Sets the Short Option Prefix property.
Parameters:
newShortOptionPrefix - the new value for the Short Option Prefix property

getShortOptionPrefix

public String getShortOptionPrefix()
Returns the Short Option Prefix property.

setLongOptionPrefix

public void setLongOptionPrefix(String newLongOptionPrefix)
Sets the Long Option Prefix property.
Parameters:
newLongOptionPrefix - the new value for the Long Option Prefix property

getLongOptionPrefix

public String getLongOptionPrefix()
Returns the Long Option Prefix property.

setEndOfOptionsToken

public void setEndOfOptionsToken(String newEndOfOptionsToken)
Sets the End Of Options Token property.
Parameters:
newEndOfOptionsToken - the new value for the End Of Options Token property

getEndOfOptionsToken

public String getEndOfOptionsToken()
Returns the End Of Options Token property.

setStdIOString

public void setStdIOString(String newStdIOString)
Deprecated.  

Sets the Std IOString property.
Parameters:
newStdIOString - the new value for the Std IOString property

getStdIOString

public String getStdIOString()
Deprecated.  

Returns the Std IOString property.

setAllowExplicitAssignments

public void setAllowExplicitAssignments(boolean newAllowExplicitAssignments)
Sets the Allow Explicit Assignments property.
Parameters:
newAllowExplicitAssignments - the new value for the Allow Explicit Assignments property

isAllowExplicitAssignments

public boolean isAllowExplicitAssignments()
Returns the Allow Explicit Assignments property.

setNegationPrefix

public void setNegationPrefix(String newNegationPrefix)
Sets the Negation Prefix property.
Parameters:
newNegationPrefix - the new value for the Negation Prefix property

getNegationPrefix

public String getNegationPrefix()
Returns the Negation Prefix property.

getTokenListIterator

protected ListIterator getTokenListIterator()
An Iterator over the tokens passed in via the setArgs method.
Returns:
an Iterator object.

add

public void add(Option option)
Adds an Option to the list of parseable options.
Specified by:
add in interface GetOptions
Parameters:
option - The Option to add.

remove

public Option remove(String optionName)
Removes an Option from the list of parseable options.
Specified by:
remove in interface GetOptions
Parameters:
optionName - The name of the Option to remove.
The - Option which was removed. If this object does not contain an Option by that name, then null will be returned.

remove

public void remove(Option option)
Removes an Option from the list of parseable options.
Specified by:
remove in interface GetOptions
Parameters:
option - The Option to remove.

get

public Option get(String optionName)
Returns an Option from the list of parseable options.
Specified by:
get in interface GetOptions
Parameters:
optionName - The name of the Option to return.
The - Option. If this object does not contain an Option by that name, then null will be returned.

get

public Option get(char optionChar)
Returns an Option from the list of parseable options.
Parameters:
optionChar - The char property of the Option to return.
The - Option specified. If this object does not contain an Option with the specified property, then null will be returned.

iterator

public Iterator iterator()
Returns an Iterator object which will iterate over all of the Options contained by this object.
Specified by:
iterator in interface GetOptions
Returns:
An Iterator

setArgs

public void setArgs(String[] args)
             throws OptionException
Sets the arguments from the command line.

The options are processed immediately.

Specified by:
setArgs in interface GetOptions
Parameters:
args - The arguments from the command line.
Throws:
OptionException - if there is an error while parsing the arguments

handleOptionException

protected void handleOptionException(OptionException e)
Handles an OptionException.
Parameters:
e - the exception

getProgramArgs

public List getProgramArgs()
Returns the program arguments.
Specified by:
getProgramArgs in interface GetOptions
Returns:
The program arguments.

parseArgs

protected void parseArgs()
                  throws OptionException
Parses the arguments specified by the setArgs method.

isOption

protected boolean isOption(String s)
Returns whether the specified string is an option.
Parameters:
s - The specified string.
Returns:
true if the specified string is an option; false if the specified string is either a program argument or an option argument.

isShortOption

protected boolean isShortOption(String s)

isLongOption

protected boolean isLongOption(String s)

isEndOfOptionsToken

protected boolean isEndOfOptionsToken(String s)
Returns whether the specified string indicates that only program arguments follow it.
Parameters:
s - The specified string.
Returns:
true if the specified string indicates that only program arguments follow it.

getOptionRecord

protected DefaultGetOptions.OptionRecord getOptionRecord(String optionID)
Returns the correct OptionRecord given an option id.
Parameters:
optionID - this is either the name or the character property on an Option object
Returns:
the correct OptionRecord. If no matching OptionRecord exists, then null is returned.

getOption

protected List getOption(String token)
                  throws OptionException
Returns the appropriate Option object(s) for the given token.
Parameters:
token - a token as if from the setArgs method.
Returns:
a List of the matching Option objects.
Throws:
an - OptionException if the given option doesn't exist

getArg

protected Object getArg(Option opt,
                        String optID,
                        String token)
Parses and returns the argument that ought to be assigned to the given option.
Parameters:
opt - the given Option
token - the given token
Returns:
the argument to be assigned to the Option
Throws:
IllegalArgumentException - if any of the parameters are null

isStdIn

protected boolean isStdIn(String s)
Returns whether the given string matches the identifier used to represent the standard input.
Parameters:
s - the string
Returns:
whether the string represents standard in

stripOption

protected String stripOption(String s)
Splits a token into an option name.
Parameters:
s - the token string
Returns:
a String containing the option name

stripPrefix

protected String stripPrefix(String s)
Strips the short or long option prefix off of the token string.
Parameters:
s - the token string
Returns:
the string stripped of the option prefix

stripAssignment

protected String stripAssignment(String s)
Returns the argument from a token.
Parameters:
s - the token string
Returns:
the argument

throwOptionException

protected void throwOptionException()
                             throws OptionException
Throws an OptionException.
Throws:
the - exception

throwOptionException

protected void throwOptionException(String errorMsg,
                                    int errorCode)
                             throws OptionException
Throws an OptionException.
Parameters:
e - an OptionException.
Throws:
the - exception

getUsage

public String getUsage()
Returns the usage string for this set of options.
Returns:
the usage string

getParsedOptions

protected List getParsedOptions()
Returns the usage string for this set of options.
Returns:
the usage string

getUniqueMap

protected static Map getUniqueMap(Map map)
Returns a Map object containing only the unique values from the given Map object.
Parameters:
map - the given Map object
Returns:
the Map object containing only the unique values

getopt-0.3