byucc.jhdl.Logic.Modules.helpers
Class CommandLinePropertyReader

java.lang.Object
  extended bybyucc.jhdl.Logic.Modules.helpers.CommandLinePropertyReader

public class CommandLinePropertyReader
extends java.lang.Object

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

CommandLinePropertyReader

public CommandLinePropertyReader(java.lang.String[] args)
Creates a new CommandLinePropertyReader.

Example:

     public static void main (String[] args) {
       CommandLinePropertyReader clpr = new CommandLinePropertyReader (args);
       ...
     }
     

Method Detail

done

public void done()
Call done() after all the getProperty() calls.

This handles the help command and checks for extraneous parameters.


toString

public java.lang.String toString()

getFlag

public boolean getFlag(java.lang.String name)
Checks whether the name exists as a flag


getProperty

public java.lang.String getProperty(java.lang.String property)
Searches for property=value arguments

Returns:
value, or null if not found

getProperty

public int getProperty(java.lang.String property,
                       int defaultValue)
Gets a property value that is expected to be an integer.

Example: iterations = clpr.getProperty ("iterations", iterations);

This will read a command-line parameter of the form:
java myclass iterations=500

Parameters:
property - - The property name to look for
defaultValue - - The default value to return if the property lookup is unsuccessful
Returns:
The integer value. If the property does not exist, or if the value is not a valid integer, the default value is returned.

getProperty

public int getProperty(java.lang.String property,
                       int defaultValue,
                       java.lang.String description)
Gets a property value that is expected to be an integer. Example: 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

Parameters:
property - - The property name to look for
defaultValue - - The default value to return if the property lookup is unsuccessful.
description - - An optional description for the parameter
Returns:
The integer value. If the property does not exist, or if the value is not a valid integer, the default value is returned.

getProperty

public boolean getProperty(java.lang.String property,
                           boolean defaultValue)
Looks for a property flag. See the preferred getProperty (property, defaultValue, description)


getProperty

public boolean getProperty(java.lang.String property,
                           boolean defaultValue,
                           java.lang.String description)
Looks for a property flag. Example: 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.

Parameters:
property - - The property name to look for
defaultValue - - The default value to return if the property lookup is unsuccessful.
description - - An optional description for the parameter
Returns:
The boolean flag value. If the property does not exist, defaultValue is returned.

checkArguments

protected void checkArguments()
                       throws java.lang.RuntimeException
Checks for invalid command-line arguments. Any argument which is not checked using getProperty() or getFlag() is considered invalid.

Throws:
java.lang.RuntimeException - if any argument was not recognized.


Copyright ? 2006 Brigham Young University, Configurable Computing Laboratory. All Rights Reserved.