ListCommand.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Command;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Output\Output;
  16. use Symfony\Component\Console\Command\Command;
  17. use Symfony\Component\Console\Input\InputDefinition;
  18. /**
  19. * ListCommand displays the list of all available commands for the application.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. class ListCommand extends Command
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function configure()
  29. {
  30. $this
  31. ->setDefinition($this->createDefinition())
  32. ->setName('list')
  33. ->setDescription('Lists commands')
  34. ->setHelp(<<<EOF
  35. The <info>list</info> command lists all commands:
  36. <info>php app/console list</info>
  37. You can also display the commands for a specific namespace:
  38. <info>php app/console list test</info>
  39. You can also output the information as XML by using the <comment>--xml</comment> option:
  40. <info>php app/console list --xml</info>
  41. EOF
  42. );
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function getNativeDefinition()
  48. {
  49. return $this->createDefinition();
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. protected function execute(InputInterface $input, OutputInterface $output)
  55. {
  56. if ($input->getOption('xml')) {
  57. $output->writeln($this->getApplication()->asXml($input->getArgument('namespace')), OutputInterface::OUTPUT_RAW);
  58. } else {
  59. $output->writeln($this->getApplication()->asText($input->getArgument('namespace')));
  60. }
  61. }
  62. private function createDefinition()
  63. {
  64. return new InputDefinition(array(
  65. new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'),
  66. new InputOption('xml', null, InputOption::VALUE_NONE, 'To output help as XML'),
  67. ));
  68. }
  69. }