public class CommandLineArguments
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
protected static java.lang.String |
DEFAULT_SWITCH_INDICATOR |
protected static java.lang.String |
QUOTE |
protected static java.lang.String |
STRING_DELIMITER |
Modifier | Constructor and Description |
---|---|
|
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.
|
Modifier and Type | Method and Description |
---|---|
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) |
protected static final java.lang.String STRING_DELIMITER
protected static final java.lang.String DEFAULT_SWITCH_INDICATOR
protected static final java.lang.String QUOTE
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 linepublic 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 optionspublic 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.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.Object
public boolean isOptionSet(java.lang.String option)
Examples:
option
- The option to be looked forjava.lang.IllegalArgumentException
- If the given option is nullpublic boolean containsArgument(java.lang.String argName)
Examples:
argName
- The name of the argument to look forjava.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 foundjava.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 valuejava.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 valuejava.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 valuesjava.lang.IllegalArgumentException
- If the given option is nullpublic 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 foundjava.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 valuesjava.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 nullpublic 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 optionjava.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()