|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.util.CommandLine
public class CommandLine
Useful when looking for command line switches.
This class is designed to help one parse command line options for an array of strings. For example, if you want to allow the user to specify one or more different command line options, these routines can be very handy when looking for command line switches. Assume you had a program which would output random numbers in the range of [0,99] by default, you could then indicate that the minumum and maximum values could be overridden by the user via command line switches like the following:
java RandomNumGen -min 10 -max 20
This class makes this easy to do and lets you look to see if any command line switches have be specified by the user.
com.ccg.example.util.Random| Constructor Summary | |
|---|---|
CommandLine(String[] args)
Constructor for the class - sets the list of command line switches. |
|
| Method Summary | |
|---|---|
Object |
get(Object key)
Fetch the value associated with a "key" Try to lookup the Object which is associated with the key. |
int |
getIntOrDefault(String switchName,
int defaultValue)
Function to retrieve an integer value following a command line switch. |
String |
getString(Object key)
Fetch a String "value" associated with a "key". |
String |
getStringAfter(int index)
Get the String value of the argument after the specified index. |
String |
getStringOrDefault(String sw,
String defValue)
Returns command line argument following switch or default value. |
char |
getSwitchChar()
Get the character used to specify command line arguments In order to differentiate a command line switch from a data or value, this function returns the character that is used to indicate a command line switch. |
int |
getSwitchPosition(String switchName)
Locates the position of a specific command line switch. |
boolean |
isSwitchSet(String switchName)
Determine if a boolean switch is present. |
void |
setSwitchChar(char val)
Set the character used to specify command line arguments |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public CommandLine(String[] args)
| Method Detail |
|---|
public Object get(Object key)
CommandLine class can be used as a standard Lookup object.
get in interface Lookupkey - Key to use to look up object with (if you pass null, you will
get null back).
Lookuppublic String getString(Object key)
CommandLine class can be used as a standard
Lookup object.
getString in interface Lookupkey - Key to use to look up object with (if you pass null, you will
get null back).
Lookuppublic void setSwitchChar(char val)
val - New char value to assign.getSwitchChar()public char getSwitchChar()
setSwitchChar(char), but this is seldom needed.
setSwitchChar(char)public int getSwitchPosition(String switchName)
-min 5 -max 12
Then invoking this function with "max" as the name of the switch to look for, this routine would return 2.
switchName - Name of the switch to look for in the array of
arguments. NOTE: you MUST NOT include the "-" character in
front of the switch name. For example, if you expect the user
to specify something like "-quiet", then you would pass
"quiet" as the parameter to this function.
isSwitchSet(java.lang.String)public boolean isSwitchSet(String switchName)
switchName - Name of the switch to look for in the array of
arguments. NOTE: you MUST NOT include the "-" character in
front of the switch name. For example, if you expect the user
to specify something like "-quiet", then you would pass
"quiet" as the parameter to this function.
public String getStringAfter(int index)
String getConfigFileName(CommandLine cl) {
return cl.getStringAfter(cl.getSwitchPosition("config"));
}
index - Index into the array of arguments one prior to the entry you
are interested in (typically the result of the
getSwitchPosition() member). This must be in the range of [0,
getSwitchPosition(java.lang.String)
public String getStringOrDefault(String sw,
String defValue)
sw - Command line switch to look for.default - Default value to return if user didn't specify a value for the
command line switch (or omitted the command line switch from
the command line).
isSwitchSet(java.lang.String)
public int getIntOrDefault(String switchName,
int defaultValue)
throws NumberFormatException
The following demonstrates how one could allow the user to specify -max VALUE on the command line:
int getMax(CommandLine cl) {
return cl.getIntOrDefault("max",100);
}
switchName - Name of the command line switch which the integer value should
follow.defaultValue - Default value to return if the user did not specify a command
line argument.
NumberFormatExceptiongetStringAfter(int)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||