ListCommand.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. ->setName('list')
  32. ->setDefinition($this->createDefinition())
  33. ->setDescription('Lists commands')
  34. ->setHelp(<<<EOF
  35. The <info>%command.name%</info> command lists all commands:
  36. <info>php %command.full_name%</info>
  37. You can also display the commands for a specific namespace:
  38. <info>php %command.full_name% test</info>
  39. You can also output the information as XML by using the <comment>--xml</comment> option:
  40. <info>php %command.full_name% --xml</info>
  41. It's also possible to get raw list of commands (useful for embedding command runner):
  42. <info>php %command.full_name% --raw</info>
  43. EOF
  44. )
  45. ;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function getNativeDefinition()
  51. {
  52. return $this->createDefinition();
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. protected function execute(InputInterface $input, OutputInterface $output)
  58. {
  59. if ($input->getOption('xml')) {
  60. $output->writeln($this->getApplication()->asXml($input->getArgument('namespace')), OutputInterface::OUTPUT_RAW);
  61. } else {
  62. $output->writeln($this->getApplication()->asText($input->getArgument('namespace'), $input->getOption('raw')));
  63. }
  64. }
  65. private function createDefinition()
  66. {
  67. return new InputDefinition(array(
  68. new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'),
  69. new InputOption('xml', null, InputOption::VALUE_NONE, 'To output help as XML'),
  70. new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
  71. ));
  72. }
  73. }