Development

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

You must first sign up to be able to contribute.

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

Revision 23922, 4.3 kB (checked in by fabien, 4 years ago)

[1.4] removed deprecated stuff

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