|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbyucc.jhdl.Logic.Modules.helpers.CommandLinePropertyReader
A convenience class for parsing command-line parameters.
The parameters are expected to be in property=value style.
(This is not the common Unix command-line style.)
Parameters may be flags or property=value pairs.
Flags are strings which are either present or absent,
e.g., "verbose" in java MyProgram verbose
.
Property=value pairs are also allowed, as in
java MyProgram color=blue max=255
.
For examples, see cltest.java:
// cltest.java import byucc.jhdl.Logic.Modules.helpers.CommandLinePropertyReader; /** An example using the CommandLinePropertyReader class. Try: <pre> java cltest java cltest iterations=500 java cltest verbose iterations=15000 java cltest verbose=true java cltest quiet </pre> / public class cltest { // These are examples of parameters that can be set from the command line. static int iterations = 10000, width = 8; static boolean verbose = false; static boolean showUsage = false; public static void main (String args[]) { CommandLinePropertyReader clpr = new CommandLinePropertyReader (args); // Read the command-line properties: // // static-variable = clpr.getProperty ("property-name", default-value); // iterations = clpr.getProperty ("iterations", iterations); width = clpr.getProperty ("width", width); verbose = clpr.getProperty ("verbose", verbose); // an alternate formulation: verbose = ! clpr.getProperty ("quiet", !verbose); showUsage = clpr.getProperty ("usage", showUsage); // The "help" command goes after all the getProperty calls. if (clpr.getProperty ("help", false)) { clpr.help(); } System.out.println(); System.out.println ("cltest: CommandLinePropertyReader example"); System.out.println ("Try 'java cltest usage' for helpful usage information."); if (showUsage) { System.out.println(); System.out.println (" Usage: java cltest [iterations=#] [width=#] [verbose[=true|false]] [quiet[=true|false]] [help]"); System.out.println (" Examples:"); System.out.println ("\tjava cltest"); System.out.println ("\tjava cltest iterations=500"); System.out.println ("\tjava cltest verbose iterations=15000"); System.out.println ("\tjava cltest verbose=true"); System.out.println ("\tjava cltest quiet"); } System.out.println(); System.out.println (" iterations: "+ iterations); System.out.println (" width: "+ width); System.out.println (" verbose: "+ verbose); System.out.println(); } }
Constructor Summary | |
CommandLinePropertyReader(java.lang.String[] args)
Creates a new CommandLinePropertyReader. |
Method Summary | |
protected void |
checkArguments()
Checks for invalid command-line arguments. |
void |
done()
Call done() after all the getProperty() calls. |
boolean |
getFlag(java.lang.String name)
Checks whether the name exists as a flag |
java.lang.String |
getProperty(java.lang.String property)
Searches for property=value arguments |
boolean |
getProperty(java.lang.String property,
boolean defaultValue)
Looks for a property flag. |
boolean |
getProperty(java.lang.String property,
boolean defaultValue,
java.lang.String description)
Looks for a property flag. |
int |
getProperty(java.lang.String property,
int defaultValue)
Gets a property value that is expected to be an integer. |
int |
getProperty(java.lang.String property,
int defaultValue,
java.lang.String description)
Gets a property value that is expected to be an integer. |
java.lang.String |
toString()
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public CommandLinePropertyReader(java.lang.String[] args)
Example:
public static void main (String[] args) { CommandLinePropertyReader clpr = new CommandLinePropertyReader (args); ... }
Method Detail |
public void done()
This handles the help command and checks for extraneous parameters.
public java.lang.String toString()
public boolean getFlag(java.lang.String name)
public java.lang.String getProperty(java.lang.String property)
public int getProperty(java.lang.String property, int defaultValue)
Example: iterations = clpr.getProperty ("iterations", iterations);
This will read a command-line parameter of the form:
java myclass iterations=500
property
- - The property name to look fordefaultValue
- - The default value to return if the property
lookup is unsuccessful
public int getProperty(java.lang.String property, int defaultValue, java.lang.String description)
iterations = clpr.getProperty ("iterations", iterations, "The number of iterations to run");
This will read a command-line parameter of the form:
java myclass iterations=500
property
- - The property name to look fordefaultValue
- - The default value to return if the property
lookup is unsuccessful.description
- - An optional description for the parameter
public boolean getProperty(java.lang.String property, boolean defaultValue)
public boolean getProperty(java.lang.String property, boolean defaultValue, java.lang.String description)
verbose = clpr.getProperty ("verbose", verbose, "Selects verbose output");
This will read a command-line parameter of the forms:
java myclass verbose
java myclass verbose=true
java myclass verbose=false
If the flag is present, true is returned.
If the flag is a property, the value is interpreted as follows:
true: "true"
false: "false"
If the flag is not present, defaultValue is returned.
property
- - The property name to look fordefaultValue
- - The default value to return if the property
lookup is unsuccessful.description
- - An optional description for the parameter
protected void checkArguments() throws java.lang.RuntimeException
java.lang.RuntimeException
- if any argument was not recognized.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |