com.bbn.openmap.util
Class ArgParser

java.lang.Object
  extended by com.bbn.openmap.util.ArgParser

public class ArgParser
extends java.lang.Object

A simple class to manage the line arguments of a program. Takes the String[] argv that is provided to the main method of a class, and separates them out, depending on the options given to the ArgParser. After you create the parser, add your options that you want.


Nested Class Summary
 class ArgParser.Arg
          A default version of the Arg class used to represent options for the ArgParser to use.
 class ArgParser.HelpArg
          A Arg class to spur off help messages.
 
Field Summary
protected  boolean allowAbbr
          Tells the Args to accept the first letter of their name for argv options specified with one letter.
protected  java.util.Vector args
          The Args that the parser is looking for.
protected  char option
          The character flag for an option.
protected  java.lang.String programName
          The program name that's using the parser.
protected  java.lang.String[] rest
          The String array that holds all of the leftover argvs.
static int TO_END
          The length to submit if you want a variable length list at the end of the command line, like all the arguments left over.
 
Constructor Summary
ArgParser(java.lang.String pName)
          Create a parser for the named program.
 
Method Summary
 void add(java.lang.String argName, java.lang.String desc)
          Add a argument to the parser.
 void add(java.lang.String argName, java.lang.String desc, int expectedNumberOfArguments)
          Add a argument to the parser.
 void add(java.lang.String argName, java.lang.String desc, int expectedNumberOfArguments, boolean expectDashedArguments)
          Add a argument to the parser.
 void bail(java.lang.String message, boolean printUsageStatement)
          Called if something is messed up.
 boolean getAllowAbbr()
           
 ArgParser.Arg getArg(java.lang.String name)
          Return a Arg object with a particular name.
 java.util.Vector getArgs()
           
 java.lang.String[] getArgValues(java.lang.String name)
          Given an Arg name, return the values.
 java.lang.String[] getRest()
           
static void main(java.lang.String[] argv)
           
 boolean parse(java.lang.String[] argv)
          Parse and organize the array of Strings.
 void printUsage()
          Print a list of options added to the parser.
 void setAllowAbbr(boolean set)
          Tell the parser to accept first-letter representations of the options.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TO_END

public static final int TO_END
The length to submit if you want a variable length list at the end of the command line, like all the arguments left over.

See Also:
Constant Field Values

programName

protected java.lang.String programName
The program name that's using the parser.


args

protected java.util.Vector args
The Args that the parser is looking for.


rest

protected java.lang.String[] rest
The String array that holds all of the leftover argvs.


option

protected char option
The character flag for an option.


allowAbbr

protected boolean allowAbbr
Tells the Args to accept the first letter of their name for argv options specified with one letter.

Constructor Detail

ArgParser

public ArgParser(java.lang.String pName)
Create a parser for the named program. Automatically adds the -help option.

Parameters:
pName - the program name.
Method Detail

add

public void add(java.lang.String argName,
                java.lang.String desc)
Add a argument to the parser. Don't include the '-' in the argName, that's added automatically. Assumes that the option expects no arguments.

Parameters:
argName - the command line option
desc - a help line description.

add

public void add(java.lang.String argName,
                java.lang.String desc,
                int expectedNumberOfArguments)
Add a argument to the parser. Don't include the '-' in the argName, that's added automatically.

Parameters:
argName - the command line option
desc - a help line description.
expectedNumberOfArguments - the number of option parameters expected for this option.

add

public void add(java.lang.String argName,
                java.lang.String desc,
                int expectedNumberOfArguments,
                boolean expectDashedArguments)
Add a argument to the parser. Don't include the '-' in the argName, that's added automatically.

Parameters:
argName - the command line option
desc - a help line description.
expectedNumberOfArguments - the number of option parameters expected for this option.
expectDashedArguments - tell the parser that this option may have arguments that may start with dashes, for instance, a negative number. False by default.

parse

public boolean parse(java.lang.String[] argv)
Parse and organize the array of Strings. If something goes wrong, bail() may be called.

Parameters:
argv - arguments to be parsed
Returns:
true if everything goes well, false if not

bail

public void bail(java.lang.String message,
                 boolean printUsageStatement)
Called if something is messed up. Prints a message, and the usage statement, if desired.

Parameters:
message - a message to display.
printUsageStatement - true to display a list of available options

setAllowAbbr

public void setAllowAbbr(boolean set)
Tell the parser to accept first-letter representations of the options.

Parameters:
set - true to accept

getAllowAbbr

public boolean getAllowAbbr()
Returns:
true the parser accepts first-letter representations of the options

getArgs

public java.util.Vector getArgs()
Returns:
a Vector of Arg objects

getArg

public ArgParser.Arg getArg(java.lang.String name)
Return a Arg object with a particular name. This method shouldn't be used to figure out if values have been passed in to an application. It's to find out if an option is available to be chosen, not if it has.

Parameters:
name - of Arg
Returns:
Arg with name

getArgValues

public java.lang.String[] getArgValues(java.lang.String name)
Given an Arg name, return the values. Returns a zero length array (non-null) value for options that don't require arguments. Returns null if the option name wasn't found in the list, or if the option wasn't chosen in the parsed array of Strings.

Parameters:
name - key string for values
Returns:
values for name

getRest

public java.lang.String[] getRest()
Returns:
the String[] that makes up the trailing Strings after the options were parsed

printUsage

public void printUsage()
Print a list of options added to the parser.


main

public static void main(java.lang.String[] argv)


Copyright (C) BBNT Solutions LLC; See http://openmap.bbn.com/ for details