pictcode / lib / Cake / Console / HelpFormatter.php @ 0b1b8047
履歴 | 表示 | アノテート | ダウンロード (5.572 KB)
1 | 635eef61 | spyder1211 | <?php
|
---|---|---|---|
2 | /**
|
||
3 | * HelpFormatter
|
||
4 | *
|
||
5 | * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||
6 | * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||
7 | *
|
||
8 | * Licensed under The MIT License
|
||
9 | * For full copyright and license information, please see the LICENSE.txt
|
||
10 | * Redistributions of files must retain the above copyright notice.
|
||
11 | *
|
||
12 | * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||
13 | * @link http://cakephp.org CakePHP(tm) Project
|
||
14 | * @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||
15 | */
|
||
16 | |||
17 | App::uses('CakeText', 'Utility'); |
||
18 | |||
19 | /**
|
||
20 | * HelpFormatter formats help for console shells. Can format to either
|
||
21 | * text or XML formats. Uses ConsoleOptionParser methods to generate help.
|
||
22 | *
|
||
23 | * Generally not directly used. Using $parser->help($command, 'xml'); is usually
|
||
24 | * how you would access help. Or via the `--help=xml` option on the command line.
|
||
25 | *
|
||
26 | * Xml output is useful for integration with other tools like IDE's or other build tools.
|
||
27 | *
|
||
28 | * @package Cake.Console
|
||
29 | * @since CakePHP(tm) v 2.0
|
||
30 | */
|
||
31 | class HelpFormatter { |
||
32 | |||
33 | /**
|
||
34 | * The maximum number of arguments shown when generating usage.
|
||
35 | *
|
||
36 | * @var int
|
||
37 | */
|
||
38 | protected $_maxArgs = 6; |
||
39 | |||
40 | /**
|
||
41 | * The maximum number of options shown when generating usage.
|
||
42 | *
|
||
43 | * @var int
|
||
44 | */
|
||
45 | protected $_maxOptions = 6; |
||
46 | |||
47 | /**
|
||
48 | * Build the help formatter for an OptionParser
|
||
49 | *
|
||
50 | * @param ConsoleOptionParser $parser The option parser help is being generated for.
|
||
51 | */
|
||
52 | public function __construct(ConsoleOptionParser $parser) { |
||
53 | $this->_parser = $parser; |
||
54 | } |
||
55 | |||
56 | /**
|
||
57 | * Get the help as formatted text suitable for output on the command line.
|
||
58 | *
|
||
59 | * @param int $width The width of the help output.
|
||
60 | * @return string
|
||
61 | */
|
||
62 | public function text($width = 72) { |
||
63 | $parser = $this->_parser; |
||
64 | $out = array(); |
||
65 | $description = $parser->description(); |
||
66 | if (!empty($description)) { |
||
67 | $out[] = CakeText::wrap($description, $width); |
||
68 | $out[] = ''; |
||
69 | } |
||
70 | $out[] = __d('cake_console', '<info>Usage:</info>'); |
||
71 | $out[] = $this->_generateUsage(); |
||
72 | $out[] = ''; |
||
73 | $subcommands = $parser->subcommands(); |
||
74 | if (!empty($subcommands)) { |
||
75 | $out[] = __d('cake_console', '<info>Subcommands:</info>'); |
||
76 | $out[] = ''; |
||
77 | $max = $this->_getMaxLength($subcommands) + 2; |
||
78 | foreach ($subcommands as $command) { |
||
79 | $out[] = CakeText::wrap($command->help($max), array( |
||
80 | 'width' => $width, |
||
81 | 'indent' => str_repeat(' ', $max), |
||
82 | 'indentAt' => 1 |
||
83 | )); |
||
84 | } |
||
85 | $out[] = ''; |
||
86 | $out[] = __d('cake_console', 'To see help on a subcommand use <info>`cake %s [subcommand] --help`</info>', $parser->command()); |
||
87 | $out[] = ''; |
||
88 | } |
||
89 | |||
90 | $options = $parser->options(); |
||
91 | if (!empty($options)) { |
||
92 | $max = $this->_getMaxLength($options) + 8; |
||
93 | $out[] = __d('cake_console', '<info>Options:</info>'); |
||
94 | $out[] = ''; |
||
95 | foreach ($options as $option) { |
||
96 | $out[] = CakeText::wrap($option->help($max), array( |
||
97 | 'width' => $width, |
||
98 | 'indent' => str_repeat(' ', $max), |
||
99 | 'indentAt' => 1 |
||
100 | )); |
||
101 | } |
||
102 | $out[] = ''; |
||
103 | } |
||
104 | |||
105 | $arguments = $parser->arguments(); |
||
106 | if (!empty($arguments)) { |
||
107 | $max = $this->_getMaxLength($arguments) + 2; |
||
108 | $out[] = __d('cake_console', '<info>Arguments:</info>'); |
||
109 | $out[] = ''; |
||
110 | foreach ($arguments as $argument) { |
||
111 | $out[] = CakeText::wrap($argument->help($max), array( |
||
112 | 'width' => $width, |
||
113 | 'indent' => str_repeat(' ', $max), |
||
114 | 'indentAt' => 1 |
||
115 | )); |
||
116 | } |
||
117 | $out[] = ''; |
||
118 | } |
||
119 | $epilog = $parser->epilog(); |
||
120 | if (!empty($epilog)) { |
||
121 | $out[] = CakeText::wrap($epilog, $width); |
||
122 | $out[] = ''; |
||
123 | } |
||
124 | return implode("\n", $out); |
||
125 | } |
||
126 | |||
127 | /**
|
||
128 | * Generate the usage for a shell based on its arguments and options.
|
||
129 | * Usage strings favor short options over the long ones. and optional args will
|
||
130 | * be indicated with []
|
||
131 | *
|
||
132 | * @return string
|
||
133 | */
|
||
134 | protected function _generateUsage() { |
||
135 | $usage = array('cake ' . $this->_parser->command()); |
||
136 | $subcommands = $this->_parser->subcommands(); |
||
137 | if (!empty($subcommands)) { |
||
138 | $usage[] = '[subcommand]'; |
||
139 | } |
||
140 | $options = array(); |
||
141 | foreach ($this->_parser->options() as $option) { |
||
142 | $options[] = $option->usage(); |
||
143 | } |
||
144 | if (count($options) > $this->_maxOptions) { |
||
145 | $options = array('[options]'); |
||
146 | } |
||
147 | $usage = array_merge($usage, $options); |
||
148 | $args = array(); |
||
149 | foreach ($this->_parser->arguments() as $argument) { |
||
150 | $args[] = $argument->usage(); |
||
151 | } |
||
152 | if (count($args) > $this->_maxArgs) { |
||
153 | $args = array('[arguments]'); |
||
154 | } |
||
155 | $usage = array_merge($usage, $args); |
||
156 | return implode(' ', $usage); |
||
157 | } |
||
158 | |||
159 | /**
|
||
160 | * Iterate over a collection and find the longest named thing.
|
||
161 | *
|
||
162 | * @param array $collection The collection to find a max length of.
|
||
163 | * @return int
|
||
164 | */
|
||
165 | protected function _getMaxLength($collection) { |
||
166 | $max = 0; |
||
167 | foreach ($collection as $item) { |
||
168 | $max = (strlen($item->name()) > $max) ? strlen($item->name()) : $max; |
||
169 | } |
||
170 | return $max; |
||
171 | } |
||
172 | |||
173 | /**
|
||
174 | * Get the help as an xml string.
|
||
175 | *
|
||
176 | * @param bool $string Return the SimpleXml object or a string. Defaults to true.
|
||
177 | * @return string|SimpleXmlElement See $string
|
||
178 | */
|
||
179 | public function xml($string = true) { |
||
180 | $parser = $this->_parser; |
||
181 | $xml = new SimpleXmlElement('<shell></shell>'); |
||
182 | $xml->addChild('command', $parser->command()); |
||
183 | $xml->addChild('description', $parser->description()); |
||
184 | |||
185 | $xml->addChild('epilog', $parser->epilog()); |
||
186 | $subcommands = $xml->addChild('subcommands'); |
||
187 | foreach ($parser->subcommands() as $command) { |
||
188 | $command->xml($subcommands); |
||
189 | } |
||
190 | $options = $xml->addChild('options'); |
||
191 | foreach ($parser->options() as $option) { |
||
192 | $option->xml($options); |
||
193 | } |
||
194 | $arguments = $xml->addChild('arguments'); |
||
195 | foreach ($parser->arguments() as $argument) { |
||
196 | $argument->xml($arguments); |
||
197 | } |
||
198 | return $string ? $xml->asXml() : $xml; |
||
199 | } |
||
200 | |||
201 | } |