HelpCommand.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Symfony\Component\Console\Command;
  3. use Symfony\Component\Console\Input\InputArgument;
  4. use Symfony\Component\Console\Input\InputOption;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Symfony\Component\Console\Output\Output;
  8. use Symfony\Component\Console\Command\Command;
  9. /*
  10. * This file is part of the Symfony framework.
  11. *
  12. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  13. *
  14. * This source file is subject to the MIT license that is bundled
  15. * with this source code in the file LICENSE.
  16. */
  17. /**
  18. * HelpCommand displays the help for a given command.
  19. *
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. */
  22. class HelpCommand extends Command
  23. {
  24. protected $command;
  25. /**
  26. * @see Command
  27. */
  28. protected function configure()
  29. {
  30. $this->ignoreValidationErrors = true;
  31. $this
  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. ))
  36. ->setName('help')
  37. ->setAliases(array('?'))
  38. ->setDescription('Displays help for a command')
  39. ->setHelp(<<<EOF
  40. The <info>help</info> command displays help for a given command:
  41. <info>./symfony help list</info>
  42. You can also output the help as XML by using the <comment>--xml</comment> option:
  43. <info>./symfony help --xml list</info>
  44. EOF
  45. );
  46. }
  47. public function setCommand(Command $command)
  48. {
  49. $this->command = $command;
  50. }
  51. /**
  52. * @see Command
  53. */
  54. protected function execute(InputInterface $input, OutputInterface $output)
  55. {
  56. if (null === $this->command) {
  57. $this->command = $this->application->get($input->getArgument('command_name'));
  58. }
  59. if ($input->getOption('xml')) {
  60. $output->writeln($this->command->asXml(), Output::OUTPUT_RAW);
  61. } else {
  62. $output->writeln($this->command->asText());
  63. }
  64. }
  65. }