de.saar.getopt
Class ConvenientGetopt

Object
  extended by ConvenientGetopt

public class ConvenientGetopt
extends Object

This class encapsulates the GNU Getopt library and makes it more convenient to use from Java programs. It hides the details of the parameter string syntax completely from the user, and provides the automatic display of usage information.


Field Summary
static int NO_ARGUMENT
          Option takes no arguments.
static int OPTIONAL_ARGUMENT
          Option takes an optional argument.
static int REQUIRED_ARGUMENT
          Option takes a required argument.
 
Constructor Summary
ConvenientGetopt(String progname, String howToCall, String docBelow)
          Create new getopt object.
 
Method Summary
 void addOption(char shortname, int hasArg, String defaultValue, String description)
          Add an option that has no long name.
 void addOption(char shortname, String longname, int hasArg, String description, String defaultValue)
          Add an option that has a long name.
 List<String> getRemaining()
          Retrieve those command-line arguments that don't belong to any option.
 String getValue(char shortname)
          Retrieve the value of an option.
 String getValue(String longname)
          Retrieve the value of an option.
 boolean hasOption(char shortname)
          Check whether an option was present on the command line.
 boolean hasOption(String longname)
          Check whether an option was present on the command line.
 void parse(String[] args)
          Parse a command line.
 
Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_ARGUMENT

public static final int NO_ARGUMENT
Option takes no arguments.

See Also:
Constant Field Values

OPTIONAL_ARGUMENT

public static final int OPTIONAL_ARGUMENT
Option takes an optional argument.

See Also:
Constant Field Values

REQUIRED_ARGUMENT

public static final int REQUIRED_ARGUMENT
Option takes a required argument.

See Also:
Constant Field Values
Constructor Detail

ConvenientGetopt

public ConvenientGetopt(String progname,
                        String howToCall,
                        String docBelow)
Create new getopt object.

Parameters:
progname - name of the program (for error message)
howToCall - description of program call syntax (or null)
docBelow - documentation that goes below the options in the usage message (or null)
Method Detail

addOption

public void addOption(char shortname,
                      int hasArg,
                      String defaultValue,
                      String description)
Add an option that has no long name.

Parameters:
shortname - short name of the option ('s' for option -s)
hasArg - does the option have arguments? (no, optional, required)
defaultValue - default value for the argument
description - short description for the usage message.

addOption

public void addOption(char shortname,
                      String longname,
                      int hasArg,
                      String description,
                      String defaultValue)
Add an option that has a long name.

Parameters:
shortname - short name of the option ('s' for option -s)
longname - long name of the option ("server" for option --server)
hasArg - does the option have arguments? (no, optional, required)
defaultValue - default value for the argument
description - short description for the usage message.

parse

public void parse(String[] args)
Parse a command line. If the command line was syntactically valid, this method will store the information about the command line internally; it can then be retrieved with the hasArgument, getValue, and getRemaining methods. If the command line was syntactically invalid, the method will print the usage message and terminate the programme.

Parameters:
args - the command-line array that the main method got.

hasOption

public boolean hasOption(char shortname)
Check whether an option was present on the command line.

Parameters:
shortname - the short name of the option
Returns:
true if the option was there.

hasOption

public boolean hasOption(String longname)
Check whether an option was present on the command line.

Parameters:
longname - the long name of the option
Returns:
true if the option was there.

getValue

public String getValue(String longname)
Retrieve the value of an option. If the option was not given on the command line, or the option takes an optional argument and the argument was not given, this method will return the default value for this option.

Parameters:
longname - the long name of the option
Returns:
the value of the option; possibly the default value.

getValue

public String getValue(char shortname)
Retrieve the value of an option. If the option was not given on the command line, or the option takes an optional argument and the argument was not given, this method will return the default value for this option.

Parameters:
shortname - the short name of the option
Returns:
the value of the option; possibly the default value.

getRemaining

public List<String> getRemaining()
Retrieve those command-line arguments that don't belong to any option.

Returns:
the list of remaining command-line arguments, in order.