Documentation

Command
in package

Base class for all commands.

Tags
author

Fabien Potencier fabien@symfony.com

Table of Contents

Constants

FAILURE  = 1
INVALID  = 2
SUCCESS  = 0

Properties

$defaultDescription  : string|null
$defaultName  : string|null
$aliases  : array<string|int, mixed>
$application  : Application|null
$code  : Closure|null
$definition  : InputDefinition
$description  : string
$fullDefinition  : InputDefinition|null
$help  : string
$helperSet  : HelperSet|null
$hidden  : bool
$ignoreValidationErrors  : bool
$name  : string|null
$processTitle  : string|null
$synopsis  : array<string|int, mixed>
$usages  : array<string|int, mixed>

Methods

__construct()  : mixed
addArgument()  : $this
Adds an argument.
addOption()  : $this
Adds an option.
addUsage()  : $this
Add a command usage example, it'll be prefixed with the command name.
complete()  : void
Adds suggestions to $suggestions for the current completion input (e.g. option or argument).
getAliases()  : array<string|int, mixed>
Returns the aliases for the command.
getApplication()  : Application|null
Gets the application instance for this command.
getDefaultDescription()  : string|null
getDefaultName()  : string|null
getDefinition()  : InputDefinition
Gets the InputDefinition attached to this Command.
getDescription()  : string
Returns the description for the command.
getHelp()  : string
Returns the help for the command.
getHelper()  : HelperInterface
Gets a helper instance by name.
getHelperSet()  : HelperSet|null
Gets the helper set.
getName()  : string|null
Returns the command name.
getNativeDefinition()  : InputDefinition
Gets the InputDefinition to be used to create representations of this Command.
getProcessedHelp()  : string
Returns the processed help for the command replacing the %command.name% and %command.full_name% patterns with the real values dynamically.
getSynopsis()  : string
Returns the synopsis for the command.
getUsages()  : array<string|int, mixed>
Returns alternative usages of the command.
ignoreValidationErrors()  : void
Ignores validation errors.
isEnabled()  : bool
Checks whether the command is enabled or not in the current environment.
isHidden()  : bool
run()  : int
Runs the command.
setAliases()  : $this
Sets the aliases for the command.
setApplication()  : void
setCode()  : $this
Sets the code to execute when running this command.
setDefinition()  : $this
Sets an array of argument and option instances.
setDescription()  : $this
Sets the description for the command.
setHelp()  : $this
Sets the help for the command.
setHelperSet()  : void
setHidden()  : $this
setName()  : $this
Sets the name of the command.
setProcessTitle()  : $this
Sets the process title of the command.
configure()  : void
Configures the current command.
execute()  : int
Executes the current command.
initialize()  : void
Initializes the command after the input has been bound and before the input is validated.
interact()  : void
Interacts with the user.
validateName()  : void
Validates a command name.

Constants

Properties

$defaultDescription

protected static string|null $defaultDescription

The default command description

Tags
deprecated

since Symfony 6.1, use the AsCommand attribute instead

$defaultName

protected static string|null $defaultName

The default command name

Tags
deprecated

since Symfony 6.1, use the AsCommand attribute instead

$aliases

private array<string|int, mixed> $aliases = []

$code

private Closure|null $code = null

$description

private string $description = ''

$hidden

private bool $hidden = false

$ignoreValidationErrors

private bool $ignoreValidationErrors = false

$name

private string|null $name = null

$processTitle

private string|null $processTitle = null

$synopsis

private array<string|int, mixed> $synopsis = []

$usages

private array<string|int, mixed> $usages = []

Methods

__construct()

public __construct([string|null $name = null ]) : mixed
Parameters
$name : string|null = null

The name of the command; passing null means it must be set in configure()

Tags
throws
LogicException

When the command name is empty

addArgument()

Adds an argument.

public addArgument(string $name[, mixed $mode = null ][, string $description = '' ][, mixed $default = null ]) : $this
Parameters
$name : string
$mode : mixed = null

The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL

$description : string = ''
$default : mixed = null

The default value (for InputArgument::OPTIONAL mode only)

Tags
throws
InvalidArgumentException

When argument mode is not valid

Return values
$this

addOption()

Adds an option.

public addOption(string $name[, mixed $shortcut = null ][, mixed $mode = null ][, string $description = '' ][, mixed $default = null ]) : $this
Parameters
$name : string
$shortcut : mixed = null

The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts

$mode : mixed = null

The option mode: One of the InputOption::VALUE_* constants

$description : string = ''
$default : mixed = null

The default value (must be null for InputOption::VALUE_NONE)

Tags
throws
InvalidArgumentException

If option mode is invalid or incompatible

Return values
$this

addUsage()

Add a command usage example, it'll be prefixed with the command name.

public addUsage(string $usage) : $this
Parameters
$usage : string
Return values
$this

getAliases()

Returns the aliases for the command.

public getAliases() : array<string|int, mixed>
Return values
array<string|int, mixed>

getDefaultDescription()

public static getDefaultDescription() : string|null
Return values
string|null

getDefaultName()

public static getDefaultName() : string|null
Return values
string|null

getDescription()

Returns the description for the command.

public getDescription() : string
Return values
string

getHelp()

Returns the help for the command.

public getHelp() : string
Return values
string

getName()

Returns the command name.

public getName() : string|null
Return values
string|null

getNativeDefinition()

Gets the InputDefinition to be used to create representations of this Command.

public getNativeDefinition() : InputDefinition

Can be overridden to provide the original command representation when it would otherwise be changed by merging with the application InputDefinition.

This method is not part of public API and should not be used directly.

Return values
InputDefinition

getProcessedHelp()

Returns the processed help for the command replacing the %command.name% and %command.full_name% patterns with the real values dynamically.

public getProcessedHelp() : string
Return values
string

getSynopsis()

Returns the synopsis for the command.

public getSynopsis([bool $short = false ]) : string
Parameters
$short : bool = false

Whether to show the short version of the synopsis (with options folded) or not

Return values
string

getUsages()

Returns alternative usages of the command.

public getUsages() : array<string|int, mixed>
Return values
array<string|int, mixed>

ignoreValidationErrors()

Ignores validation errors.

public ignoreValidationErrors() : void

This is mainly useful for the help command.

isEnabled()

Checks whether the command is enabled or not in the current environment.

public isEnabled() : bool

Override this to check for x or y and return false if the command cannot run properly under the current conditions.

Return values
bool

isHidden()

public isHidden() : bool
Return values
bool

whether the command should be publicly shown or not

run()

Runs the command.

public run(InputInterface $input, OutputInterface $output) : int

The code to execute is either defined directly with the setCode() method or by overriding the execute() method in a sub-class.

Parameters
$input : InputInterface
$output : OutputInterface
Tags
throws
ExceptionInterface

When input binding fails. Bypass this by calling .

see
setCode()
see
execute()
Return values
int

The command exit code

setAliases()

Sets the aliases for the command.

public setAliases(array<string|int, string> $aliases) : $this
Parameters
$aliases : array<string|int, string>

An array of aliases for the command

Tags
throws
InvalidArgumentException

When an alias is invalid

Return values
$this

setCode()

Sets the code to execute when running this command.

public setCode(callable $code) : $this

If this method is used, it overrides the code defined in the execute() method.

Parameters
$code : callable

A callable(InputInterface $input, OutputInterface $output)

Tags
throws
InvalidArgumentException
see
execute()
Return values
$this

setDefinition()

Sets an array of argument and option instances.

public setDefinition(array<string|int, mixed>|InputDefinition $definition) : $this
Parameters
$definition : array<string|int, mixed>|InputDefinition
Return values
$this

setDescription()

Sets the description for the command.

public setDescription(string $description) : $this
Parameters
$description : string
Return values
$this

setHelp()

Sets the help for the command.

public setHelp(string $help) : $this
Parameters
$help : string
Return values
$this

setHidden()

public setHidden([bool $hidden = true ]) : $this
Parameters
$hidden : bool = true

Whether or not the command should be hidden from the list of commands

Return values
$this

setName()

Sets the name of the command.

public setName(string $name) : $this

This method can set both the namespace and the name if you separate them by a colon (:)

$command->setName('foo:bar');
Parameters
$name : string
Tags
throws
InvalidArgumentException

When the name is invalid

Return values
$this

setProcessTitle()

Sets the process title of the command.

public setProcessTitle(string $title) : $this

This feature should be used only when creating a long process command, like a daemon.

Parameters
$title : string
Return values
$this

configure()

Configures the current command.

protected configure() : void

execute()

Executes the current command.

protected execute(InputInterface $input, OutputInterface $output) : int

This method is not abstract because you can use this class as a concrete class. In this case, instead of defining the execute() method, you set the code to execute by passing a Closure to the setCode() method.

Parameters
$input : InputInterface
$output : OutputInterface
Tags
throws
LogicException

When this abstract method is not implemented

see
setCode()
Return values
int

0 if everything went fine, or an exit code

interact()

Interacts with the user.

protected interact(InputInterface $input, OutputInterface $output) : void

This method is executed before the InputDefinition is validated. This means that this is the only place where the command can interactively ask for values of missing required arguments.

Parameters
$input : InputInterface
$output : OutputInterface

validateName()

Validates a command name.

private validateName(string $name) : void

It must be non-empty and parts can optionally be separated by ":".

Parameters
$name : string
Tags
throws
InvalidArgumentException

When the name is invalid


        
On this page

Search results