ListCommand.php 3.1 KB

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