ListCommand.php 2.2 KB

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