jargs.gnu
Class CmdLineParser

java.lang.Object
  |
  +--jargs.gnu.CmdLineParser

public class CmdLineParser
extends java.lang.Object

Largely GNU-compatible command-line options parser. Has short (-v) and long-form (--verbose) option support, and also allows options with associated values (-d 2, --debug 2, --debug=2). Option processing can be explicitly terminated by the argument '--'.

Version:
$Revision: 1.4 $
Author:
Steve Purcell
See Also:
jargs.examples.gnu.OptionTest

Nested Class Summary
static class CmdLineParser.IllegalOptionValueException
          Thrown when an illegal or missing value is given by the user for an option that takes a value.
static class CmdLineParser.Option
          Representation of a command-line option
static class CmdLineParser.OptionException
          Base class for exceptions that may be thrown when options are parsed
static class CmdLineParser.UnknownOptionException
          Thrown when the parsed command-line contains an option that is not recognised.
 
Field Summary
private  java.util.Hashtable options
           
private  java.lang.String[] remainingArgs
           
private  java.util.Hashtable values
           
 
Constructor Summary
CmdLineParser()
           
 
Method Summary
 CmdLineParser.Option addBooleanOption(char shortForm, java.lang.String longForm)
          Convenience method for adding a boolean option.
 CmdLineParser.Option addDoubleOption(char shortForm, java.lang.String longForm)
          Convenience method for adding a double option.
 CmdLineParser.Option addIntegerOption(char shortForm, java.lang.String longForm)
          Convenience method for adding an integer option.
 CmdLineParser.Option addOption(CmdLineParser.Option opt)
          Add the specified Option to the list of accepted options
 CmdLineParser.Option addStringOption(char shortForm, java.lang.String longForm)
          Convenience method for adding a string option.
 java.lang.Object getOptionValue(CmdLineParser.Option o)
           
 java.lang.String[] getRemainingArgs()
           
 void parse(java.lang.String[] argv)
          Extract the options and non-option arguments from the given list of command-line arguments.
 void parse(java.lang.String[] argv, java.util.Locale locale)
          Extract the options and non-option arguments from the given list of command-line arguments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

remainingArgs

private java.lang.String[] remainingArgs

options

private java.util.Hashtable options

values

private java.util.Hashtable values
Constructor Detail

CmdLineParser

public CmdLineParser()
Method Detail

addOption

public final CmdLineParser.Option addOption(CmdLineParser.Option opt)
Add the specified Option to the list of accepted options


addStringOption

public final CmdLineParser.Option addStringOption(char shortForm,
                                                  java.lang.String longForm)
Convenience method for adding a string option.

Returns:
the new Option

addIntegerOption

public final CmdLineParser.Option addIntegerOption(char shortForm,
                                                   java.lang.String longForm)
Convenience method for adding an integer option.

Returns:
the new Option

addDoubleOption

public final CmdLineParser.Option addDoubleOption(char shortForm,
                                                  java.lang.String longForm)
Convenience method for adding a double option.

Returns:
the new Option

addBooleanOption

public final CmdLineParser.Option addBooleanOption(char shortForm,
                                                   java.lang.String longForm)
Convenience method for adding a boolean option.

Returns:
the new Option

getOptionValue

public final java.lang.Object getOptionValue(CmdLineParser.Option o)
Returns:
the parsed value of the given Option, or null if the option was not set

getRemainingArgs

public final java.lang.String[] getRemainingArgs()
Returns:
the non-option arguments

parse

public final void parse(java.lang.String[] argv)
                 throws CmdLineParser.IllegalOptionValueException,
                        CmdLineParser.UnknownOptionException
Extract the options and non-option arguments from the given list of command-line arguments.

CmdLineParser.IllegalOptionValueException
CmdLineParser.UnknownOptionException

parse

public final void parse(java.lang.String[] argv,
                        java.util.Locale locale)
                 throws CmdLineParser.IllegalOptionValueException,
                        CmdLineParser.UnknownOptionException
Extract the options and non-option arguments from the given list of command-line arguments.

CmdLineParser.IllegalOptionValueException
CmdLineParser.UnknownOptionException


Copyright © R. Kevin Cole. All Rights Reserved.