public abstract class AbstractCommand extends Object implements Runnable
CommandLineArguments object represents a the
command-line arguments. It is constructed with an array of
command-line arguments and an optional properties value specifying
default values.
Arguments of the form
-Property=Value induce the property
value pair specified. Argument values may be empty, in which
case the value will be the empty string.
Arguments of the form -Flag not matching the
previous form induce properties Flag=yes. Bare
arguments with of the form BareArgument induce
properties BARE_ARG_i=BareArgument so
that bare arguments may be retrieved by position; bare arguments
equal to the empty string are ignored.
Because commands are based on the run() method, they
have been defined to implement the Runnable interface.
| Modifier and Type | Field and Description |
|---|---|
static String |
BARE_ARG_PREFIX
The string prefixed before a number to indicate bare arguments.
|
static String |
HAS_PROPERTY_VALUE
The value assigned to arguments beginning with
'-'
and not containing an '='. |
| Constructor and Description |
|---|
AbstractCommand(String[] args)
Constructs a command-line arguments object from the specified
arguments list using no default values.
|
AbstractCommand(String[] args,
Properties defaultProperties)
Constructs a command-line arguments object from the specified
arguments using the specified properties as default values.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addDefaultProperty(String property,
String defaultValue)
Adds the specified key and value as default arguments.
|
String[] |
bareArguments()
Returns the array of bare arguments, in order, supplied on the
command line.
|
void |
checkParameterImplication(String ifParam,
String thenParam)
Throws an illegal argument exception if the first parameter is
defined in the command the second parameter is not.
|
long |
elapsedTimeMillis()
Return the elapsed time since the construction of
this command in milliseconds.
|
String |
getArgument(String key)
Returns the value of the command-line argument with the
specified key, or the value of the key in the default
properties, or
null if it does not exist. |
protected File |
getArgumentCreatableFile(String fileParam)
Returns the model file specified by the command line
after guaranteeing that the file can be created.
|
File |
getArgumentDirectory(String key)
Returns the value of the argument with the specified key
converted to a directory.
|
double |
getArgumentDouble(String key)
Returns the value of the argument with the specified key
converted to a double value.
|
File |
getArgumentExistingNormalFile(String key)
Returns the existing normal file named by the value of
the specified key.
|
File |
getArgumentFile(String key)
Returns the value of the argument with the specified key
converted to a file.
|
int |
getArgumentInt(String key)
Returns the value of the argument with the specified key
converted to an integer value.
|
long |
getArgumentLong(String key)
Returns the value of the argument with the specified key
converted to a long value.
|
Properties |
getArguments()
Returns the complete list of command-line parameters for this
abstract command.
|
String |
getBareArgument(int n)
Returns the specified bare argument.
|
String |
getExistingArgument(String key)
Returns the same value as
getArgument(String), but
throws an exception if the argument does not exist. |
File |
getOrCreateArgumentDirectory(String key)
Returns the value of the argument with the specified key
converted to a directory.
|
boolean |
hasArgument(String key)
Returns
true if there is a command-line
argument specified for the key. |
boolean |
hasFlag(String arg)
Returns
true if the arguments set the specified
flag. |
boolean |
hasProperty(String arg)
Returns
true if the arguments have the
specified property defined. |
protected void |
illegalArgument(String msg)
Throw an illegal argument exception with the specified message.
|
protected void |
illegalArgument(String msg,
Exception e)
Throw an illegal argument exception with the specified message
with a source provided by the specified exception.
|
protected void |
illegalArgument(String msg,
String arg)
Throw an illegal argument exception with the specified message
and a report of the argument found.
|
protected void |
illegalPropertyArgument(String msg,
String key)
Throw an illegal argument message with the specified message
and a report of the specified key and its value.
|
int |
numBareArguments()
Returns the number of bare arguments supplied on the command
line.
|
abstract void |
run()
Runs the abstract command.
|
long |
startTimeMillis()
Return the time at which this command was constructed.
|
public static final String HAS_PROPERTY_VALUE
'-'
and not containing an '='.public static final String BARE_ARG_PREFIX
BARE_ARG_PREFIX + 5.public AbstractCommand(String[] args)
args - Command-line arguments.IllegalArgumentException - If the arguments are not well
formed.public AbstractCommand(String[] args, Properties defaultProperties)
args - Command-line arguments.defaultProperties - Default values for properties.IllegalArgumentException - If the arguments are not well
formed.public void addDefaultProperty(String property, String defaultValue)
property - Name of property.defaultValue - Default value for property.public long elapsedTimeMillis()
public long startTimeMillis()
public int numBareArguments()
public String[] bareArguments()
-) indicating a flag or
property.public boolean hasFlag(String arg)
true if the arguments set the specified
flag. Note that this method returns false for
properties; see hasProperty(String).arg - The argument to test for existence on the command line.true if the argument was specified on the
command line preceded by a '-'.public boolean hasProperty(String arg)
true if the arguments have the
specified property defined. Note that this method
returns false for flags; see hasFlag(String).public String getBareArgument(int n)
n - Index of bare argument to return.null if not specified.public String getArgument(String key)
null if it does not exist. The
property exists in the command-line if it was specified by
"-Arg=Value.key - Name of command-line argument to return.public Properties getArguments()
Flags are included as properties with the flag as key and
value HAS_PROPERTY_VALUE. Bare arguments are included
with prefixes consisting of BARE_ARG_PREFIX followed
by a number.
public String getExistingArgument(String key) throws IllegalArgumentException
getArgument(String), but
throws an exception if the argument does not exist.key - Name of command-line argument to return.IllegalArgumentException - If there is no value specified
for the given key.public boolean hasArgument(String key)
true if there is a command-line
argument specified for the key.true if there is a command-line argument
specified for the key.public int getArgumentInt(String key)
key - Name of argument to convert to an integer and
return.IllegalArgumentException - If there is a number format
exception converting the argument to an integer, or if there is
no value supplied.public long getArgumentLong(String key)
key - Name of argument to convert to a long and
return.IllegalArgumentException - If there is a number format
exception converting the argument to an integer, or if there is
no value supplied.public double getArgumentDouble(String key)
key - Name of argument to convert to a double and return.IllegalArgumentException - If there is a number format
exception converting the argument to a double, or if there is
no value supplied.public File getArgumentFile(String key)
key - Name of argument to convert to a file and return.IllegalArgumentException - If the argument is not
specified.public File getArgumentExistingNormalFile(String key)
key - Name of argument to convert to file, check for
existence, and return.IllegalArgumentException - If the argument has no value
or is not an existing file.public File getArgumentDirectory(String key)
key - Name of argument to convert to a directory and return.IllegalArgumentException - If the argument is not
specified, or is not an existing directory.public File getOrCreateArgumentDirectory(String key)
key - Name of argument to convert to a directory and return.IllegalArgumentException - If the argument is not
specified, and is not an existing or creatable directory.protected File getArgumentCreatableFile(String fileParam)
IllegalArgumentException - If the model is not specified
on the command line or is not a file that can be created.protected void illegalPropertyArgument(String msg, String key)
msg - Message to display.key - Key required or found on the command line.IllegalArgumentException - Always, with specified message and
a report of the found argument.protected void illegalArgument(String msg, String arg)
msg - Message to display.arg - The argument found on the command line.IllegalArgumentException - Always, with specified message and
a report of the found argument.protected void illegalArgument(String msg, Exception e)
msg - Message to display.e - Exception causing the illegal argument exception.IllegalArgumentException - Always, with specified message and
a report of embedded exception.protected void illegalArgument(String msg)
msg - Message to include in the exception.IllegalArgumentException - Always, with the specified message.public void checkParameterImplication(String ifParam, String thenParam)
ifParam - Parameter to test for definedness.thenParam - Parameter to test for definedness.IllegalArgumentException - If the first parameter is
defined and the second is not.Copyright © 2019 Alias-i, Inc.. All rights reserved.