Class Args


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 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.


No warranty expressed or implied; use at your own risk; for educational purposes only.

Keith Edwards

Constructor Summary
Args(java.lang.String[] argv, java.lang.String template)
Method Summary
protected  void createArgMap(java.lang.String[] argv)
protected  void createTemplateMap(java.lang.String template)
 void dumpArgs()
 java.util.Map getAllArgs()
 java.lang.String getArg(java.lang.String option)
 java.util.List getTrailingArgs()
static void main(java.lang.String[] argv)
 boolean optionExists(java.lang.String option)
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public Args(java.lang.String[] argv,
            java.lang.String template)
     throws ArgException
Method Detail


protected void createTemplateMap(java.lang.String template)


protected void createArgMap(java.lang.String[] argv)
                     throws ArgException


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)