com.kedwards.swt
Class Args
java.lang.Object
|
+--com.kedwards.swt.Args
- public class Args
- extends java.lang.Object
Args.java
This is a totally cheesy little argument processing gizmo. The basic
idea is that, soon after you enter main(), you pass your argv to a static
method on this thing.
The command line is processed so that tokens beginning with "-" are
treated as options, and every non-dash-started token after an option
is considered to be an operand of that option.
The class builds a hash map of the arguments, with the options as keys
and a list of strings representing the operands of those options as values.
This class does no validity checking on arguments or types or anything...
that's up to application code.
Code that needs to use command line args can do the following:
- Test that a given option exists, via
optionExists
- Fetch the operands for a given option as a "raw" list, using
getOperands
- Fetch the operands for a given option and do some simply
type conversion to some standard types. For example,
getStringOperand
will return the 0th item
in the operand list as a string, and raise hell if there is no operand
or if there are more than one operands. Likewise,
getBooleanOperand
does the same... only it
treats no operands as true.
In the above type conversions, "raising hell" is defined as throwing
ArgException.
There are versions of the above methods that take a boolean parameter
"required" that indicates that the option must be present, and a valid operand
must be present; if required is true, and the option is not present, or if it
has no operand, then an ArgException will be raised.
One other thing, any of the option testing or operand fetching methods
described above will raise IllegalStateException if no arguments have
been set. This is probably because some program forgot to process the
args but is still trying to use them. If your code doesn't care whether or
not args have been processed, it can use the argsProcessed
method to test this, before entering any of the potentially hell-raising
methods.
See http://www.kedwards.com/jini
No warranty expressed or implied; use at your own risk; for educational
purposes only.
- Author:
- Keith Edwards
Constructor Summary |
Args(java.lang.String[] argv,
java.lang.String template)
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Args
public Args(java.lang.String[] argv,
java.lang.String template)
throws ArgException
createTemplateMap
protected void createTemplateMap(java.lang.String template)
createArgMap
protected void createArgMap(java.lang.String[] argv)
throws ArgException
getArg
public java.lang.String getArg(java.lang.String option)
optionExists
public boolean optionExists(java.lang.String option)
getAllArgs
public java.util.Map getAllArgs()
getTrailingArgs
public java.util.List getTrailingArgs()
dumpArgs
public void dumpArgs()
main
public static void main(java.lang.String[] argv)