HelpCommand.php 2.3 KB

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