Represents a command to be fired by the CLI application. This class should be extended to implement the execute() method. All commands support command chains - a set of commands that this command can execute (e.g. cliCommandNew).
Each commend must receive an instance of the cliRequest and should have a command pattern. The command pattern is the keyword required from the command line to execute it. This keyword may be a value of another command (see cliCommandNew).
Each command can have a number of aliases - these are usually a single character that can be used to execute the command e.g. the log command has v and V as aliases to enable verbose or very verbose logging.
Additional properties are help text and if the command requires a value to be executed. This information will be used by the cliCommandHelp command which formats help information about the current application.
Lastly: commands can redirect to other commands. The redirect can be to a command within the current command chain, or if not in that set, the application command chain will be searched.
// short example of a command
class myCommand extends cliCommand {
function __construct($inRequest) {
parent::__construct($inRequest, 'mycomm');
$this->setCommandHelp('This is my command example');
// un-comment the next line to force a value for this command
//$this->setCommandRequiresValue(true);
}
function execute() {
echo "Hello world\n";
}
}
By componentising commands and keeping them to a single purpose it is possible to re-use huge chunks of code e.g. the 'new', help and log commands can all be re-used easily.
Lastly: to fire events from a command, reference the request and fetch the application instance and then call cliApplication::notify() and pass in a cliApplicationEvent.
public __construct($inRequest, [$inCommandPattern = null], [$inCommandChain = null], [$inCommandAliases = null])
Creates a new cliCommand
cliCommand always requires the current cliRequest, however all other constructor parameters are optional. For $inCommandChain, a cliCommandChain object must always be set, however $inCommandAliases can be either a single character string, array of characters or a cliCommandAliases object.
public abstract execute()
Executes the command
public redirect($inCommand)
Redirect processing to a new command $inCommand
protected _findCommand($inCommand)
Attempts to locate $inCommand in the current chain, or in the application
public reset()
Reset object
public isModified()
Returns true if object has been modified
public setModified([$status = true])
Set the status of the object if it has been changed
public getCommandPattern()
Returns $_CommandPattern
public setCommandPattern($inCommandPattern)
Set $_CommandPattern to $inCommandPattern
public getCommandAliases()
Returns $_CommandAliases
public setCommandAliases($inCommandAliases)
Set $_CommandAliases to $inCommandAliases
public getCommandChain()
Returns $_CommandChain
public setCommandChain($inCommandChain)
Set $_CommandChain to $inCommandChain
public getCommandHelp()
Returns $_CommandHelp
public setCommandHelp($inCommandHelp)
Set $_CommandHelp to $inCommandHelp
public getCommandRequiresValue()
Used by the help system, if true adds [=value] to help output
public setCommandRequiresValue($inCommandRequiresValue)
Set $_CommandRequiresValue to $inCommandRequiresValue
public getCommandIsSwitch()
Returns $_CommandIsSwitch
public setCommandIsSwitch($inCommandIsSwitch)
Set $_CommandIsSwitch to $inCommandIsSwitch
public getCommandIsOptional()
Returns $_CommandIsOptional
public setCommandIsOptional($inCommandIsOptional)
Set $_CommandIsOptional to $inCommandIsOptional
public haltAppAfterExecute()
Returns true if the application should be halted after this command executes. Used to control initial app firing.
public setHaltAppAfterExecute($inHaltAppAfterExecute)
Set $_HaltAppAfterExecute to $inHaltAppAfterExecute
public getRequest()
Returns request object
public setRequest($inRequest)
Set the cliRequest object
Tags: cli, clicommand,
cli Articles