| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfHelpTask extends sfCommandApplicationTask |
|---|
| 20 |
{ |
|---|
| 21 |
|
|---|
| 22 |
* @see sfTask |
|---|
| 23 |
*/ |
|---|
| 24 |
protected function configure() |
|---|
| 25 |
{ |
|---|
| 26 |
$this->addArguments(array( |
|---|
| 27 |
new sfCommandArgument('task_name', sfCommandArgument::OPTIONAL, 'The task name', 'help'), |
|---|
| 28 |
)); |
|---|
| 29 |
|
|---|
| 30 |
$this->aliases = array('h'); |
|---|
| 31 |
|
|---|
| 32 |
$this->briefDescription = 'Displays help for a task'; |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
* @see sfTask |
|---|
| 37 |
*/ |
|---|
| 38 |
protected function execute($arguments = array(), $options = array()) |
|---|
| 39 |
{ |
|---|
| 40 |
if (!isset($this->commandApplication)) |
|---|
| 41 |
{ |
|---|
| 42 |
throw new sfCommandException('You can only launch this task from the command line.'); |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
$task = $this->commandApplication->getTask($arguments['task_name']); |
|---|
| 46 |
|
|---|
| 47 |
$messages = array(); |
|---|
| 48 |
|
|---|
| 49 |
$messages[] = $this->formatter->format('Usage:', 'COMMENT'); |
|---|
| 50 |
$messages[] = $this->formatter->format(sprintf(' '.$task->getSynopsis(), is_null($this->commandApplication) ? '' : $this->commandApplication->getName()))."\n"; |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
$max = 0; |
|---|
| 54 |
foreach ($task->getOptions() as $option) |
|---|
| 55 |
{ |
|---|
| 56 |
$max = strlen($option->getName()) + 2 > $max ? strlen($option->getName()) + 2 : $max; |
|---|
| 57 |
} |
|---|
| 58 |
foreach ($task->getArguments() as $argument) |
|---|
| 59 |
{ |
|---|
| 60 |
$max = strlen($argument->getName()) > $max ? strlen($argument->getName()) : $max; |
|---|
| 61 |
} |
|---|
| 62 |
$max += strlen($this->formatter->format(' ', 'INFO')); |
|---|
| 63 |
|
|---|
| 64 |
if ($task->getAliases()) |
|---|
| 65 |
{ |
|---|
| 66 |
$messages[] = $this->formatter->format('Aliases:', 'COMMENT').' '.$this->formatter->format(implode(', ', $task->getAliases()), 'INFO')."\n"; |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
if ($task->getArguments()) |
|---|
| 70 |
{ |
|---|
| 71 |
$messages[] = $this->formatter->format('Arguments:', 'COMMENT'); |
|---|
| 72 |
foreach ($task->getArguments() as $argument) |
|---|
| 73 |
{ |
|---|
| 74 |
$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') : ''; |
|---|
| 75 |
$messages[] = sprintf(" %-${max}s %s%s", $this->formatter->format($argument->getName(), 'INFO'), $argument->getHelp(), $default); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
$messages[] = ''; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
if ($task->getOptions()) |
|---|
| 82 |
{ |
|---|
| 83 |
$messages[] = $this->formatter->format('Options:', 'COMMENT'); |
|---|
| 84 |
|
|---|
| 85 |
foreach ($task->getOptions() as $option) |
|---|
| 86 |
{ |
|---|
| 87 |
$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') : ''; |
|---|
| 88 |
$multiple = $option->isArray() ? $this->formatter->format(' (multiple values allowed)', 'COMMENT') : ''; |
|---|
| 89 |
$messages[] = sprintf(' %-'.$max.'s %s%s%s%s', $this->formatter->format('--'.$option->getName(), 'INFO'), $option->getShortcut() ? sprintf('(-%s) ', $option->getShortcut()) : '', $option->getHelp(), $default, $multiple); |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
$messages[] = ''; |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
if ($detailedDescription = $task->getDetailedDescription()) |
|---|
| 96 |
{ |
|---|
| 97 |
$messages[] = $this->formatter->format('Description:', 'COMMENT'); |
|---|
| 98 |
|
|---|
| 99 |
$messages[] = ' '.implode("\n ", explode("\n", $detailedDescription))."\n"; |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
$this->log($messages); |
|---|
| 103 |
} |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|