ListCommand.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\Helper\DescriptorHelper;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Input\InputDefinition;
  17. /**
  18. * ListCommand displays the list of all available commands for the application.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class ListCommand extends Command
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function configure()
  28. {
  29. $this
  30. ->setName('list')
  31. ->setDefinition($this->createDefinition())
  32. ->setDescription('Lists commands')
  33. ->setHelp(<<<'EOF'
  34. The <info>%command.name%</info> command lists all commands:
  35. <info>php %command.full_name%</info>
  36. You can also display the commands for a specific namespace:
  37. <info>php %command.full_name% test</info>
  38. You can also output the information in other formats by using the <comment>--format</comment> option:
  39. <info>php %command.full_name% --format=xml</info>
  40. It's also possible to get raw list of commands (useful for embedding command runner):
  41. <info>php %command.full_name% --raw</info>
  42. EOF
  43. )
  44. ;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getNativeDefinition()
  50. {
  51. return $this->createDefinition();
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. protected function execute(InputInterface $input, OutputInterface $output)
  57. {
  58. $helper = new DescriptorHelper();
  59. $helper->describe($output, $this->getApplication(), array(
  60. 'format' => $input->getOption('format'),
  61. 'raw_text' => $input->getOption('raw'),
  62. 'namespace' => $input->getArgument('namespace'),
  63. ));
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. private function createDefinition()
  69. {
  70. return new InputDefinition(array(
  71. new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'),
  72. new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
  73. new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
  74. ));
  75. }
  76. }