com.foxsmart.ic.util
Class CommandLineParams

java.lang.Object
  extended by com.foxsmart.ic.util.CommandLineParams

public class CommandLineParams
extends java.lang.Object

This class provides the ability to define and parse command line parameters to a Java main program.

A command line takes the form of the program name along with any number of flags (e.g. -a) along with any number of options (e.g. -p ) along with any number of arguments (e.g. ). "Flags" are always optional. "Options" are optional or required. "Arguments" are always required.


Constructor Summary
CommandLineParams()
          Construct a new CommandLineArgs object.
 
Method Summary
 void addArgument(java.lang.String name, java.lang.String description)
          Adds a command line argument.
 void addFlag(java.lang.String flag, java.lang.String name, java.lang.String description)
          Adds a command line flag.
 void addOption(java.lang.String option, java.lang.String name, java.lang.String description, boolean required)
          Adds a command line option.
 boolean containsFlag(java.lang.String flag)
          Returns whether the command line parameters contained the specified flag.
 boolean containsOption(java.lang.String option)
          Returns whether the command line parameters contained the specified option.
 java.lang.String getAdditionUsage()
          Gets the additional usage text if specified.
 java.lang.String getArgument(java.lang.String name)
          Gets the value of the specified argument.
 java.lang.String getOption(java.lang.String option)
          Gets the value of the specified option.
 java.lang.String getProgramName()
          Gets the program name.
 java.lang.String getUsage()
          Gets the usage description for the program.
 void parse(java.lang.String[] args)
          Parses command line arguments and compares the arguments to what is expected.
 void removeArgument(java.lang.String name)
          Removes a command line argument.
 void removeFlag(java.lang.String flag)
          Removes a command line flag.
 void removeOption(java.lang.String option)
          Removes a command line option.
 void reset()
          Resets the previously parsed parameters.
 void setAdditionalUsage(java.lang.String additionalUsage)
          Sets any additional usage text that will be appended to the end of the usage statement.
 void setProgramName(java.lang.String programName)
          Sets the program name.
 void setUsage(java.lang.String usage)
          Sets the usage statement.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommandLineParams

public CommandLineParams()
Construct a new CommandLineArgs object.

Method Detail

reset

public void reset()
Resets the previously parsed parameters. This can be useful if the parameters didn't parse properly and the caller wants to try again.


addFlag

public void addFlag(java.lang.String flag,
                    java.lang.String name,
                    java.lang.String description)
Adds a command line flag.

Parameters:
flag - A command line flag. The "-" prefix is not required.
name - The name of the flag.
description - A description of the flag. Used for building a usage statement.

removeFlag

public void removeFlag(java.lang.String flag)
Removes a command line flag.

Parameters:
flag - A command line flag. The "-" prefix is not required.

containsFlag

public boolean containsFlag(java.lang.String flag)
Returns whether the command line parameters contained the specified flag.

Parameters:
flag - A command line flag. The "-" prefix is not required.
Returns:
True if the flag is present or false if not.

addOption

public void addOption(java.lang.String option,
                      java.lang.String name,
                      java.lang.String description,
                      boolean required)
Adds a command line option.

Parameters:
option - A command line option. The "-" prefix is not required.
name - The name of the option.
description - A description of the option. Used for building a usage statement.
required - True if the option is required or false if the option is optional.

removeOption

public void removeOption(java.lang.String option)
Removes a command line option.

Parameters:
option - A command line option. The "-" prefix is not required.

containsOption

public boolean containsOption(java.lang.String option)
Returns whether the command line parameters contained the specified option.

Parameters:
option - A command line option. The "-" prefix is not required.
Returns:
True if the option is present or false if not.

getOption

public java.lang.String getOption(java.lang.String option)
Gets the value of the specified option.

Parameters:
option - A command line option. The "-" prefix is not required.
Returns:
The value of the specified option. Null is returned if the option was not specified.

addArgument

public void addArgument(java.lang.String name,
                        java.lang.String description)
Adds a command line argument. The order that arguments are added is the order that they will be searched on the command line.

Parameters:
name - A command line argument. Arguments can not start with the "-" prefix. If a prefix is found, it will be removed.
description - A description of the argument. Used for building a usage statement.

removeArgument

public void removeArgument(java.lang.String name)
Removes a command line argument.

Parameters:
name - A command line argument. Arguments can not start with the "-" prefix. If a prefix is found, it will be removed.

getArgument

public java.lang.String getArgument(java.lang.String name)
Gets the value of the specified argument.

Parameters:
name - A command line argument. Arguments can not start with the "-" prefix. If a prefix is found, it will be removed.
Returns:
The value of the specified argument. Null is returned if the argument was not specified.

setProgramName

public void setProgramName(java.lang.String programName)
Sets the program name. This is needed if the default usage is requested. If the program name is not set the default usage will use "Program" as the name.

Parameters:
programName - The name of the program.

setUsage

public void setUsage(java.lang.String usage)
Sets the usage statement. A default usage statement will be returned unless this method is invoked. Once this method is called, it will be returned verbatim in the getUsage method. If this method is called will null, the default usage statement will be returned.

Parameters:
usage - A description of the program usage.

setAdditionalUsage

public void setAdditionalUsage(java.lang.String additionalUsage)
Sets any additional usage text that will be appended to the end of the usage statement. This text will be appended on the end of the default usage or the specified usage.

Parameters:
additionalUsage - Additional Usage text.

getProgramName

public java.lang.String getProgramName()
Gets the program name. If no name has been set, "Program" is used.

Returns:
The program name.

getUsage

public java.lang.String getUsage()
Gets the usage description for the program. The default usage will be returned unless a specific usage was specified with the setUsage method. Any addition usage text specified in the setAdditionalUsage method will always be added to the end of the usage text.

Returns:
the usage.

getAdditionUsage

public java.lang.String getAdditionUsage()
Gets the additional usage text if specified. If no additional usage text is specified, null is returned.

Returns:
the addition usage text.

parse

public void parse(java.lang.String[] args)
           throws java.lang.Exception
Parses command line arguments and compares the arguments to what is expected. If any errors occur, an exception will be thrown.

Parameters:
args - program arguments.
Throws:
java.lang.Exception - if the arguments specified do not match what is expected.


Copyright © 2005-2008 Fox Smart, Inc. All Rights Reserved.