mark.core.args
Class ArgHelper

java.lang.Object
  |
  +--mark.core.args.ArgHelper

public class ArgHelper
extends java.lang.Object

An ArgHelper assists an application in handling command-line arguments. Typically, an application will initialize an ArgHelper with the arguments it receives as parameters to main. It then informs the ArgHelper which arguments it expects, so that the ArgHelper can print a usage string should something go wrong. Thereafter, the application will call the ArgHelper to extract arguments. Sample Usage: public static void main (String[] a) { ArgHelper args = new ArgHelper (a, "GenFeatures"); args.augmentUsage ("-v", false); args.augmentUsage ("-f ", true); boolean verbose = args.extractMatched ("-v"); java.io.File file = args.extractMatchedPlusOne ("-f", true); ...


Constructor Summary
ArgHelper(java.lang.String[] args, java.lang.String programName)
          Initializes the ArgHelper.
 
Method Summary
 void augmentUsage(java.lang.String option, boolean required)
          Appends an option to the usage string.
 void error(java.lang.String message)
          Throws an exception.
 void extractAll(java.util.Collection c, boolean required)
          Extracts and all remaining arguments and places them in the given collection.
 java.lang.String extractFirst(boolean required)
          Extracts and returns the first argument in the ArgHelper.
 boolean extractMatched(java.lang.String arg)
          Extracts the argument that matches the given String.
 java.lang.String[] extractMatchedPlusMany(java.lang.String arg, boolean required)
          Extracts the argument that matches the given String and the following argument, converts the second extracted argument into a parameter list, and returns the parameter list.
 java.lang.String extractMatchedPlusOne(java.lang.String arg, boolean required)
          Extracts the argument that matches the given String and the following argument and returns the second extracted argument.
 int size()
          Returns the number of arguments remaining in the ArgHelper.
 java.lang.String toString()
           
 void verifyEmpty()
          If an argument exists, throws an exception.
 void verifyNoMatch(java.lang.String arg)
          Throws an exception if the given argument is in the ArgHelper.
 void verifyNotEmpty()
          If no argument exists, throws an exception.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ArgHelper

public ArgHelper(java.lang.String[] args,
                 java.lang.String programName)
Initializes the ArgHelper.

Parameters:
args - the arguments.
programName - the name of the program. If the ArgHelper ever throws an exception, it will embed this name inside the exception message.
Method Detail

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

error

public void error(java.lang.String message)
           throws java.lang.Exception
Throws an exception. Prepends usage to the error message.

Parameters:
message - the exception message.
Throws:
java.lang.Exception - always.

augmentUsage

public void augmentUsage(java.lang.String option,
                         boolean required)
Appends an option to the usage string.

Parameters:
option - the option to append (e.g. "-t").
required - whether the option is required.

size

public int size()
Returns the number of arguments remaining in the ArgHelper.

Returns:
the number of arguments.

verifyNoMatch

public void verifyNoMatch(java.lang.String arg)
                   throws java.lang.Exception
Throws an exception if the given argument is in the ArgHelper.

Parameters:
arg - the argument to match (e.g. "-f").
Throws:
java.lang.Exception - if the argument is in the ArgHandler.

extractFirst

public java.lang.String extractFirst(boolean required)
                              throws java.lang.Exception
Extracts and returns the first argument in the ArgHelper. If no argument exists, returns null.

Parameters:
required - whether an argument is required.
Returns:
the extracted argument or null if no argument exists.
Throws:
java.lang.Exception - if required and no argument exists.

extractAll

public void extractAll(java.util.Collection c,
                       boolean required)
                throws java.lang.Exception
Extracts and all remaining arguments and places them in the given collection.

Parameters:
c - the collection.
required - whether an argument is required.
Throws:
java.lang.Exception - if required and no argument exists.

extractMatched

public boolean extractMatched(java.lang.String arg)
Extracts the argument that matches the given String. Returns whether a match occurred.

Parameters:
arg - the argument to match (e.g. "-f").
Returns:
whether the argument existed.

extractMatchedPlusOne

public java.lang.String extractMatchedPlusOne(java.lang.String arg,
                                              boolean required)
                                       throws java.lang.Exception
Extracts the argument that matches the given String and the following argument and returns the second extracted argument. If no match occurs, returns null.

Parameters:
arg - the argument to match (e.g. "-f").
required - whether the match is required.
Returns:
the second extracted argument or null if there is no match.
Throws:
java.lang.Exception - if required and no match occurs or if a match occurs on the last argument.

extractMatchedPlusMany

public java.lang.String[] extractMatchedPlusMany(java.lang.String arg,
                                                 boolean required)
                                          throws java.lang.Exception
Extracts the argument that matches the given String and the following argument, converts the second extracted argument into a parameter list, and returns the parameter list. If no match occurs, returns null. A parameter list is an array of arguments, constructed from a single comma-separated argument. The argument "a,b,c" converts into the argument list {"a", "b", "c"}.

Parameters:
arg - the argument to match (e.g. "-f").
required - whether the match is required.
Returns:
the parameter list or null.
Throws:
java.lang.Exception - if required and no match occurs or if a match occurs on the last argument.

verifyEmpty

public void verifyEmpty()
                 throws java.lang.Exception
If an argument exists, throws an exception.

Throws:
java.lang.Exception - if an argument exists.

verifyNotEmpty

public void verifyNotEmpty()
                    throws java.lang.Exception
If no argument exists, throws an exception.

Throws:
java.lang.Exception - if no argument exists.