Class CommandLine

java.lang.Object
io.sf.jclf.app.CommandLine

public class CommandLine extends Object
Command-line parameter/argument handling.

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

  • Only single-letter options can have name=value assignations on it
  • Only double-dash options can have values assigned by equals
  • Double-dash options can't have contiguous values (e.g. -p2 is valid but not --p2)

For example,

  • it is legal: -s -p2 -Dname1=value1 -Dname2=value2 --path=/usr/bin
  • It is not legal: -p=2 -q=3
  • and --varPATH=/usr/bin would be interpreted as parameter "varPATH" having value "/usr/bin", and not as "PATH" being a variable of type "var" with the value "/usr/bin".

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"
 }
 
  • Constructor Details

    • 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 Details

    • 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<String> getParameterValues(String pname)
    • toString

      public String toString()
      Overrides:
      toString in class Object