Development

/branches/1.2/lib/task/help/sfHelpTask.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/task/help/sfHelpTask.class.php

Revision 7397, 3.7 kB (checked in by fabien, 7 years ago)

added sfTask::log() and sfTask::logSection() shortcuts

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Displays help for a task.
13  *
14  * @package    symfony
15  * @subpackage task
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
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     // find the largest option or argument name
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
Note: See TracBrowser for help on using the browser.