HelpCommand.php 2.8 KB

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