summaryrefslogtreecommitdiff
path: root/poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/Console/HelpFormatter.php
blob: 2b495b9e17061bc5090c5c9efc83a5d50683c7a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
 * HelpFormatter
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('String', 'Utility');

/**
 * HelpFormatter formats help for console shells.  Can format to either
 * text or XML formats.  Uses ConsoleOptionParser methods to generate help.
 *
 * Generally not directly used. Using $parser->help($command, 'xml'); is usually
 * how you would access help.  Or via the `--help=xml` option on the command line.
 *
 * Xml output is useful for integration with other tools like IDE's or other build tools.
 *
 * @package       Cake.Console
 * @since  CakePHP(tm) v 2.0
 */
class HelpFormatter {

/**
 * The maximum number of arguments shown when generating usage.
 *
 * @var integer
 */
	protected $_maxArgs = 6;

/**
 * The maximum number of options shown when generating usage.
 *
 * @var integer
 */
	protected $_maxOptions = 6;

/**
 * Build the help formatter for a an OptionParser
 *
 * @param ConsoleOptionParser $parser The option parser help is being generated for.
 */
	public function __construct(ConsoleOptionParser $parser) {
		$this->_parser = $parser;
	}

/**
 * Get the help as formatted text suitable for output on the command line.
 *
 * @param integer $width The width of the help output.
 * @return string
 */
	public function text($width = 72) {
		$parser = $this->_parser;
		$out = array();
		$description = $parser->description();
		if (!empty($description)) {
			$out[] = String::wrap($description, $width);
			$out[] = '';
		}
		$out[] = __d('cake_console', '<info>Usage:</info>');
		$out[] = $this->_generateUsage();
		$out[] = '';
		$subcommands = $parser->subcommands();
		if (!empty($subcommands)) {
			$out[] = __d('cake_console', '<info>Subcommands:</info>');
			$out[] = '';
			$max = $this->_getMaxLength($subcommands) + 2;
			foreach ($subcommands as $command) {
				$out[] = String::wrap($command->help($max), array(
					'width' => $width,
					'indent' => str_repeat(' ', $max),
					'indentAt' => 1
				));
			}
			$out[] = '';
			$out[] = __d('cake_console', 'To see help on a subcommand use <info>`cake %s [subcommand] --help`</info>', $parser->command());
			$out[] = '';
		}

		$options = $parser->options();
		if (!empty($options)) {
			$max = $this->_getMaxLength($options) + 8;
			$out[] = __d('cake_console', '<info>Options:</info>');
			$out[] = '';
			foreach ($options as $option) {
				$out[] = String::wrap($option->help($max), array(
					'width' => $width,
					'indent' => str_repeat(' ', $max),
					'indentAt' => 1
				));
			}
			$out[] = '';
		}

		$arguments = $parser->arguments();
		if (!empty($arguments)) {
			$max = $this->_getMaxLength($arguments) + 2;
			$out[] = __d('cake_console', '<info>Arguments:</info>');
			$out[] = '';
			foreach ($arguments as $argument) {
				$out[] = String::wrap($argument->help($max), array(
					'width' => $width,
					'indent' => str_repeat(' ', $max),
					'indentAt' => 1
				));
			}
			$out[] = '';
		}
		$epilog = $parser->epilog();
		if (!empty($epilog)) {
			$out[] = String::wrap($epilog, $width);
			$out[] = '';
		}
		return implode("\n", $out);
	}

/**
 * Generate the usage for a shell based on its arguments and options.
 * Usage strings favor short options over the long ones. and optional args will
 * be indicated with []
 *
 * @return string
 */
	protected function _generateUsage() {
		$usage = array('cake ' . $this->_parser->command());
		$subcommands = $this->_parser->subcommands();
		if (!empty($subcommands)) {
			$usage[] = '[subcommand]';
		}
		$options = array();
		foreach ($this->_parser->options() as $option) {
			$options[] = $option->usage();
		}
		if (count($options) > $this->_maxOptions) {
			$options = array('[options]');
		}
		$usage = array_merge($usage, $options);
		$args = array();
		foreach ($this->_parser->arguments() as $argument) {
			$args[] = $argument->usage();
		}
		if (count($args) > $this->_maxArgs) {
			$args = array('[arguments]');
		}
		$usage = array_merge($usage, $args);
		return implode(' ', $usage);
	}

/**
 * Iterate over a collection and find the longest named thing.
 *
 * @param array $collection
 * @return integer
 */
	protected function _getMaxLength($collection) {
		$max = 0;
		foreach ($collection as $item) {
			$max = (strlen($item->name()) > $max) ? strlen($item->name()) : $max;
		}
		return $max;
	}

/**
 * Get the help as an xml string.
 *
 * @param boolean $string Return the SimpleXml object or a string.  Defaults to true.
 * @return mixed. See $string
 */
	public function xml($string = true) {
		$parser = $this->_parser;
		$xml = new SimpleXmlElement('<shell></shell>');
		$xml->addChild('command', $parser->command());
		$xml->addChild('description', $parser->description());

		$xml->addChild('epilog', $parser->epilog());
		$subcommands = $xml->addChild('subcommands');
		foreach ($parser->subcommands() as $command) {
			$command->xml($subcommands);
		}
		$options = $xml->addChild('options');
		foreach ($parser->options() as $option) {
			$option->xml($options);
		}
		$arguments = $xml->addChild('arguments');
		foreach ($parser->arguments() as $argument) {
			$argument->xml($arguments);
		}
		return $string ? $xml->asXml() : $xml;
	}

}