There are two different ways of creating a command configuration:
* Call {@link create()} or {@link 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 {@link 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 {@link 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
{@link getHandler()}. Since the command handler is separated, it can be
easily tested:
php
class ServerConfig extends CommandConfig
{
public function getHandler()
{
return new ServerHandler();
}
}