water
Class Arguments

java.lang.Object
  extended by water.Arguments

public class Arguments
extends java.lang.Object

Utility for processing command Simple command line processing. This class provides functionality for parsing command line arguments that is coded over and over again in main methods. The model is that command line arguments have the form:

  option_args* free_form*
 
where each element in option_args is an option starting with a '-' character and each element in free_form is a string. Option arguments have the syntax:
  '-'NAME[=VALUE]
 
where NAME is the option identifier and VALUE is the string argument for that option.

An example use of the class is as follows:

  static void main(String[] args) {
    Arguments cl = new Arguments();
    cl.parse(args);
    if (cl.getOption("verbose") != null) ... ;
    String file = cl.getArgument(0);
    String path = cl.getOption("classpath");
 


Nested Class Summary
static class Arguments.Arg
           
static class Arguments.MissingArgumentError
           
static class Arguments.Opt
          Optional arguments.
static class Arguments.Req
          Required arguments.
 
Constructor Summary
Arguments()
          Create a new CommandLine object with no arguments.
Arguments(java.lang.String[] args)
          Create a new CommandLine object with an initial argument array.
 
Method Summary
 int addArgument(java.lang.String str, java.lang.String next)
          Add a new argument to this command line.
 void extract(Arguments.Arg arg)
           
 int firstFlag()
          Index of first flag, or -1 if command line contains only options.
 java.lang.String get(int i)
           
 java.lang.String getValue(java.lang.String name)
          Return the value of a binding (e.g.
 int size()
          Returns the number of remaining command line arguments.
 java.lang.String toString()
           
 java.lang.String[] toStringArray()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Arguments

public Arguments(java.lang.String[] args)
Create a new CommandLine object with an initial argument array.

Parameters:
args - array of options and argument that will be parsed.

Arguments

public Arguments()
Create a new CommandLine object with no arguments.

Method Detail

size

public int size()
Returns the number of remaining command line arguments.


firstFlag

public int firstFlag()
Index of first flag, or -1 if command line contains only options.


get

public java.lang.String get(int i)

addArgument

public int addArgument(java.lang.String str,
                       java.lang.String next)
Add a new argument to this command line. The argument will be parsed and add at the end of the list. Bindings have the following format "-name=value" if value is empty, the binding is treated as an option. Options have the form "-name". All other strings are treated as values.

Parameters:
str - a string

extract

public void extract(Arguments.Arg arg)
             throws Arguments.MissingArgumentError
Throws:
Arguments.MissingArgumentError

getValue

public java.lang.String getValue(java.lang.String name)
Return the value of a binding (e.g. "value" for "-name=value") and the empty string "" for an option ("-name" or "-name="). A null value is returned if no binding or option is found.

Parameters:
name - string name of the option or binding
Returns:

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toStringArray

public java.lang.String[] toStringArray()