ListCommand.php 2.5 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\Descriptor\ApplicationDescription;
  12. use Symfony\Component\Console\Helper\DescriptorHelper;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  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. * @return void
  26. */
  27. protected function configure()
  28. {
  29. $this
  30. ->setName('list')
  31. ->setDefinition([
  32. new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name', null, fn () => array_keys((new ApplicationDescription($this->getApplication()))->getNamespaces())),
  33. new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
  34. new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt', fn () => (new DescriptorHelper())->getFormats()),
  35. new InputOption('short', null, InputOption::VALUE_NONE, 'To skip describing commands\' arguments'),
  36. ])
  37. ->setDescription('List commands')
  38. ->setHelp(<<<'EOF'
  39. The <info>%command.name%</info> command lists all commands:
  40. <info>%command.full_name%</info>
  41. You can also display the commands for a specific namespace:
  42. <info>%command.full_name% test</info>
  43. You can also output the information in other formats by using the <comment>--format</comment> option:
  44. <info>%command.full_name% --format=xml</info>
  45. It's also possible to get raw list of commands (useful for embedding command runner):
  46. <info>%command.full_name% --raw</info>
  47. EOF
  48. )
  49. ;
  50. }
  51. protected function execute(InputInterface $input, OutputInterface $output): int
  52. {
  53. $helper = new DescriptorHelper();
  54. $helper->describe($output, $this->getApplication(), [
  55. 'format' => $input->getOption('format'),
  56. 'raw_text' => $input->getOption('raw'),
  57. 'namespace' => $input->getArgument('namespace'),
  58. 'short' => $input->getOption('short'),
  59. ]);
  60. return 0;
  61. }
  62. }