info.informatica.app
Class CommandLine

java.lang.Object
  extended by info.informatica.app.CommandLine

public class CommandLine
extends Object

Command-line parameter/argument handling.

Generic encapsulation of command-line parameters, with the following general rules:

For example,

Those options prefixed by a "-" or "--" are called "parameters" or "options", while the rest are called "last arguments" or simply "arguments".

Usage example:

Arguments: -afirst_arg -Dname1=value -Dlogic --dir=/var/tmp

 public static void main(String[] args) {
        CommandLine cmdline = new CommandLine(args);
        String opt_a = cmdline.getParam("a"); // opt_a = "first_arg"
        Enumeration enu = cmdline.getParameterValues("D");
        while (enu.hasMoreElements()) {
                String s = (String) enu.nextElement(); // s = "name1=value"
                // s = "logic"
        }
        String param_dir = cmdline.getAssignParam("dir"); // param_dir = "/var/tmp"
 }
 

Version:
1.02
Author:
amengual at informatica dot info

Constructor Summary
CommandLine(String[] cmdline)
          Constructor with a String array of command line arguments.
 
Method Summary
 String[] getArguments()
           
 String getAssignParam(String pname)
          Gets the value assigned to a '--' parameter.
 String getLastArgument()
          Returns the last command-line argument (the last of those arguments not starting with a "-").
 String[] getLastArguments()
          Returns the last command-line arguments (those arguments not starting with a "-").
 String getParam(String pname)
          Gets a parameter of a given name.
 Enumeration getParameterValues(String pname)
           
 boolean hasParameter(String pname)
          Tells if the command line has a parameter of the given name.
 String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CommandLine

public CommandLine(String[] cmdline)
Constructor with a String array of command line arguments.

Parameters:
cmdline - the String array of command line arguments as given in the main method.
Method Detail

getParam

public String getParam(String pname)
Gets a parameter of a given name.

Parameters:
pname - parameter name.
Returns:
the parameter value, empty string if the parameter has no value, or null if the parameter is not present at all.

getAssignParam

public String getAssignParam(String pname)
Gets the value assigned to a '--' parameter.

Parameters:
pname - parameter name.
Returns:
the parameter value, empty string if the parameter has no value, or null if the parameter is not present at all.

hasParameter

public boolean hasParameter(String pname)
Tells if the command line has a parameter of the given name.

Parameters:
pname - the parameter name.
Returns:
true if the parameter exists, false otherwise.

getLastArgument

public String getLastArgument()
Returns the last command-line argument (the last of those arguments not starting with a "-").

Returns:
the last argument.

getLastArguments

public String[] getLastArguments()
Returns the last command-line arguments (those arguments not starting with a "-").

Returns:
the last arguments.

getArguments

public String[] getArguments()
Returns:
the command-line array.

getParameterValues

public Enumeration getParameterValues(String pname)

toString

public String toString()
Overrides:
toString in class Object