edu.rice.cs.plt.text
Class ArgumentParser

java.lang.Object
  extended by edu.rice.cs.plt.text.ArgumentParser

public class ArgumentParser
extends java.lang.Object

A simple utility class for processing command-line argument arrays. Argument strings are assumed to be made up of options, option arguments, and parameters. All argument string beginning with the character '-' are interpreted as options. Clients should specify the names and arities (and, optionally, default values) of their supported options. Aliases may also be specified, allowing multiple names to be interpreted as the same option. parse(java.lang.String...) may then be invoked on an argument array, producing errors for unrecognized options or options with the wrong number of arguments, and interpreting all remaining strings as parameters.


Nested Class Summary
static class ArgumentParser.Result
          A collection of the options and parameters parsed from an array of arguments.
 
Constructor Summary
ArgumentParser()
          Create an argument parser with an initially-empty set of supported options.
 
Method Summary
 ArgumentParser.Result parse(java.lang.String... args)
          Parse an array of arguments based on the previously-defined supported options.
 void requireParams(int count)
          Require at least the given number of parameters (0 by default).
 void requireStrictOrder()
          Require all options to precede any parameters (not required by default).
 void supportAlias(java.lang.String aliasName, java.lang.String optionName)
          Create an alias: aliasName is an alias for optionName.
 void supportOption(java.lang.String name, int arity)
          Add the given option name to the list of supported options.
 void supportOption(java.lang.String name, java.lang.String... defaultArguments)
          Add the given option name to the list of supported options and record the given default argument set.
 boolean supportsOption(java.lang.String name)
          Determine whether the given option is supported.
 void supportVarargOption(java.lang.String name)
          Add the given option name to the list of supported options.
 void supportVarargOption(java.lang.String name, java.lang.String... defaultArguments)
          Add the given option name to the list of supported options and record the given default argument set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArgumentParser

public ArgumentParser()
Create an argument parser with an initially-empty set of supported options.

Method Detail

requireStrictOrder

public void requireStrictOrder()
Require all options to precede any parameters (not required by default).


requireParams

public void requireParams(int count)
Require at least the given number of parameters (0 by default).


supportsOption

public boolean supportsOption(java.lang.String name)
Determine whether the given option is supported.


supportOption

public void supportOption(java.lang.String name,
                          int arity)
Add the given option name to the list of supported options.

Parameters:
name - The option name, excluding the "-" prefix.
arity - The number of arguments to follow the option.
Throws:
java.lang.IllegalArgumentException - If the option is already supported.

supportOption

public void supportOption(java.lang.String name,
                          java.lang.String... defaultArguments)
Add the given option name to the list of supported options and record the given default argument set.

Parameters:
name - The option name, excluding the "-" prefix.
defaultArguments - The default arguments associated with the option. Implicitly defines the arity of the option as well. If the length is 0, no default will be set (otherwise, the nullary option would always appear to be present).
Throws:
java.lang.IllegalArgumentException - If the option is already supported.

supportVarargOption

public void supportVarargOption(java.lang.String name)
Add the given option name to the list of supported options. An arbitrary number of arguments, terminated by another option or the end of the argument list, may follow the option.

Parameters:
name - The option name, excluding the "-" prefix.
Throws:
java.lang.IllegalArgumentException - If the option is already supported.

supportVarargOption

public void supportVarargOption(java.lang.String name,
                                java.lang.String... defaultArguments)
Add the given option name to the list of supported options and record the given default argument set. An arbitrary number of arguments, terminated by another option or the end of the argument list, may follow the option. (Use a 0-length array to set a 0-length default. Otherwise, supportVarargOption(String) will be invoked instead.)

Parameters:
name - The option name, excluding the "-" prefix.
defaultArguments - The default arguments associated with the option.
Throws:
java.lang.IllegalArgumentException - If the option is already supported.

supportAlias

public void supportAlias(java.lang.String aliasName,
                         java.lang.String optionName)
Create an alias: aliasName is an alias for optionName. Parsed matches will appear under the referenced option name in the result.

Throws:
java.lang.IllegalArgumentException - If the alias name is already supported, or if the option name is not supported.

parse

public ArgumentParser.Result parse(java.lang.String... args)
                            throws java.lang.IllegalArgumentException
Parse an array of arguments based on the previously-defined supported options.

Throws:
java.lang.IllegalArgumentException