symfony/symfony — pull request #4104 by brikou (2012-04-25 09:11:05+00:00)
Title: [Console] command's name required when added to an application

- Bug fix: no
- Feature addition: yes
- Backwards compatibility break: no
- Symfony2 tests pass: yes
- Fixes the following tickets: #3857
- Todo: -

[![Build Status](https://secure.travis-ci.org/brikou/symfony.png?branch=console_simple)](http://travis-ci.org/brikou/symfony)

---

Here is an application with a single purpose/command : listing a directory [as seen here](http://fabien.potencier.org/article/49/what-is-symfony2)

``` php

require_once __DIR__.'/../../../../../vendor/autoload.php';

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

$console = new Application();
$console
->register('ls')
->setDefinition(array(
new InputArgument('dir', InputArgument::REQUIRED, 'Directory name'),
))
->setDescription('Displays the files in the given directory')
->setCode(function (InputInterface $input, OutputInterface $output) {
$dir = $input->getArgument('dir');

$output->writeln(sprintf('Dir listing for %s', $dir));
})
;
$console->run();

```

This simple command can be refactored into this...

``` php

require_once __DIR__.'/../../../../../vendor/autoload.php';

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\ConsoleOutput;

$command = new Command("ANameAsItIsMandatory");
$command
->setDefinition(array(
new InputArgument('dir', InputArgument::REQUIRED, 'Directory name'),
))
->setDescription('Displays the files in the given directory')
->setCode(function (InputInterface $input, OutputInterface $output) {
$dir = $input->getArgument('dir');

$output->writeln(sprintf('Dir listing for %s', $dir));
})
;

$command->run(new ArgvInput(), new ConsoleOutput());
```

The conceptual problem is that command's name is required even if not needed/used.
This PR makes the **name required only when this command is attached to an application**.