org.pf.text
Class CommandLineArguments

java.lang.Object
  extended byorg.pf.text.CommandLineArguments

public class CommandLineArguments
extends java.lang.Object

Utility class for easy evaluation of command line arguments.

Version:
1.4
Author:
Manfred Duchrow

Constructor Summary
CommandLineArguments()
          Initialize the new instance with no arguments.
CommandLineArguments(java.lang.String[] args)
          Initialize the new instance with an array of arguments.
CommandLineArguments(java.lang.String[] args, java.lang.String switchIndicator)
          Initialize the new instance with an array of arguments and a prefix indicator for options
 
Method Summary
 void addOption(java.lang.String option)
          Adds the given option to the command line.
 void addOptionWithArgument(java.lang.String option, java.lang.String argument)
          Adds the given option to the command line.
 CommandLineArguments copy()
          Returns a copy of this object, with all internal state being the same as in the original.
 java.lang.String getArgumentAt(int index)
          Returns the argument at the specified index or null if the index is outside the bounds of the argument list.
 java.lang.String[] getArguments()
          Returns a string array containing all arguments.
 java.lang.String getArgumentValue(java.lang.String option)
          Returns the value following the specified option.
 java.lang.String getArgumentValue(java.lang.String option, java.lang.String defaultValue)
          Returns the value following the specified option.
 java.lang.String[] getArgumentValues(java.lang.String option)
          Returns all values that are found for the specified option.
 java.lang.String[] getArgumentValues(java.lang.String option, java.lang.String[] defaultValues)
          Returns the values following the specified option.
 java.lang.String[] getOptionValues(java.lang.String option)
          Returns all values after the given option which do NOT start with the configured switch indicator.
 java.lang.String getSwitchIndicator()
          Returns the prefix which must preceed each command line switch (option).
 boolean isOptionSet(java.lang.String option)
          Returns whether or not the specified option is set in the command line arguments.
 void removeOption(java.lang.String option)
          Remove the specified option from the command line arguments.
 void removeOptionWithArgument(java.lang.String option)
          Remove the specified option from the command line arguments.
 void setSwitchIndicator(java.lang.String newValue)
          Sets the prefix which must preceed each command line swith.
 int size()
          Returns the current number of argument
 java.lang.String toString()
          Returns a String with all arguments separated by blanks
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CommandLineArguments

public CommandLineArguments()
Initialize the new instance with no arguments.


CommandLineArguments

public CommandLineArguments(java.lang.String[] args)
Initialize the new instance with an array of arguments.

Parameters:
args - The arguments of the command line

CommandLineArguments

public CommandLineArguments(java.lang.String[] args,
                            java.lang.String switchIndicator)
Initialize the new instance with an array of arguments and a prefix indicator for options

Parameters:
args - The arguments of the command line
switchIndicator - A prefix for options
Method Detail

toString

public java.lang.String toString()
Returns a String with all arguments separated by blanks


isOptionSet

public boolean isOptionSet(java.lang.String option)
Returns whether or not the specified option is set in the command line arguments. The search for the option is case-sensitive.

Examples:

Parameters:
option - The option to be looked for
Throws:
java.lang.IllegalArgumentException - If the given option is null

getArgumentValue

public java.lang.String getArgumentValue(java.lang.String option,
                                         java.lang.String defaultValue)
Returns the value following the specified option. Returns the given default value if the specified option doesn't occur at all in the arguments array.
For more details see getArgumentValue(String).

Parameters:
option - The option which indicates that the next argument is the desired value
defaultValue - The default value to return if the option is not found
Throws:
java.lang.IllegalArgumentException - If the given option is null

getArgumentValue

public java.lang.String getArgumentValue(java.lang.String option)
Returns the value following the specified option. Returns null if the specified option doesn't occur at all in the arguments array.
An empty String will be returned if the option is followed by another option (detected by the switchIndicator prefix) or no further argument follows the specified option.
If an argument exists that starts with the given option name then the rest of the argument is returned as the option's value.

Examples:

Arguments: -v -f sample.xml -t
getArgumentValue( "-f" ) returns "sample.xml"

Arguments: -v -m -t
getArgumentValue( "-f" ) returns null

Arguments: -v -t -f
getArgumentValue( "-f" ) returns ""

Arguments: -v -f -t sample.xml
getArgumentValue( "-f" ) returns ""

Arguments: -v -x2000 -t
getArgumentValue( "-x" ) returns "2000"

Arguments: -v"first draft" -x -t
getArgumentValue( "-v" ) returns "first draft"

Parameters:
option - The option which indicates that the next argument is the desired value
Throws:
java.lang.IllegalArgumentException - If the given option is null

getArgumentValues

public java.lang.String[] getArgumentValues(java.lang.String option)
Returns all values that are found for the specified option. Helps to collect all values for options that can be used more than once.

Example:
Arguments: -v -x2000 -t -x 120 -xFM
getArgumentValues( "-x" ) returns { "2000", "120", "FM" }

Parameters:
option - The option which is the prefix for the desired values
Throws:
java.lang.IllegalArgumentException - If the given option is null

getArgumentValues

public java.lang.String[] getArgumentValues(java.lang.String option,
                                            java.lang.String[] defaultValues)
Returns the values following the specified option. Returns the given default values if the specified option doesn't occur at all in the arguments array.
For more details see getArgumentValue(String, String).

Parameters:
option - The option which indicates that the next arguments are the desired values
defaultValues - The default values to return if the option is not found
Throws:
java.lang.IllegalArgumentException - If the given option is null

getOptionValues

public java.lang.String[] getOptionValues(java.lang.String option)
Returns all values after the given option which do NOT start with the configured switch indicator. Returns null if the specified option doesn't occur at all in the arguments array.

Example:
Arguments: -v -x 2000 south 30 west -t -f test.txt -Uc22
getOptionValues( "-x" ) returns { "2000", "south", "30", "west }
getOptionValues( "-v" ) returns String[0]
getOptionValues( "-f" ) returns { "test.txt" }
getOptionValues( "-M" ) returns null

Parameters:
option - The option which is the prefix for the desired values
Throws:
java.lang.IllegalArgumentException - If the given option is null

addOption

public void addOption(java.lang.String option)
Adds the given option to the command line. If it is already set it is not added a second time.

Parameters:
option - An option including the switch indicator if necessary (e.g. "-x")
Throws:
java.lang.IllegalArgumentException - If the given option is null

addOptionWithArgument

public void addOptionWithArgument(java.lang.String option,
                                  java.lang.String argument)
Adds the given option to the command line. If it is already set it is not added a second time.

Parameters:
option - An option including the switch indicator if necessary (e.g. "-x")
argument - The argument of the option
Throws:
java.lang.IllegalArgumentException - If the given option or argument is null

removeOption

public void removeOption(java.lang.String option)
Remove the specified option from the command line arguments.

Parameters:
option - The option to be removed

removeOptionWithArgument

public void removeOptionWithArgument(java.lang.String option)
Remove the specified option from the command line arguments. If there's an argument following the option, it will be removed as well.

Parameters:
option - The option to be removed

copy

public CommandLineArguments copy()
Returns a copy of this object, with all internal state being the same as in the original.


getSwitchIndicator

public java.lang.String getSwitchIndicator()
Returns the prefix which must preceed each command line switch (option). The default value is "-".


setSwitchIndicator

public void setSwitchIndicator(java.lang.String newValue)
Sets the prefix which must preceed each command line swith. For example to change to Windows style call setSwitchIndicator( "/" )


size

public int size()
Returns the current number of argument


getArguments

public java.lang.String[] getArguments()
Returns a string array containing all arguments. The array will be a copy, so modifications of that array will not alter any argument in this object.


getArgumentAt

public java.lang.String getArgumentAt(int index)
Returns the argument at the specified index or null if the index is outside the bounds of the argument list.