HelpCommand.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Symfony\Components\Console\Command;
  3. use Symfony\Components\Console\Input\InputArgument;
  4. use Symfony\Components\Console\Input\InputOption;
  5. use Symfony\Components\Console\Input\InputInterface;
  6. use Symfony\Components\Console\Output\OutputInterface;
  7. use Symfony\Components\Console\Output\Output;
  8. use Symfony\Components\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. * @package Symfony
  21. * @subpackage Components_Console
  22. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  23. */
  24. class HelpCommand extends Command
  25. {
  26. protected $command;
  27. /**
  28. * @see Command
  29. */
  30. protected function configure()
  31. {
  32. $this->ignoreValidationErrors = true;
  33. $this
  34. ->setDefinition(array(
  35. new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help'),
  36. new InputOption('xml', null, InputOption::PARAMETER_NONE, 'To output help as XML'),
  37. ))
  38. ->setName('help')
  39. ->setAliases(array('?'))
  40. ->setDescription('Displays help for a command')
  41. ->setHelp(<<<EOF
  42. The <info>help</info> command displays help for a given command:
  43. <info>./symfony help list</info>
  44. You can also output the help as XML by using the <comment>--xml</comment> option:
  45. <info>./symfony help --xml list</info>
  46. EOF
  47. );
  48. }
  49. public function setCommand(Command $command)
  50. {
  51. $this->command = $command;
  52. }
  53. /**
  54. * @see Command
  55. */
  56. protected function execute(InputInterface $input, OutputInterface $output)
  57. {
  58. if (null === $this->command)
  59. {
  60. $this->command = $this->application->getCommand($input->getArgument('command_name'));
  61. }
  62. if ($input->getOption('xml'))
  63. {
  64. $output->writeln($this->command->asXml(), Output::OUTPUT_RAW);
  65. }
  66. else
  67. {
  68. $output->writeln($this->command->asText());
  69. }
  70. }
  71. }