ListCommand.php 2.5 KB

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