1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17: App::uses('String', 'Utility');
18:
19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30:
31: class HelpFormatter {
32: 33: 34: 35: 36:
37: protected $_maxArgs = 6;
38:
39: 40: 41: 42: 43:
44: protected $_maxOptions = 6;
45:
46: 47: 48: 49: 50:
51: public function __construct(ConsoleOptionParser $parser) {
52: $this->_parser = $parser;
53: }
54:
55: 56: 57: 58: 59: 60:
61: public function text($width = 72) {
62: $parser = $this->_parser;
63: $out = array();
64: $description = $parser->description();
65: if (!empty($description)) {
66: $out[] = String::wrap($description, $width);
67: $out[] = '';
68: }
69: $out[] = __d('cake_console', '<info>Usage:</info>');
70: $out[] = $this->_generateUsage();
71: $out[] = '';
72: $subcommands = $parser->subcommands();
73: if (!empty($subcommands)) {
74: $out[] = __d('cake_console', '<info>Subcommands:</info>');
75: $out[] = '';
76: $max = $this->_getMaxLength($subcommands) + 2;
77: foreach ($subcommands as $command) {
78: $out[] = String::wrap($command->help($max), array(
79: 'width' => $width,
80: 'indent' => str_repeat(' ', $max),
81: 'indentAt' => 1
82: ));
83: }
84: $out[] = '';
85: $out[] = __d('cake_console', 'To see help on a subcommand use <info>`cake %s [subcommand] --help`</info>', $parser->command());
86: $out[] = '';
87: }
88:
89: $options = $parser->options();
90: if (!empty($options)) {
91: $max = $this->_getMaxLength($options) + 8;
92: $out[] = __d('cake_console', '<info>Options:</info>');
93: $out[] = '';
94: foreach ($options as $option) {
95: $out[] = String::wrap($option->help($max), array(
96: 'width' => $width,
97: 'indent' => str_repeat(' ', $max),
98: 'indentAt' => 1
99: ));
100: }
101: $out[] = '';
102: }
103:
104: $arguments = $parser->arguments();
105: if (!empty($arguments)) {
106: $max = $this->_getMaxLength($arguments) + 2;
107: $out[] = __d('cake_console', '<info>Arguments:</info>');
108: $out[] = '';
109: foreach ($arguments as $argument) {
110: $out[] = String::wrap($argument->help($max), array(
111: 'width' => $width,
112: 'indent' => str_repeat(' ', $max),
113: 'indentAt' => 1
114: ));
115: }
116: $out[] = '';
117: }
118: $epilog = $parser->epilog();
119: if (!empty($epilog)) {
120: $out[] = String::wrap($epilog, $width);
121: $out[] = '';
122: }
123: return implode("\n", $out);
124: }
125:
126: 127: 128: 129: 130: 131: 132:
133: protected function _generateUsage() {
134: $usage = array('cake ' . $this->_parser->command());
135: $subcommands = $this->_parser->subcommands();
136: if (!empty($subcommands)) {
137: $usage[] = '[subcommand]';
138: }
139: $options = array();
140: foreach ($this->_parser->options() as $option) {
141: $options[] = $option->usage();
142: }
143: if (count($options) > $this->_maxOptions) {
144: $options = array('[options]');
145: }
146: $usage = array_merge($usage, $options);
147: $args = array();
148: foreach ($this->_parser->arguments() as $argument) {
149: $args[] = $argument->usage();
150: }
151: if (count($args) > $this->_maxArgs) {
152: $args = array('[arguments]');
153: }
154: $usage = array_merge($usage, $args);
155: return implode(' ', $usage);
156: }
157:
158: 159: 160: 161: 162: 163:
164: protected function _getMaxLength($collection) {
165: $max = 0;
166: foreach ($collection as $item) {
167: $max = (strlen($item->name()) > $max) ? strlen($item->name()) : $max;
168: }
169: return $max;
170: }
171:
172: 173: 174: 175: 176: 177:
178: public function xml($string = true) {
179: $parser = $this->_parser;
180: $xml = new SimpleXmlElement('<shell></shell>');
181: $xml->addChild('command', $parser->command());
182: $xml->addChild('description', $parser->description());
183:
184: $xml->addChild('epilog', $parser->epilog());
185: $subcommands = $xml->addChild('subcommands');
186: foreach ($parser->subcommands() as $command) {
187: $command->xml($subcommands);
188: }
189: $options = $xml->addChild('options');
190: foreach ($parser->options() as $option) {
191: $option->xml($options);
192: }
193: $arguments = $xml->addChild('arguments');
194: foreach ($parser->arguments() as $argument) {
195: $argument->xml($arguments);
196: }
197: return $string ? $xml->asXml() : $xml;
198: }
199: }
200: