class Command extends AbstractCommand (View source)

Base Command class.

Methods

__construct(string $name = null, IOInterface $io = null, AbstractCommand $parent = null)

Console constructor.

mixed
execute()

Execute this command.

getIO()

Method to get property Io

setIO(IOInterface $io)

Method to set property io

setParent(AbstractCommand $parent = null)

Parent command setter.

getParent()

Get Parent Command.

addCommand(string|AbstractCommand $command, string $description = null, Option[] $options = array(), Closure $code = null)

Add an argument(sub command) setting. This method in Command use 'self' instead 'static' to make sure every sub command add Command class as arguments.

null|string
getArgument(int $offset, callable|mixed $default = null)

Get argument by offset or return default.

addChild(string|AbstractCommand $argument, string $description = null, Option[] $options = array(), Closure $handler = null)

Alias of addCommand if someone think child is more semantic.

getChild(string $path)

Get a argument(command) by name path.

array
getChildren()

Get children array.

setChildren(array $children)

Batch set children (sub commands).

addOption(mixed $option, mixed $default = null, string $description = null)

Add a option object to this command.

addGlobalOption(mixed $option, mixed $default = null, string $description = null)

Add a option object to this command.

mixed
getOption(string $name, string $default = null)

Get value from an option.

Option[]
getOptions(boolean $global = false)

Get options as array.

getOptionSet(boolean $global = false)

Get option set object.

array
getAllOptions()

Get all options include global.

setOptions(mixed $options)

Batch add options to command.

setOptionAliases(mixed $aliases, string $name, bool $global = false)

set the option alias.

string
getDescription()

The command description getter.

description(string $description)

The command description setter.

string
getName()

Console name getter.

setName(string $name)

Console name setter.

getHandler()

Console execute code getter.

handler(callable $handler = null)

Console execute code setter.

getApplication()

Get the application.

setApplication(Console $application)

Set the application.

string
getHelp()

Get the help manual.

help(string $help)

Sets the help manual

string
getUsage()

Get the usage.

usage(string $usage)

Sets the usage to tell user how to use this command.

void
renderAlternatives(string $wrongName, CommandNotFoundException $exception)

Render auto complete alternatives.

void
renderException(Exception $exception)

Render exception for debugging.

void
error(Exception|string $exception)

Raise error message.

out(string $text = '', boolean $nl = true)

Write a string to standard output.

err(string $text = '', boolean $nl = true)

Write a string to standard error output.

string
in(string $question = '')

Get a value from standard input.

void
offsetSet(mixed $offset, AbstractCommand $value)

Set child command, note the key is no use, we use command name as key.

boolean
offsetExists(string $offset)

Is a child exists?

void
offsetUnset(string $offset)

Unset a child command.

offsetGet(string $offset)

Get a command by name.

Details

__construct(string $name = null, IOInterface $io = null, AbstractCommand $parent = null)

Console constructor.

Parameters

string $name Console name.
IOInterface $io Cli input object.
AbstractCommand $parent Parent Console.

Exceptions

LogicException

mixed execute()

Execute this command.

Return Value

mixed Executed result or exit code.

Exceptions

Exception

IOInterface getIO()

Method to get property Io

Return Value

IOInterface

AbstractCommand setIO(IOInterface $io)

Method to set property io

Parameters

IOInterface $io

Return Value

AbstractCommand Return self to support chaining.

AbstractCommand setParent(AbstractCommand $parent = null)

Parent command setter.

Parameters

AbstractCommand $parent The parent comment.

Return Value

AbstractCommand Return this object to support chaining.

AbstractCommand getParent()

Get Parent Command.

Return Value

AbstractCommand

at line line 50
AbstractCommand addCommand(string|AbstractCommand $command, string $description = null, Option[] $options = array(), Closure $code = null)

Add an argument(sub command) setting. This method in Command use 'self' instead 'static' to make sure every sub command add Command class as arguments.

Parameters

string|AbstractCommand $command The argument name or Console object. If we just send a string, the object will auto create.
string $description Console description.
Option[] $options Console options.
Closure $code The closure to execute.

Return Value

AbstractCommand Return this object to support chaining.

null|string getArgument(int $offset, callable|mixed $default = null)

Get argument by offset or return default.

Parameters

int $offset Argument offset.
callable|mixed $default Default value, if is a callable, will execute it.

Return Value

null|string Values from argument or user input.

AbstractCommand addChild(string|AbstractCommand $argument, string $description = null, Option[] $options = array(), Closure $handler = null)

Alias of addCommand if someone think child is more semantic.

Parameters

string|AbstractCommand $argument The argument name or Console object. If we just send a string, the object will auto create.
string $description Console description.
Option[] $options Console options.
Closure $handler The closure to execute.

Return Value

AbstractCommand Return this object to support chaining.

AbstractCommand|null getChild(string $path)

Get a argument(command) by name path.

Parameters

string $path Command name path.

Return Value

AbstractCommand|null Return command or null.

array getChildren()

Get children array.

Return Value

array children.

AbstractCommand setChildren(array $children)

Batch set children (sub commands).

Parameters

array $children An array include argument objects.

Return Value

AbstractCommand Return this object to support chaining.

Option addOption(mixed $option, mixed $default = null, string $description = null)

Add a option object to this command.

Parameters

mixed $option The option name. Can be a string, an array or an object. If we use array, the first element will be option name, others will be alias.
mixed $default The default value when we get a non-exists option.
string $description The option description.

Return Value

Option Return Option object.

Option addGlobalOption(mixed $option, mixed $default = null, string $description = null)

Add a option object to this command.

Parameters

mixed $option The option name. Can be a string, an array or an object. If we use array, the first element will be option name, others will be alias.
mixed $default The default value when we get a non-exists option.
string $description The option description.

Return Value

Option Return Option object.

mixed getOption(string $name, string $default = null)

Get value from an option.

If the name not found, we use alias to find options.

Parameters

string $name The option name.
string $default The default value when option not set.

Return Value

mixed The option value we want to get or default value if option not exists.

Option[] getOptions(boolean $global = false)

Get options as array.

Parameters

boolean $global is Global options.

Return Value

Option[] The options array.

OptionSet getOptionSet(boolean $global = false)

Get option set object.

Parameters

boolean $global is Global options.

Return Value

OptionSet The options array.

array getAllOptions()

Get all options include global.

Return Value

array The options array.

AbstractCommand setOptions(mixed $options)

Batch add options to command.

Parameters

mixed $options An options array.

Return Value

AbstractCommand Return this object to support chaining.

AbstractCommand setOptionAliases(mixed $aliases, string $name, bool $global = false)

set the option alias.

Parameters

mixed $aliases The alias to map this option.
string $name The option name.
bool $global Is global option?

Return Value

AbstractCommand Return this object to support chaining.

string getDescription()

The command description getter.

Return Value

string Console description.

AbstractCommand description(string $description)

The command description setter.

Parameters

string $description Console description.

Return Value

AbstractCommand Return this object to support chaining.

string getName()

Console name getter.

Return Value

string Console name.

AbstractCommand setName(string $name)

Console name setter.

Parameters

string $name Console name.

Return Value

AbstractCommand Return this object to support chaining.

Closure getHandler()

Console execute code getter.

Return Value

Closure Console execute code.

AbstractCommand handler(callable $handler = null)

Console execute code setter.

Parameters

callable $handler Console execute handler.

Return Value

AbstractCommand Return this object to support chaining.

Console getApplication()

Get the application.

Return Value

Console Console application.

AbstractCommand setApplication(Console $application)

Set the application.

Parameters

Console $application Application object.

Return Value

AbstractCommand Return this object to support chaining.

string getHelp()

Get the help manual.

Return Value

string Help of this Command.

AbstractCommand help(string $help)

Sets the help manual

Parameters

string $help The help manual.

Return Value

AbstractCommand Return this object to support chaining.

string getUsage()

Get the usage.

Return Value

string Usage of this command.

AbstractCommand usage(string $usage)

Sets the usage to tell user how to use this command.

Parameters

string $usage Usage of this command.

Return Value

AbstractCommand Return this object to support chaining.

void renderAlternatives(string $wrongName, CommandNotFoundException $exception)

Render auto complete alternatives.

Parameters

string $wrongName The wrong command name to auto completed.
CommandNotFoundException $exception The exception of wrong argument.

Return Value

void

void renderException(Exception $exception)

Render exception for debugging.

Parameters

Exception $exception The exception we want to render.

Return Value

void

void error(Exception|string $exception)

Raise error message.

Parameters

Exception|string $exception Exception object or message string.

Return Value

void

Exceptions

Exception

AbstractCommand out(string $text = '', boolean $nl = true)

Write a string to standard output.

Parameters

string $text The text to display.
boolean $nl True (default) to append a new line at the end of the output string.

Return Value

AbstractCommand Instance of $this to allow chaining.

AbstractCommand err(string $text = '', boolean $nl = true)

Write a string to standard error output.

Parameters

string $text The text to display.
boolean $nl True (default) to append a new line at the end of the output string.

Return Value

AbstractCommand Instance of $this to allow chaining.

string in(string $question = '')

Get a value from standard input.

Parameters

string $question The question you want to ask user.

Return Value

string The input string from standard input.

void offsetSet(mixed $offset, AbstractCommand $value)

Set child command, note the key is no use, we use command name as key.

Parameters

mixed $offset No use here.
AbstractCommand $value Command object.

Return Value

void

boolean offsetExists(string $offset)

Is a child exists?

Parameters

string $offset The command name to get command.

Return Value

boolean True if command exists.

void offsetUnset(string $offset)

Unset a child command.

Parameters

string $offset The command name to remove.

Return Value

void

AbstractCommand|null offsetGet(string $offset)

Get a command by name.

Parameters

string $offset The command name to get command.

Return Value

AbstractCommand|null Return command object if found.