public abstract class CliCommand
extends java.lang.Object
Provides an abstract base class that command line applications can extend to declare custom commands and switches.
This class utilizes the Composite pattern where a parent/child relationship
exists and the child is of the same type as the parent (base type in this case).
This allows for any number width and depth nodes in a command tree. The
CliBase provides the root of the command tree, with this class
being the parent/child nodes coming from the root.
A switch is an Apache commons-cli "option". The CliBase class
and this class assume these are single hyphen prefixed short names (typically a
single character) with a corresponding double hyphened long name (human readable equivalent).
A command is a leading command line argument which provides a hierarchy where a set of custom switches are scoped unique to the leaf commands of a command tree. Because of the composite pattern mentioned above anY level of width and depth is possible. However, in practice it is likely that the tree will only be one level deep (i.e. ExecutableClass verbCmd) or two levels deep (i.e. ExecutableClass nounCmd verbCmd).
The commandName used to instantiate an object of this class is used
as the leading command line argument command node names. An application can instantiate
a single CliCommand with a null commandName to create a simple
switch driven application with no command structure.
The following switches are reserved for all extending applications with some caveats:
args[0]) and can be used for other purposes
at any command leaf. The config-path and log-path switches are optional. Both are implemented
to initialize application wide Properties and logging.CliBase| Constructor and Description |
|---|
CliCommand(CliBase cliBase,
CliCommand parent,
java.lang.String commandName)
Full constructor.
|
CliCommand(CliBase cliBase,
java.lang.String commandName)
Simple constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addChild(CliCommand command)
Add a command child.
|
org.apache.commons.cli.CommandLine |
getCommandLine()
Return the CommandLine object.
|
java.lang.String |
getCommandName()
Return Command Name.
|
java.lang.String |
getLeadingCommandString(CliCommand command)
Return Leading Command String.
|
CliCommand |
getParent()
Return Parent.
|
java.lang.String |
getSpacedName(java.lang.String[] spacedName)
Convert String array into space separated name.
|
boolean |
handleConfiguration()
Handle Configuration file.
|
boolean |
hasChildren()
Report if this node has children.
|
boolean |
hasOptions()
Report if this node has
Options. |
java.lang.String |
toString() |
CliCommand |
validateCommands(int commandDepth)
Validate the command line
args. |
public CliCommand(CliBase cliBase, java.lang.String commandName)
cliBase - the Object of the class extending CliBase.
The root of the command tree. Must not be nullcommandName - the command nodes name. If null a non-command application is declared.
Otherwise, the value given is used for the name of the command node.public CliCommand(CliBase cliBase, CliCommand parent, java.lang.String commandName)
cliBase - the Object of the class extending CliBase.
The root of the command tree. Must not be nullparent - the parent command of this node. Null if the parent is the CliBase.commandName - the command nodes name. If null a non-command application is declared.
Otherwise, the value given is used for the name of the command node.public java.lang.String getLeadingCommandString(CliCommand command)
command - the node to determine the path to.String of the executable class and command names
leading to the given command. Will never return null.public CliCommand getParent()
CliBase.public java.lang.String getCommandName()
public org.apache.commons.cli.CommandLine getCommandLine()
Only leaf node commands can have a commons-cli CommandLine object.
It must also be the active command, based on the given command line arguments.
In the case of non-command mode, there is only one node and it will have
the CommandLine object.
Note that the CommandLine object is never available until
after the validateCommands method has been called.
CommandLine object. Returns non-null
if validateCommands has been called and this node is the activeCommand.
Otherwise, null is returned.public boolean hasChildren()
public boolean hasOptions()
Options.
A command node might not have any switches (i.e. clear).
Option's set, false otherwise.public void addChild(CliCommand command)
command - the child command to add.public CliCommand validateCommands(int commandDepth)
args.
This method will cause Help to be called if invalid
command line args are given. Help exits
the application and this method will never return in this case.
commandDepth - the zero based depth of this command.activeCommand if the command line was valid and
the requested command path is down my branch. Returns null otherwise.public boolean handleConfiguration()
The method will attempt to obtain the properties file from the reserved
configuration switch if it is enabled. The application wide Properties
obtained from the CliBase will be loaded with the configuration if
enabled and found.
If the configuration switch is enabled and the Properties cannot be
loaded with the given file, this method will call Help with error information. Help
will exit the application and this method will never return in this case.
public java.lang.String getSpacedName(java.lang.String[] spacedName)
spacedName - the string array to be space seperated.public java.lang.String toString()
toString in class java.lang.Object