Overview

Namespaces

  • Webmozart
    • Console
      • Adapter
      • Api
        • Application
        • Args
          • Format
        • Command
        • Config
        • Event
        • Formatter
        • IO
        • Resolver
      • Args
      • Config
      • Formatter
      • Handler
        • Help
      • IO
        • InputStream
        • OutputStream
      • Process
      • Resolver
      • UI
        • Alignment
        • Component
        • Help
        • Layout
        • Style
      • Util

Classes

  • ApplicationConfig
  • CommandConfig
  • Config
  • OptionCommandConfig
  • SubCommandConfig
  • Overview
  • Namespace
  • Class

Class CommandConfig

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(); } }

Webmozart\Console\Api\Config\Config
Extended by Webmozart\Console\Api\Config\CommandConfig

Direct known subclasses

Webmozart\Console\Api\Config\SubCommandConfig

Indirect known subclasses

Webmozart\Console\Api\Config\OptionCommandConfig
Namespace: Webmozart\Console\Api\Config
Author: Bernhard Schussek bschussek@gmail.com
Since: 1.0
Located at Api/Config/CommandConfig.php
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
# __construct( 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.

Overrides

Webmozart\Console\Api\Config\Config::__construct()
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

Webmozart\Console\Api\Config\ApplicationConfig
The application configuration.
public
# setApplicationConfig( Webmozart\Console\Api\Config\ApplicationConfig $applicationConfig )

Sets the application configuration.

Sets the application configuration.

Parameters

$applicationConfig
The application configuration.
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') // ... ->end()
 // ...
;

Returns

Webmozart\Console\Api\Config\ApplicationConfig
The application configuration.
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

Webmozart\Console\Api\Config\CommandConfig::addAlias(), Webmozart\Console\Api\Config\CommandConfig::setAliases()
public static
# addAlias( string $alias )

Adds an alias name.

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

Webmozart\Console\Api\Config\CommandConfig::addAliases(), Webmozart\Console\Api\Config\CommandConfig::setAliases(), getAlias()
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

Webmozart\Console\Api\Config\CommandConfig::addAlias(), Webmozart\Console\Api\Config\CommandConfig::setAliases(), getAlias()
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

Webmozart\Console\Api\Config\CommandConfig::addAlias(), Webmozart\Console\Api\Config\CommandConfig::addAliases(), getAlias()
public string
# getDescription( )

Returns the description of the command.

Returns the description of the command.

Returns

string
The description of the command.

See

Webmozart\Console\Api\Config\CommandConfig::setDescription()
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

Webmozart\Console\Api\Config\CommandConfig::getDescription()
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

Webmozart\Console\Api\Config\CommandConfig::setHelp()
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

Webmozart\Console\Api\Config\CommandConfig::getHelp()
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

Webmozart\Console\Api\Config\CommandConfig::enable(), Webmozart\Console\Api\Config\CommandConfig::disable(), Webmozart\Console\Api\Config\CommandConfig::enableIf(), Webmozart\Console\Api\Config\CommandConfig::disableIf()
public static
# enable( )

Enables the command.

Enables the command.

Returns

static
The current instance.

See

Webmozart\Console\Api\Config\CommandConfig::enableIf(), Webmozart\Console\Api\Config\CommandConfig::disable(), Webmozart\Console\Api\Config\CommandConfig::isEnabled()
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

Webmozart\Console\Api\Config\CommandConfig::enable(), Webmozart\Console\Api\Config\CommandConfig::disable(), Webmozart\Console\Api\Config\CommandConfig::isEnabled()
public static
# disable( )

Disables the command.

Disables the command.

Returns

static
The current instance.

See

Webmozart\Console\Api\Config\CommandConfig::disableIf(), Webmozart\Console\Api\Config\CommandConfig::enable(), Webmozart\Console\Api\Config\CommandConfig::isEnabled()
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

Webmozart\Console\Api\Config\CommandConfig::disable(), Webmozart\Console\Api\Config\CommandConfig::enable(), Webmozart\Console\Api\Config\CommandConfig::isEnabled()
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

Webmozart\Console\Api\Config\CommandConfig::setProcessTitle()
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

Webmozart\Console\Api\Config\CommandConfig::getProcessTitle()
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

Webmozart\Console\Api\Config\CommandConfig::markAnonymous(), Webmozart\Console\Api\Config\CommandConfig::markNoDefault()
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

Webmozart\Console\Api\Config\CommandConfig::markDefault(), Webmozart\Console\Api\Config\CommandConfig::markNoDefault()
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

Webmozart\Console\Api\Config\CommandConfig::markDefault(), Webmozart\Console\Api\Config\CommandConfig::markAnonymous()
public boolean
# isDefault( )

Returns whether the command is a default command.

Returns whether the command is a default command.

Returns

boolean

Returns true if either Webmozart\Console\Api\Config\CommandConfig::markDefault() or Webmozart\Console\Api\Config\CommandConfig::markAnonymous() was called and false otherwise.

public boolean
# isAnonymous( )

Returns whether the command is anonymous.

Returns whether the command is anonymous.

Returns

boolean

Returns true if Webmozart\Console\Api\Config\CommandConfig::markAnonymous() was called and false otherwise.

public Webmozart\Console\Api\Args\Format\ArgsFormat
# buildArgsFormat( Webmozart\Console\Api\Args\Format\ArgsFormat $baseFormat = null )

Builds an Webmozart\Console\Api\Args\Format\ArgsFormat instance with the given base format.

Builds an Webmozart\Console\Api\Args\Format\ArgsFormat instance with the given base format.

Parameters

$baseFormat
The base format.

Returns

Webmozart\Console\Api\Args\Format\ArgsFormat
The built format for the console arguments.
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

Webmozart\Console\Api\Config\SubCommandConfig
The sub-command configuration.

See

Webmozart\Console\Api\Config\CommandConfig::editSubCommand()
public Webmozart\Console\Api\Config\SubCommandConfig
# editSubCommand( string $name )

Alias for Webmozart\Console\Api\Config\CommandConfig::getSubCommandConfig().

Alias for Webmozart\Console\Api\Config\CommandConfig::getSubCommandConfig().

This method can be used to nicely edit a sub-command inherited from a parent configuration using the fluent API:

protected function configure()
{ parent::configure();
 $this ->editCommand('server') ->editSubCommand('add') // ... ->end() ->end()
 // ... ;
}

Parameters

$name
The name of the sub-command to edit.

Returns

Webmozart\Console\Api\Config\SubCommandConfig
The sub-command configuration.

See

Webmozart\Console\Api\Config\CommandConfig::beginSubCommand()
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

Webmozart\Console\Api\Config\OptionCommandConfig
The option command configuration.

See

Webmozart\Console\Api\Config\CommandConfig::editOptionCommand()
public Webmozart\Console\Api\Config\OptionCommandConfig
# editOptionCommand( string $name )

Alias for Webmozart\Console\Api\Config\CommandConfig::getSubCommandConfig().

Alias for Webmozart\Console\Api\Config\CommandConfig::getSubCommandConfig().

This method can be used to nicely edit an option command inherited from a parent configuration using the fluent API:

protected function configure()
{ parent::configure();
 $this ->editCommand('server') ->editOptionCommand('add') // ... ->end() ->end()
 // ... ;
}

Parameters

$name
The name of the option command to edit.

Returns

Webmozart\Console\Api\Config\OptionCommandConfig
The option command configuration.

See

Webmozart\Console\Api\Config\CommandConfig::beginOptionCommand()
public static
# addSubCommandConfig( Webmozart\Console\Api\Config\SubCommandConfig $config )

Adds configuration for a sub-command.

Adds configuration for a sub-command.

Parameters

$config
The sub-command configuration.

Returns

static
The current instance.

See

Webmozart\Console\Api\Config\CommandConfig::beginSubCommand()
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

Webmozart\Console\Api\Config\CommandConfig::beginSubCommand()
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

Webmozart\Console\Api\Config\CommandConfig::beginSubCommand()
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

Webmozart\Console\Api\Config\SubCommandConfig
The sub-command configuration.

Throws

Webmozart\Console\Api\Command\NoSuchCommandException

If the sub-command configuration is not found.

See

Webmozart\Console\Api\Config\CommandConfig::beginSubCommand()
public Webmozart\Console\Api\Config\SubCommandConfig[]
# getSubCommandConfigs( )

Returns the configurations of all sub-commands.

Returns the configurations of all sub-commands.

Returns

Webmozart\Console\Api\Config\SubCommandConfig[]
The sub-command configurations.

See

Webmozart\Console\Api\Config\CommandConfig::beginSubCommand()
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

Webmozart\Console\Api\Config\CommandConfig::beginSubCommand()
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

Webmozart\Console\Api\Config\CommandConfig::beginSubCommand()
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

Webmozart\Console\Api\Config\Config::getDefaultHelperSet()
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

Webmozart\Console\Api\Config\Config::getDefaultHandler()
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

Webmozart\Console\Api\Config\Config::getDefaultHandlerMethod()
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

Webmozart\Console\Api\Config\Config::getDefaultArgsParser()
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

Webmozart\Console\Api\Config\Config::getDefaultLenientArgsParsing()
Methods inherited from Webmozart\Console\Api\Config\Config
addArgument(), addOption(), configure(), disableLenientArgsParsing(), enableLenientArgsParsing(), getArgsParser(), getArguments(), getHandler(), getHandlerMethod(), getHelperSet(), getOptions(), isLenientArgsParsingEnabled(), setArgsParser(), setHandler(), setHandlerMethod(), setHelperSet()
Webmozart Console API API documentation generated by ApiGen