sfHelpTask.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Displays help for a task.
  11. *
  12. * @package symfony
  13. * @subpackage task
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfHelpTask.class.php 7397 2008-02-08 06:48:35Z fabien $
  16. */
  17. class sfHelpTask extends sfCommandApplicationTask
  18. {
  19. /**
  20. * @see sfTask
  21. */
  22. protected function configure()
  23. {
  24. $this->addArguments(array(
  25. new sfCommandArgument('task_name', sfCommandArgument::OPTIONAL, 'The task name', 'help'),
  26. ));
  27. $this->aliases = array('h');
  28. $this->briefDescription = 'Displays help for a task';
  29. }
  30. /**
  31. * @see sfTask
  32. */
  33. protected function execute($arguments = array(), $options = array())
  34. {
  35. if (!isset($this->commandApplication))
  36. {
  37. throw new sfCommandException('You can only launch this task from the command line.');
  38. }
  39. $task = $this->commandApplication->getTask($arguments['task_name']);
  40. $messages = array();
  41. $messages[] = $this->formatter->format('Usage:', 'COMMENT');
  42. $messages[] = $this->formatter->format(sprintf(' '.$task->getSynopsis(), is_null($this->commandApplication) ? '' : $this->commandApplication->getName()))."\n";
  43. // find the largest option or argument name
  44. $max = 0;
  45. foreach ($task->getOptions() as $option)
  46. {
  47. $max = strlen($option->getName()) + 2 > $max ? strlen($option->getName()) + 2 : $max;
  48. }
  49. foreach ($task->getArguments() as $argument)
  50. {
  51. $max = strlen($argument->getName()) > $max ? strlen($argument->getName()) : $max;
  52. }
  53. $max += strlen($this->formatter->format(' ', 'INFO'));
  54. if ($task->getAliases())
  55. {
  56. $messages[] = $this->formatter->format('Aliases:', 'COMMENT').' '.$this->formatter->format(implode(', ', $task->getAliases()), 'INFO')."\n";
  57. }
  58. if ($task->getArguments())
  59. {
  60. $messages[] = $this->formatter->format('Arguments:', 'COMMENT');
  61. foreach ($task->getArguments() as $argument)
  62. {
  63. $default = !is_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') : '';
  64. $messages[] = sprintf(" %-${max}s %s%s", $this->formatter->format($argument->getName(), 'INFO'), $argument->getHelp(), $default);
  65. }
  66. $messages[] = '';
  67. }
  68. if ($task->getOptions())
  69. {
  70. $messages[] = $this->formatter->format('Options:', 'COMMENT');
  71. foreach ($task->getOptions() as $option)
  72. {
  73. $default = $option->acceptParameter() && !is_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') : '';
  74. $multiple = $option->isArray() ? $this->formatter->format(' (multiple values allowed)', 'COMMENT') : '';
  75. $messages[] = sprintf(' %-'.$max.'s %s%s%s%s', $this->formatter->format('--'.$option->getName(), 'INFO'), $option->getShortcut() ? sprintf('(-%s) ', $option->getShortcut()) : '', $option->getHelp(), $default, $multiple);
  76. }
  77. $messages[] = '';
  78. }
  79. if ($detailedDescription = $task->getDetailedDescription())
  80. {
  81. $messages[] = $this->formatter->format('Description:', 'COMMENT');
  82. $messages[] = ' '.implode("\n ", explode("\n", $detailedDescription))."\n";
  83. }
  84. $this->log($messages);
  85. }
  86. }