iglu.util
Class CommandLineParser

java.lang.Object
  |
  +--iglu.util.CommandLineParser

public class CommandLineParser
extends java.lang.Object

A command line parser for applications. It does not do anything with the commands that it parses, but it sorts them out, and provides an easy way for an application to query whether some option was present on the command line.
Two important terms to understand


If an option has no option associated with it, it is called a switch.

Example: java apps.doodle -q -a trbauer -u -n
Here, -q, -a, -u, and -n are options. trbauer is a parameter associated with the option -a.

Author:
Travis Bauer

Field Summary
private  java.util.LinkedList extras
          Command line options present that could not be associated with option switches
private  java.util.HashMap params
          The list of parameters and their values.
 
Constructor Summary
CommandLineParser(java.lang.String[] argv, char optionMarker, boolean loadFile)
          Creates new CommandLineParser
CommandLineParser(java.lang.String[] argv, char optionMarker, boolean loadFile, java.lang.String[] switches)
          Creates a new commandlineparser, never associating the items in switches with any parameters.
 
Method Summary
 java.util.Iterator extrasIterator()
          An iterator over the strings not associated with any options
 java.lang.String[] getExtras()
          Returns an array of the strings passed to the command line without option tags.
 java.lang.String getOptionValue(java.lang.String option)
          Returns the parameter associated with the option, or null if no associated parameter exists.
protected  java.lang.String[] loadStringsFromFile(java.lang.String fileName)
          loads a file into an array of strings, and breaks them up into options
static void main(java.lang.String[] argv)
          Simple test.
 boolean optionExists(java.lang.String option)
          Returns true if the option was put on the command line.
 java.util.Set optionSet()
          All the options on the command line.
protected  void parseStrings(java.lang.String[] args, char optionMarker, java.util.LinkedList switches)
          Parses the strings passes to it, interpreting them as commandline options
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

params

private java.util.HashMap params
The list of parameters and their values. If the item is a switch, its value is the Boolean true


extras

private java.util.LinkedList extras
Command line options present that could not be associated with option switches

Constructor Detail

CommandLineParser

public CommandLineParser(java.lang.String[] argv,
                         char optionMarker,
                         boolean loadFile)
Creates new CommandLineParser

Parameters:
argv - The parameters to parse.
optionMarker - What indicates an option.
loadFile - If set to true, use @filename option to specify a file where options are stored.

CommandLineParser

public CommandLineParser(java.lang.String[] argv,
                         char optionMarker,
                         boolean loadFile,
                         java.lang.String[] switches)
Creates a new commandlineparser, never associating the items in switches with any parameters. So "java me.myapp -q goofy" would treat -q as a switch if were passed in switches[] even though is could be interpretes as as an option with the parameter goofy

Parameters:
argv - The string to parse
optionMarker - What denotes an option
loadFile - Should I load a file if @ is present?
switches - Options never to associate with a parameter
Method Detail

loadStringsFromFile

protected java.lang.String[] loadStringsFromFile(java.lang.String fileName)
loads a file into an array of strings, and breaks them up into options

Parameters:
fileName -

parseStrings

protected void parseStrings(java.lang.String[] args,
                            char optionMarker,
                            java.util.LinkedList switches)
Parses the strings passes to it, interpreting them as commandline options

Parameters:
args -
optionMarker -
switches -

getExtras

public java.lang.String[] getExtras()
Returns an array of the strings passed to the command line without option tags.


optionExists

public boolean optionExists(java.lang.String option)
Returns true if the option was put on the command line.

Parameters:
option -

getOptionValue

public java.lang.String getOptionValue(java.lang.String option)
Returns the parameter associated with the option, or null if no associated parameter exists.

Parameters:
option -

optionSet

public java.util.Set optionSet()
All the options on the command line.


extrasIterator

public java.util.Iterator extrasIterator()
An iterator over the strings not associated with any options


main

public static void main(java.lang.String[] argv)
Simple test. -q, -r, and -s are switches. Give it a command line, and see it parsed.

Parameters:
argv -