1. sfHelpTask.class.php
  2. /** * Displays help for a task. * * @package symfony * @subpackage task * @author Fabien Potencier * @version SVN: $Id: sfHelpTask.class.php 23922 2009-11-14 14:58:38Z fabien $ */
  3. class sfHelpTask extends sfCommandApplicationTask
  4. {
  5. /**
  6. * @see sfTask
  7. */
  8. protected function configure()
  9. {
  10. $this->addArguments(array(
  11. new sfCommandArgument('task_name', sfCommandArgument::OPTIONAL, 'The task name', 'help'),
  12. ));
  13. $this->addOptions(array(
  14. new sfCommandOption('xml', null, sfCommandOption::PARAMETER_NONE, 'To output help as XML'),
  15. ));
  16. $this->briefDescription = 'Displays help for a task';
  17. $this->detailedDescription = <<<EOF
  18. The [help|INFO] task displays help for a given task:
  19. [./symfony help test:all|INFO]
  20. You can also output the help as XML by using the [--xml|COMMENT] option:
  21. [./symfony help test:all --xml|INFO]
  22. EOF;
  23. }
  24. /**
  25. * @see sfTask
  26. */
  27. protected function execute($arguments = array(), $options = array())
  28. {
  29. if (!isset($this->commandApplication))
  30. {
  31. throw new sfCommandException('You can only launch this task from the command line.');
  32. }
  33. $task = $this->commandApplication->getTask($arguments['task_name']);
  34. if ($options['xml'])
  35. {
  36. $this->outputAsXml($task);
  37. }
  38. else
  39. {
  40. $this->outputAsText($task);
  41. }
  42. }
  43. protected function outputAsText(sfTask $task)
  44. {
  45. $messages = array();
  46. $messages[] = $this->formatter->format('Usage:', 'COMMENT');
  47. $messages[] = $this->formatter->format(sprintf(' '.$task->getSynopsis(), null === $this->commandApplication ? '' : $this->commandApplication->getName()))."\n";
  48. // find the largest option or argument name
  49. $max = 0;
  50. foreach ($task->getOptions() as $option)
  51. {
  52. $max = strlen($option->getName()) + 2 > $max ? strlen($option->getName()) + 2 : $max;
  53. }
  54. foreach ($task->getArguments() as $argument)
  55. {
  56. $max = strlen($argument->getName()) > $max ? strlen($argument->getName()) : $max;
  57. }
  58. $max += strlen($this->formatter->format(' ', 'INFO'));
  59. if ($task->getAliases())
  60. {
  61. $messages[] = $this->formatter->format('Aliases:', 'COMMENT').' '.$this->formatter->format(implode(', ', $task->getAliases()), 'INFO')."\n";
  62. }
  63. if ($task->getArguments())
  64. {
  65. $messages[] = $this->formatter->format('Arguments:', 'COMMENT');
  66. foreach ($task->getArguments() as $argument)
  67. {
  68. $default = null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault())) ? $this->formatter->format(sprintf(' (default: %s)', is_array($argument->getDefault()) ? str_replace("\n", '', print_r($argument->getDefault(), true)): $argument->getDefault()), 'COMMENT') : '';
  69. $messages[] = sprintf(" %-${max}s %s%s", $this->formatter->format($argument->getName(), 'INFO'), $argument->getHelp(), $default);
  70. }
  71. $messages[] = '';
  72. }
  73. if ($task->getOptions())
  74. {
  75. $messages[] = $this->formatter->format('Options:', 'COMMENT');
  76. foreach ($task->getOptions() as $option)
  77. {
  78. $default = $option->acceptParameter() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault())) ? $this->formatter->format(sprintf(' (default: %s)', is_array($option->getDefault()) ? str_replace("\n", '', print_r($option->getDefault(), true)): $option->getDefault()), 'COMMENT') : '';
  79. $multiple = $option->isArray() ? $this->formatter->format(' (multiple values allowed)', 'COMMENT') : '';
  80. $messages[] = sprintf(' %-'.$max.'s %s%s%s%s', $this->formatter->format('--'.$option->getName(), 'INFO'), $option->getShortcut() ? sprintf('(-%s) ', $option->getShortcut()) : '', $option->getHelp(), $default, $multiple);
  81. }
  82. $messages[] = '';
  83. }
  84. if ($detailedDescription = $task->getDetailedDescription())
  85. {
  86. $messages[] = $this->formatter->format('Description:', 'COMMENT');
  87. $messages[] = ' '.implode("\n ", explode("\n", $detailedDescription))."\n";
  88. }
  89. $this->log($messages);
  90. }
  91. protected function outputAsXml(sfTask $task)
  92. {
  93. echo $task->asXml();
  94. }
  95. }

Debug toolbar