- public class Args
- extends java.lang.Object
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:
In the above type conversions, "raising hell" is defined as throwing
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
- Test that a given option exists, via
- Fetch the operands for a given option as a "raw" list, using
- 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.
method to test this, before entering any of the potentially hell-raising
No warranty expressed or implied; use at your own risk; for educational
- Keith Edwards
|Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
public Args(java.lang.String argv,
protected void createTemplateMap(java.lang.String template)
protected void createArgMap(java.lang.String argv)
public java.lang.String getArg(java.lang.String option)
public boolean optionExists(java.lang.String option)
public java.util.Map getAllArgs()
public java.util.List getTrailingArgs()
public void dumpArgs()
public static void main(java.lang.String argv)