HelpCommand.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\InputOption;
  14. use Symfony\Component\Console\Input\InputInterface;
  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(array(
  33. new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help'),
  34. new InputOption('xml', null, InputOption::VALUE_NONE, 'To output help as XML'),
  35. new InputOption('format', null, InputOption::VALUE_REQUIRED, 'To output help in other formats'),
  36. new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command help'),
  37. ))
  38. ->setDescription('Displays help for a command')
  39. ->setHelp(<<<EOF
  40. The <info>%command.name%</info> command displays help for a given command:
  41. <info>php %command.full_name% list</info>
  42. You can also output the help in other formats by using the <comment>--format</comment> option:
  43. <info>php %command.full_name% --format=xml list</info>
  44. To display the list of available commands, please use the <info>list</info> command.
  45. EOF
  46. )
  47. ;
  48. }
  49. /**
  50. * Sets the command
  51. *
  52. * @param Command $command The command to set
  53. */
  54. public function setCommand(Command $command)
  55. {
  56. $this->command = $command;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function execute(InputInterface $input, OutputInterface $output)
  62. {
  63. if (null === $this->command) {
  64. $this->command = $this->getApplication()->find($input->getArgument('command_name'));
  65. }
  66. if ($input->getOption('xml')) {
  67. $input->setOption('format', 'xml');
  68. }
  69. $helper = new DescriptorHelper();
  70. $helper->describe($output, $this->command, $input->getOption('format'), $input->getOption('raw'));
  71. $this->command = null;
  72. }
  73. }