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