HelpCommand.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * HelpCommand displays the help for a given command.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. */
  24. class HelpCommand extends Command
  25. {
  26. private $command;
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function configure()
  31. {
  32. $this->ignoreValidationErrors();
  33. $this
  34. ->setName('help')
  35. ->setDefinition([
  36. new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help'),
  37. new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
  38. new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command help'),
  39. ])
  40. ->setDescription('Display help for a command')
  41. ->setHelp(<<<'EOF'
  42. The <info>%command.name%</info> command displays help for a given command:
  43. <info>%command.full_name% list</info>
  44. You can also output the help in other formats by using the <comment>--format</comment> option:
  45. <info>%command.full_name% --format=xml list</info>
  46. To display the list of available commands, please use the <info>list</info> command.
  47. EOF
  48. )
  49. ;
  50. }
  51. public function setCommand(Command $command)
  52. {
  53. $this->command = $command;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. protected function execute(InputInterface $input, OutputInterface $output)
  59. {
  60. if (null === $this->command) {
  61. $this->command = $this->getApplication()->find($input->getArgument('command_name'));
  62. }
  63. $helper = new DescriptorHelper();
  64. $helper->describe($output, $this->command, [
  65. 'format' => $input->getOption('format'),
  66. 'raw_text' => $input->getOption('raw'),
  67. ]);
  68. $this->command = null;
  69. return 0;
  70. }
  71. public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
  72. {
  73. if ($input->mustSuggestArgumentValuesFor('command_name')) {
  74. $descriptor = new ApplicationDescription($this->getApplication());
  75. $suggestions->suggestValues(array_keys($descriptor->getCommands()));
  76. return;
  77. }
  78. if ($input->mustSuggestOptionValuesFor('format')) {
  79. $helper = new DescriptorHelper();
  80. $suggestions->suggestValues($helper->getFormats());
  81. }
  82. }
  83. }