TUM CCSM Commons

edu.tum.cs.commons.options
Class CommandLine

java.lang.Object
  extended by edu.tum.cs.commons.options.CommandLine

public class CommandLine
extends java.lang.Object

A class providing command line parsing and usage messages using GNU syntax.

The GNU syntax is implemented as follows. There are short (single character) and long (multi character) options, just as provided by the AOption annotation. Short options are introduced using a single minus (e.g. '-h') while long options are introduced using a double minus (e.g. '--help'). The parameter for an option is either the next argument, or--in case of long options--possibly separated by an equals sign (e.g. '--file=test.txt'). Short options may be chained (e.g. '-xvf abc' instead of '-x -v -f abc'). For chained short options, only the last option may take a parameter.

Version:
$Rev: 26283 $
Author:
Benjamin Hummel, $Author: juergens $
Rating:
GREEN Hash: 11D10A0EF71752B4224881C9DF088659

Constructor Summary
CommandLine(OptionRegistry registry)
          Constructor.
 
Method Summary
 java.lang.String[] parse(CommandLineTokenStream ts)
          Parses the command line parameters implicitly given by the token stream and applies the options found.
 java.lang.String[] parse(java.lang.String[] args)
          Parses the given command line parameters and applies the options found.
 void printUsage(java.io.PrintWriter pw)
          Print the list of all supported options using reasonable default values for widths.
 void printUsage(java.io.PrintWriter pw, int firstCol, int width)
          Print the list of all supported options.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommandLine

public CommandLine(OptionRegistry registry)
Constructor.

Parameters:
registry - Registry containing the options to be used by this instance.
Method Detail

parse

public java.lang.String[] parse(java.lang.String[] args)
                         throws OptionException
Parses the given command line parameters and applies the options found. The arguments not treated as options or parameters are returned (often they are treated as file arguments). If the syntax does not conform to the options in the registry, an OptionException is thrown.

Parameters:
args - the command line arguments to be parsed.
Returns:
the remaining arguments.
Throws:
OptionException - in case of syntax errors or invalid parameters.

parse

public java.lang.String[] parse(CommandLineTokenStream ts)
                         throws OptionException
Parses the command line parameters implicitly given by the token stream and applies the options found. The arguments not treated as options or parameters are returned (often they are treated as file arguments). If the syntax does not conform to the options in the registry an IllegalArgumentException is thrown.

Parameters:
ts - Token stream containing the arguments.
Returns:
Remaining arguments.
Throws:
OptionException - in case of syntax errors or invalid parameters.

printUsage

public void printUsage(java.io.PrintWriter pw)
Print the list of all supported options using reasonable default values for widths.

Parameters:
pw - the writer used for output.

printUsage

public void printUsage(java.io.PrintWriter pw,
                       int firstCol,
                       int width)
Print the list of all supported options.

Parameters:
pw - the writer to print to.
firstCol - the width of the first column containing the option name (without the trailing space).
width - the maximal width of a line (aka terminal width).

TUM CCSM Commons

TUM CCSM Commons - 2.7