org.openbp.common.commandline
Class CommandLineParser

java.lang.Object
  extended by org.openbp.common.commandline.CommandLineParser

public class CommandLineParser
extends java.lang.Object

The command line parser parses a command line as supplied to the main method of a class via the argument array of the main method.

Author:
Heiko Erhardt

Constructor Summary
CommandLineParser()
          Default constructor.
 
Method Summary
 void addArgumentOption(java.lang.String name, java.lang.String usageMsg)
          Adds an option with an argument to the list of possible options.
 void addArgumentOption(java.lang.String name, java.lang.String usageMsg, java.lang.String dflt)
          Adds an option with an argument to the list of possible options.
 void addBooleanOption(java.lang.String name, java.lang.String usageMsg)
          Adds a boolean option to the list of possible options.
 void addBooleanOption(java.lang.String name, java.lang.String usageMsg, boolean dflt)
          Adds a boolean option to the list of possible options.
 void addRepeatableOption(java.lang.String name, java.lang.String usageMsg)
          Adds a repeatable option with an argument (i\. e\. an option that occur more than one time) to the list of possible options.
 java.lang.String[] getArguments()
          Retrieves the list of command arguments (i\. e\. command line arguments that don't begin with '-' or '+'.
 boolean getBooleanOption(java.lang.String name)
          Retrieves a boolean option value.
 boolean getBooleanOption(java.lang.String name, boolean dflt)
          Retrieves a boolean option value with default value support.
 java.util.Iterator getOptions()
          Retrieves an iterator of options specified on the command line.
 java.lang.String[] getRepeatableOption(java.lang.String name)
          Retrieves a repeatable string option value.
 java.lang.String getStringOption(java.lang.String name)
          Retrieves a string option value.
 void parse(java.lang.String[] args)
          Parses the command line of a Java program.
 void printUsage()
          Prints a usage message with all options to the standard error output.
 void printUsageAndExit()
          Prints a usage message with all options to the standard error output and exits the program with error code 1.
 void setAcceptUnknownOptions(boolean acceptUnknownOptions)
          Sets the flag if unknown options should be accepted (false by default).
 void setUsageMsgHeader(java.lang.String[] usageMsgHeader)
          Sets the header lines for the usage message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommandLineParser

public CommandLineParser()
Default constructor.

Method Detail

addBooleanOption

public void addBooleanOption(java.lang.String name,
                             java.lang.String usageMsg)
Adds a boolean option to the list of possible options.

Parameters:
name - Name of the option
usageMsg - Help message for this option

addBooleanOption

public void addBooleanOption(java.lang.String name,
                             java.lang.String usageMsg,
                             boolean dflt)
Adds a boolean option to the list of possible options.

Parameters:
name - Name of the option
usageMsg - Help message for this option
dflt - Default value of the option

addArgumentOption

public void addArgumentOption(java.lang.String name,
                              java.lang.String usageMsg)
Adds an option with an argument to the list of possible options.

Parameters:
name - Name of the option
usageMsg - Help message for this option

addArgumentOption

public void addArgumentOption(java.lang.String name,
                              java.lang.String usageMsg,
                              java.lang.String dflt)
Adds an option with an argument to the list of possible options.

Parameters:
name - Name of the option
usageMsg - Help message for this option
dflt - Default value of the option

addRepeatableOption

public void addRepeatableOption(java.lang.String name,
                                java.lang.String usageMsg)
Adds a repeatable option with an argument (i\. e\. an option that occur more than one time) to the list of possible options.

Parameters:
name - Name of the option
usageMsg - Help message for this option

setAcceptUnknownOptions

public void setAcceptUnknownOptions(boolean acceptUnknownOptions)
Sets the flag if unknown options should be accepted (false by default).


setUsageMsgHeader

public void setUsageMsgHeader(java.lang.String[] usageMsgHeader)
Sets the header lines for the usage message.


parse

public void parse(java.lang.String[] args)
           throws CommandLineParserException
Parses the command line of a Java program.

Parameters:
args - Array of program arguments as passed to the main method of the program
Throws:
CommandLineParserException - If a supplied option is not contained in the list of possible arguments or an option argument is missing.

printUsageAndExit

public void printUsageAndExit()
Prints a usage message with all options to the standard error output and exits the program with error code 1.


printUsage

public void printUsage()
Prints a usage message with all options to the standard error output.


getOptions

public java.util.Iterator getOptions()
Retrieves an iterator of options specified on the command line. The value of an option can be retrieved using one of the the get...Option methods.

Returns:
An Iterator of String objects

getStringOption

public java.lang.String getStringOption(java.lang.String name)
                                 throws CommandLineParserException
Retrieves a string option value.

Parameters:
name - Name of the option
Returns:
The option value
Throws:
CommandLineParserException - On option type error

getRepeatableOption

public java.lang.String[] getRepeatableOption(java.lang.String name)
                                       throws CommandLineParserException
Retrieves a repeatable string option value.

Parameters:
name - Name of the option
Returns:
An array of option values or null if no such option was given at all
Throws:
CommandLineParserException - On option type error

getBooleanOption

public boolean getBooleanOption(java.lang.String name,
                                boolean dflt)
                         throws CommandLineParserException
Retrieves a boolean option value with default value support.

Parameters:
name - Name of the option
dflt - Default value of the option
Returns:
The option value or the default value if this option was not specified on the command line
Throws:
CommandLineParserException - On option type error

getBooleanOption

public boolean getBooleanOption(java.lang.String name)
                         throws CommandLineParserException
Retrieves a boolean option value.

Parameters:
name - Name of the option
Returns:
The boolean option value
Throws:
CommandLineParserException - On option type error

getArguments

public java.lang.String[] getArguments()
Retrieves the list of command arguments (i\. e\. command line arguments that don't begin with '-' or '+'.

Returns:
An array of argument strings. The array size can be 0.


Copyright © 2011. All Rights Reserved.