The configuration of a console command.
There are two different ways of creating a command configuration:
* Call Webmozart\Console\Api\Config\CommandConfig::create()
or ApplicationConfig::beginCommand() and use the fluent interface:
php $config = CommandConfig::create() ->setName('server') ->setDescription('List and manage servers')
->beginSubCommand('add') ->setDescription('Add a new server') ->addArgument('host', Argument::REQUIRED) ->addOption('port', 'p', Option::VALUE_OPTIONAL, null, 80) ->end()
// ... ;
* Extend the class and implement the Webmozart\Console\Api\Config\Config::configure()
method:
php class ServerCommandConfig extends CommandConfig { protected function configure() { $this ->setName('server') ->setDescription('List and manage servers')
->beginSubCommand('add') ->setDescription('Add a new server') ->addArgument('host', Argument::REQUIRED) ->addOption('port', 'p', Option::VALUE_OPTIONAL, null, 80) ->end()
// ... ; } }
You can choose between two different ways of executing a command:
* You can register a callback with setCallback(). The callback receives the input, the standard output and the error output as arguments:
php $config->setCallback( function (InputInterface $input, OutputInterface $output, OutputInterface $errorOutput) { // ... } );
* You can implement a custom command handler and return the handler from Webmozart\Console\Api\Config\Config::getHandler()
. Since the command handler is separated, it can be easily tested:
php class ServerConfig extends CommandConfig { public function getHandler() { return new ServerHandler(); } }
Methods summary
public static
static
|
#
create( string $name = null, Webmozart\Console\Api\Config\ApplicationConfig $applicationConfig = null )
Creates a new configuration.
Creates a new configuration.
Parameters
- $name
- The name of the command.
- $applicationConfig
- The application configuration.
Returns
static The created configuration.
|
public
|
|
public
string
|
#
getName( )
Returns the name of the command.
Returns the name of the command.
Returns
string The name of the command.
|
public
static
|
#
setName( string $name )
Sets the name of the command.
Sets the name of the command.
Parameters
- $name
- The name of the command.
Returns
static The current instance.
|
public
Webmozart\Console\Api\Config\ApplicationConfig
|
#
getApplicationConfig( )
Returns the application configuration.
Returns the application configuration.
Returns
|
public
|
|
public
Webmozart\Console\Api\Config\ApplicationConfig
|
#
end( )
Ends the block when dynamically configuring a command configuration.
Ends the block when dynamically configuring a command configuration.
This method is usually used together with
ApplicationConfig::beginCommand():
$config ->beginCommand('command')
;
Returns
|
public
string[]
|
#
getAliases( )
Returns the alias names of the command.
Returns the alias names of the command.
Returns
string[] An array of alias names of the command.
See
|
public
static
|
#
addAlias( string $alias )
Adds an alias name.
An alias is an alternative name that can be used when calling the
command. Aliases are a useful way for migrating a command from one name
to another.
Existing alias names are preserved.
Parameters
- $alias
- The alias name to add.
Returns
static The current instance.
See
|
public
static
|
#
addAliases( array $aliases )
Adds a list of alias names.
Adds a list of alias names.
Existing alias names are preserved.
Parameters
- $aliases
- The alias names to add.
Returns
static The current instance.
See
|
public
static
|
#
setAliases( array $aliases )
Sets the alias names of the command.
Sets the alias names of the command.
Existing alias names are replaced.
Parameters
- $aliases
- The alias names.
Returns
static The current instance.
See
|
public
string
|
#
getDescription( )
Returns the description of the command.
Returns the description of the command.
Returns
string The description of the command.
See
|
public
static
|
#
setDescription( string $description )
Sets the description of the command.
Sets the description of the command.
The description is a short one-liner that describes the command in the
command listing. The description should be written in imperative form
rather than in descriptive form. So:
List the contents of a directory.
should be preferred over
Lists the contents of a directory.
Parameters
- $description
- The description.
Returns
static The current instance.
See
|
public
string
|
#
getHelp( )
Returns the help text of the command.
Returns the help text of the command.
The help text provides additional information about a command that is
displayed in the help view.
Returns
string The help text of the command.
See
|
public
static
|
#
setHelp( string $help )
Sets the help text of the command.
Sets the help text of the command.
The help text provides additional information about a command that is
displayed in the help view.
Parameters
- $help
- The help text of the command.
Returns
static The current instance.
See
|
public
boolean
|
#
isEnabled( )
Returns whether the command is enabled or not in the current environment.
Returns whether the command is enabled or not in the current environment.
Returns
boolean Returns true if the command is currently enabled and
false otherwise.
See
|
public
static
|
#
enable( )
Enables the command.
Returns
static The current instance.
See
|
public
static
|
#
enableIf( boolean $condition )
Enables the command if a condition holds and disables it otherwise.
Enables the command if a condition holds and disables it otherwise.
Parameters
- $condition
- The condition under which to enable the command.
Returns
static The current instance.
See
|
public
static
|
#
disable( )
Disables the command.
Returns
static The current instance.
See
|
public
static
|
#
disableIf( boolean $condition )
Disables the command if a condition holds and enables it otherwise.
Disables the command if a condition holds and enables it otherwise.
Parameters
- $condition
- The condition under which to disable the command.
Returns
static The current instance.
See
|
public
string|null
|
#
getProcessTitle( )
Returns the title of the command process.
Returns the title of the command process.
Returns
string|null The process title or null if no title should be
set.
See
|
public
static
|
#
setProcessTitle( string|null $processTitle )
Sets the title of the command process.
Sets the title of the command process.
Parameters
- $processTitle
The process title or null if no title
should be set.
Returns
static The current instance.
See
|
public
static
|
#
markDefault( )
Marks the command as default command.
Marks the command as default command.
The names of default commands can be omitted when calling the command.
For example, the following command can be called in two ways:
protected function configure()
{ $this ->beginCommand('add') ->markDefault() ->addArgument('host', Argument::REQUIRED) ->end()
}
The first way is to call the command regularly. The second way is to
omit the name of the command:
$ ./console add localhost
$ ./console localhost
Returns
static The current instance.
See
|
public
static
|
#
markAnonymous( )
Marks the command as anonymous command.
Marks the command as anonymous command.
Anonymous commands cannot be called by name:
protected function configure()
{ $this ->beginCommand('add') ->markAnonymous() ->addArgument('host', Argument::REQUIRED) ->end()
}
The name "add" is given to the command only to access the command later
on. Since the command is anonymous, the name cannot be passed when
when calling the command:
$ ./console add localhost
Instead, the command should be called without name:
$ ./console localhost
Returns
static The current instance.
See
|
public
static
|
#
markNoDefault( )
Marks the command as neither anonymous nor default.
Marks the command as neither anonymous nor default.
Returns
static The current instance.
See
|
public
boolean
|
#
isDefault( )
Returns whether the command is a default command.
Returns whether the command is a default command.
Returns
|
public
boolean
|
#
isAnonymous( )
Returns whether the command is anonymous.
Returns whether the command is anonymous.
Returns
|
public
Webmozart\Console\Api\Args\Format\ArgsFormat
|
|
public
Webmozart\Console\Api\Config\SubCommandConfig
|
#
beginSubCommand( string $name )
Starts a configuration block for a sub-command.
Starts a configuration block for a sub-command.
A sub-command is executed if the name of the command is passed after the
name of the containing command. For example, if the command "server" has
a sub-command command named "add", that command can be called with:
$ console server add ...
The configuration of the sub-command is returned by this method. You can
use the fluent interface to configure the sub-command before jumping back
to this configuration with SubCommandConfig::end():
protected function configure()
{ $this ->beginCommand('server') ->setDescription('List and manage servers')
->beginSubCommand('add') ->setDescription('Add a server') ->addArgument('host', Argument::REQUIRED) ->addOption('port', 'p', Option::VALUE_OPTIONAL, null, 80) ->end() ->end()
}
Parameters
- $name
- The name of the sub-command.
Returns
See
|
public
Webmozart\Console\Api\Config\SubCommandConfig
|
|
public
Webmozart\Console\Api\Config\OptionCommandConfig
|
#
beginOptionCommand( string $name, string $shortName = null )
Starts a configuration block for an option command.
Starts a configuration block for an option command.
An option command is executed if the corresponding option is passed after
the command name. For example, if the command "server" has an option
command named "--add" with the short name "-a", that command can be
called with:
$ console server --add ...
$ console server -a ...
The configuration of the option command is returned by this method.
You can use the fluent interface to configure the option command
before jumping back to this configuration with
SubCommandConfig::end():
protected function configure()
{ $this ->beginCommand('server') ->setDescription('List and manage servers')
->beginOptionCommand('add', 'a') ->setDescription('Add a server') ->addArgument('host', Argument::REQUIRED) ->addOption('port', 'p', Option::VALUE_OPTIONAL, null, 80) ->end() ->end()
}
Parameters
- $name
- The name of the option command.
- $shortName
- The short name of the option command.
Returns
See
|
public
Webmozart\Console\Api\Config\OptionCommandConfig
|
|
public
static
|
|
public
static
|
#
addSubCommandConfigs( array $configs )
Adds sub-command configurations to the command.
Adds sub-command configurations to the command.
Parameters
- $configs
- The sub-command configurations.
Returns
static The current instance.
See
|
public
static
|
#
setSubCommandConfigs( array $configs )
Sets the sub-command configurations of the command.
Sets the sub-command configurations of the command.
Parameters
- $configs
- The sub-command configurations.
Returns
static The current instance.
See
|
public
Webmozart\Console\Api\Config\SubCommandConfig
|
#
getSubCommandConfig( string $name )
Returns the sub-command configuration for a given name.
Returns the sub-command configuration for a given name.
Parameters
- $name
- The name of the sub-command.
Returns
Throws
See
|
public
Webmozart\Console\Api\Config\SubCommandConfig[]
|
#
getSubCommandConfigs( )
Returns the configurations of all sub-commands.
Returns the configurations of all sub-commands.
Returns
See
|
public
boolean
|
#
hasSubCommandConfig( string $name )
Returns whether the command has a sub-command with a given name.
Returns whether the command has a sub-command with a given name.
Parameters
- $name
- The name of the sub-command.
Returns
boolean Returns true if the sub-command configuration with the
given name exists and false otherwise.
See
|
public
boolean
|
#
hasSubCommandConfigs( )
Returns whether the command has any registered sub-command configurations.
Returns whether the command has any registered sub-command configurations.
Returns
boolean Returns true if sub-command configurations were added to
the command and false otherwise.
See
|
protected
HelperSet
|
#
getDefaultHelperSet( )
Returns the helper set to use if none is set.
Returns the helper set to use if none is set.
Returns
HelperSet The default helper set.
Overrides
|
protected
object
|
#
getDefaultHandler( )
Returns the command handler to use if none is set.
Returns the command handler to use if none is set.
Returns
object The default command handler.
Overrides
|
protected
string
|
#
getDefaultHandlerMethod( )
Returns the handler method to use if none is set.
Returns the handler method to use if none is set.
Returns
string The default handler method.
Overrides
|
protected
ArgsParser
|
#
getDefaultArgsParser( )
Returns the arguments parser to use if none is set.
Returns the arguments parser to use if none is set.
Returns
ArgsParser The default args parser.
Overrides
|
protected
boolean
|
#
getDefaultLenientArgsParsing( )
Returns whether the arguments parsing handles errors gracefully.
Returns whether the arguments parsing handles errors gracefully.
Returns
boolean The default value for lenient args parsing.
Overrides
|