Development

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

You must first sign up to be able to contribute.

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

Revision 21908, 4.3 kB (checked in by fabien, 5 years ago)

[1.3] made some optimizations by replacing is_null() by null ===

  • 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->addOptions(array(
31       new sfCommandOption('xml', null, sfCommandOption::PARAMETER_NONE, 'To output help as XML'),
32     ));
33
34     $this->aliases = array('h');
35
36     $this->briefDescription = 'Displays help for a task';
37
38     $this->detailedDescription = <<<EOF
39 The [help|INFO] task displays help for a given task:
40
41   [./symfony help test:all|INFO]
42
43 You can also output the help as XML by using the [--xml|COMMENT] option:
44
45   [./symfony help test:all --xml|INFO]
46 EOF;
47   }
48
49   /**
50    * @see sfTask
51    */
52   protected function execute($arguments = array(), $options = array())
53   {
54     if (!isset($this->commandApplication))
55     {
56       throw new sfCommandException('You can only launch this task from the command line.');
57     }
58
59     $task = $this->commandApplication->getTask($arguments['task_name']);
60
61     if ($options['xml'])
62     {
63       $this->outputAsXml($task);
64     }
65     else
66     {
67       $this->outputAsText($task);
68     }
69   }
70
71   protected function outputAsText(sfTask $task)
72   {
73     $messages = array();
74
75     $messages[] = $this->formatter->format('Usage:', 'COMMENT');
76     $messages[] = $this->formatter->format(sprintf(' '.$task->getSynopsis(), null === $this->commandApplication ? '' : $this->commandApplication->getName()))."\n";
77
78     // find the largest option or argument name
79     $max = 0;
80     foreach ($task->getOptions() as $option)
81     {
82       $max = strlen($option->getName()) + 2 > $max ? strlen($option->getName()) + 2 : $max;
83     }
84     foreach ($task->getArguments() as $argument)
85     {
86       $max = strlen($argument->getName()) > $max ? strlen($argument->getName()) : $max;
87     }
88     $max += strlen($this->formatter->format(' ', 'INFO'));
89
90     if ($task->getAliases())
91     {
92       $messages[] = $this->formatter->format('Aliases:', 'COMMENT').' '.$this->formatter->format(implode(', ', $task->getAliases()), 'INFO')."\n";
93     }
94
95     if ($task->getArguments())
96     {
97       $messages[] = $this->formatter->format('Arguments:', 'COMMENT');
98       foreach ($task->getArguments() as $argument)
99       {
100         $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') : '';
101         $messages[] = sprintf(" %-${max}s %s%s", $this->formatter->format($argument->getName(), 'INFO'), $argument->getHelp(), $default);
102       }
103
104       $messages[] = '';
105     }
106
107     if ($task->getOptions())
108     {
109       $messages[] = $this->formatter->format('Options:', 'COMMENT');
110
111       foreach ($task->getOptions() as $option)
112       {
113         $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') : '';
114         $multiple = $option->isArray() ? $this->formatter->format(' (multiple values allowed)', 'COMMENT') : '';
115         $messages[] = sprintf(' %-'.$max.'s %s%s%s%s', $this->formatter->format('--'.$option->getName(), 'INFO'), $option->getShortcut() ? sprintf('(-%s) ', $option->getShortcut()) : '', $option->getHelp(), $default, $multiple);
116       }
117
118       $messages[] = '';
119     }
120
121     if ($detailedDescription = $task->getDetailedDescription())
122     {
123       $messages[] = $this->formatter->format('Description:', 'COMMENT');
124
125       $messages[] = ' '.implode("\n ", explode("\n", $detailedDescription))."\n";
126     }
127
128     $this->log($messages);
129   }
130
131   protected function outputAsXml(sfTask $task)
132   {
133     echo $task->asXml();
134   }
135 }
136
Note: See TracBrowser for help on using the browser.