HelpCommand.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Output\Output;
  16. use Symfony\Component\Console\Command\Command;
  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. private $command;
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function configure()
  29. {
  30. $this->ignoreValidationErrors();
  31. $this
  32. ->setName('help')
  33. ->setDefinition(array(
  34. new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help'),
  35. new InputOption('xml', null, InputOption::VALUE_NONE, 'To output help as XML'),
  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 as XML by using the <comment>--xml</comment> option:
  42. <info>php %command.full_name% --xml list</info>
  43. EOF
  44. )
  45. ;
  46. }
  47. /**
  48. * Sets the command
  49. *
  50. * @param Command $command The command to set
  51. */
  52. public function setCommand(Command $command)
  53. {
  54. $this->command = $command;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. protected function execute(InputInterface $input, OutputInterface $output)
  60. {
  61. if (null === $this->command) {
  62. $this->command = $this->getApplication()->get($input->getArgument('command_name'));
  63. }
  64. if ($input->getOption('xml')) {
  65. $output->writeln($this->command->asXml(), OutputInterface::OUTPUT_RAW);
  66. } else {
  67. $output->writeln($this->command->asText());
  68. }
  69. $this->command = null;
  70. }
  71. }