|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.pfsw.text.CommandLineArguments
public class CommandLineArguments
Utility class for easy evaluation of command line arguments.
| Field Summary | |
|---|---|
protected static java.lang.String |
DEFAULT_SWITCH_INDICATOR
|
protected static java.lang.String |
QUOTE
|
protected static java.lang.String |
STRING_DELIMITER
|
| Constructor Summary | |
|---|---|
|
CommandLineArguments()
Initialize the new instance with no arguments. |
|
CommandLineArguments(boolean switchAndValuesMustBeSeparated)
Initialize the new instance to handle arguments with separated option flag and option value. |
|
CommandLineArguments(java.lang.String[] args)
Initialize the new instance with an array of arguments. |
|
CommandLineArguments(java.lang.String[] args,
boolean switchAndValuesMustBeSeparated)
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. |
|
CommandLineArguments(java.lang.String[] args,
java.lang.String switchIndicator,
boolean switchAndValuesMustBeSeparated)
Initialize the new instance with an array of arguments and a prefix indicator for options. |
protected |
CommandLineArguments(java.lang.String[] args,
java.lang.String switchIndicator,
boolean switchAndValuesMustBeSeparated,
boolean supportQuotedArguments)
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. |
protected void |
addOptionWithValue(java.util.Map<java.lang.String,java.lang.String[]> map,
java.lang.String option,
java.lang.String value)
|
protected void |
addOptionWithValues(java.util.Map<java.lang.String,java.lang.String[]> map,
java.lang.String option,
java.util.List<java.lang.String> values)
|
protected java.lang.String[] |
arguments()
|
protected void |
arguments(java.lang.String[] newValue)
|
boolean |
containsAny(java.lang.String... argNames)
Returns true one of the given argument names has been specified on the command line. |
boolean |
containsArgument(java.lang.String argName)
Returns whether or not the given argument was specified in the command line. |
CommandLineArguments |
copy()
Returns a copy of this object, with all internal state being the same as in the original. |
protected java.lang.String[] |
copyOfArguments()
Returns a copy of the original arguments. |
static CommandLineArguments |
create(java.lang.String... options)
Convenience method with varargs to create a new instance. |
protected java.lang.String |
findOptionValue(java.lang.String option)
|
protected java.lang.String[] |
findValues(java.lang.String option)
|
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 |
getFirstFoundValue(java.lang.String... options)
Returns the value following the first option found that matches one of the specified options. |
protected java.util.Map<java.lang.String,java.lang.String[]> |
getOptionsWithValues()
|
java.lang.String[] |
getOptionValues(java.lang.String option)
Returns all values after the given option which do NOT start with the configured switch indicator. |
boolean |
getSupportQuotedArguments()
Returns whether or not quoted arguments are supported. |
protected boolean |
getSwitchAndValuesMustBeSeparated()
|
java.lang.String |
getSwitchIndicator()
Returns the prefix which must proceed each command line switch (option). |
protected void |
init()
|
boolean |
isEmpty()
Returns true if no argument is set. |
protected boolean |
isOption(java.lang.String arg)
|
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. |
protected void |
setOptionsWithValues(java.util.Map<java.lang.String,java.lang.String[]> newValue)
|
void |
setSupportQuotedArguments(boolean supportThem)
Defines whether or not quoted arguments are supported. |
protected void |
setSwitchAndValuesMustBeSeparated(boolean newValue)
|
void |
setSwitchIndicator(java.lang.String newValue)
Sets the prefix which must proceed each command line switch. |
int |
size()
Returns the current number of argument |
protected StringUtil |
strUtil()
|
protected java.lang.String |
switchIndicator()
|
protected void |
switchIndicator(java.lang.String newValue)
|
java.lang.String |
toString()
Returns a String with all arguments separated by blanks |
protected java.lang.String |
unquoted(java.lang.String value)
|
protected java.lang.String |
unquotedIfSupported(java.lang.String value)
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected static final java.lang.String STRING_DELIMITER
protected static final java.lang.String DEFAULT_SWITCH_INDICATOR
protected static final java.lang.String QUOTE
| Constructor Detail |
|---|
public CommandLineArguments()
public CommandLineArguments(boolean switchAndValuesMustBeSeparated)
switchAndValuesMustBeSeparated - If true no switches with length > 2
will be treated as a one character option switch plus the remaining string as its value.public CommandLineArguments(java.lang.String[] args)
args - The arguments of the command line
public CommandLineArguments(java.lang.String[] args,
boolean switchAndValuesMustBeSeparated)
args - The arguments of the command lineswitchAndValuesMustBeSeparated - If true no switches with length > 2
will be treated as a one character option switch plus the remaining string as its value.
public CommandLineArguments(java.lang.String[] args,
java.lang.String switchIndicator)
args - The arguments of the command lineswitchIndicator - A prefix for options
public CommandLineArguments(java.lang.String[] args,
java.lang.String switchIndicator,
boolean switchAndValuesMustBeSeparated)
args - The arguments of the command lineswitchIndicator - A prefix for optionsswitchAndValuesMustBeSeparated - If true no switches with length > 2
will be treated as a one character option switch plus the remaining string as its value.
protected CommandLineArguments(java.lang.String[] args,
java.lang.String switchIndicator,
boolean switchAndValuesMustBeSeparated,
boolean supportQuotedArguments)
args - The arguments of the command lineswitchIndicator - A prefix for optionsswitchAndValuesMustBeSeparated - If true no switches with length > 2
will be treated as a one character option switch plus the remaining string as its value.supportQuotedArguments - Handle wildcard patterns enclosed in quotes.| Method Detail |
|---|
public static CommandLineArguments create(java.lang.String... options)
options - The options to be used as arguments.
public java.lang.String toString()
toString in class java.lang.Objectpublic boolean isOptionSet(java.lang.String option)
Examples:
option - The option to be looked for
java.lang.IllegalArgumentException - If the given option is nullpublic boolean containsArgument(java.lang.String argName)
Examples:
argName - The name of the argument to look for
java.lang.IllegalArgumentException - If the given argName is nullpublic boolean containsAny(java.lang.String... argNames)
public java.lang.String getArgumentValue(java.lang.String option,
java.lang.String defaultValue)
getArgumentValue(String).
option - The option which indicates that the next argument is the desired valuedefaultValue - The default value to return if the option is not found
java.lang.IllegalArgumentException - If the given option is nullpublic java.lang.String getArgumentValue(java.lang.String option)
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"
option - The option which indicates that the next argument is the desired value
java.lang.IllegalArgumentException - If the given option is null.public java.lang.String getFirstFoundValue(java.lang.String... options)
options - The option which indicates that the next argument is the desired value
java.lang.IllegalArgumentException - If no option is specified.public java.lang.String[] getArgumentValues(java.lang.String option)
Example:
Arguments: -v -x2000 -t -x 120 -xFM
getArgumentValues( "-x" ) returns { "2000", "120", "FM" }
option - The option which is the prefix for the desired values
java.lang.IllegalArgumentException - If the given option is null
public java.lang.String[] getArgumentValues(java.lang.String option,
java.lang.String... defaultValues)
getArgumentValue(String, String).
option - The option which indicates that the next arguments are the desired valuesdefaultValues - The default values to return if the option is not found
java.lang.IllegalArgumentException - If the given option is nullpublic java.lang.String[] getOptionValues(java.lang.String option)
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
option - The option which is the prefix for the desired values
java.lang.IllegalArgumentException - If the given option is nullpublic void addOption(java.lang.String option)
option - An option including the switch indicator if necessary (e.g. "-x")
java.lang.IllegalArgumentException - If the given option is null
public void addOptionWithArgument(java.lang.String option,
java.lang.String argument)
option - An option including the switch indicator if necessary (e.g. "-x")argument - The argument of the option
java.lang.IllegalArgumentException - If the given option or argument is nullpublic void removeOption(java.lang.String option)
option - The option to be removedpublic void removeOptionWithArgument(java.lang.String option)
option - The option to be removedpublic CommandLineArguments copy()
public java.lang.String getSwitchIndicator()
public void setSwitchIndicator(java.lang.String newValue)
public int size()
public boolean isEmpty()
public java.lang.String[] getArguments()
public java.lang.String getArgumentAt(int index)
public boolean getSupportQuotedArguments()
public void setSupportQuotedArguments(boolean supportThem)
protected void init()
protected void addOptionWithValue(java.util.Map<java.lang.String,java.lang.String[]> map,
java.lang.String option,
java.lang.String value)
protected void addOptionWithValues(java.util.Map<java.lang.String,java.lang.String[]> map,
java.lang.String option,
java.util.List<java.lang.String> values)
protected boolean isOption(java.lang.String arg)
protected java.lang.String findOptionValue(java.lang.String option)
protected java.lang.String[] findValues(java.lang.String option)
protected java.lang.String[] copyOfArguments()
protected java.lang.String unquotedIfSupported(java.lang.String value)
protected java.lang.String unquoted(java.lang.String value)
protected java.lang.String[] arguments()
protected void arguments(java.lang.String[] newValue)
protected java.lang.String switchIndicator()
protected void switchIndicator(java.lang.String newValue)
protected java.util.Map<java.lang.String,java.lang.String[]> getOptionsWithValues()
protected void setOptionsWithValues(java.util.Map<java.lang.String,java.lang.String[]> newValue)
protected boolean getSwitchAndValuesMustBeSeparated()
protected void setSwitchAndValuesMustBeSeparated(boolean newValue)
protected StringUtil strUtil()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||