2010-10-19 03:38:14 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-10-20 02:44:46 +00:00
|
|
|
* HelpFormatter
|
2010-10-19 03:38:14 +00:00
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-10-19 03:38:14 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-19 03:38:14 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-10-19 03:38:14 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2010-10-19 03:38:14 +00:00
|
|
|
*/
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2015-01-05 00:00:57 +00:00
|
|
|
App::uses('CakeText', 'Utility');
|
2010-11-17 04:19:15 +00:00
|
|
|
|
2010-10-20 02:44:46 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* HelpFormatter formats help for console shells. Can format to either
|
|
|
|
* text or XML formats. Uses ConsoleOptionParser methods to generate help.
|
2010-10-20 02:44:46 +00:00
|
|
|
*
|
|
|
|
* Generally not directly used. Using $parser->help($command, 'xml'); is usually
|
2012-12-22 22:48:15 +00:00
|
|
|
* how you would access help. Or via the `--help=xml` option on the command line.
|
2010-10-20 02:44:46 +00:00
|
|
|
*
|
2011-04-25 19:17:59 +00:00
|
|
|
* Xml output is useful for integration with other tools like IDE's or other build tools.
|
2010-10-20 02:44:46 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Console
|
2011-07-26 01:46:52 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2010-10-20 02:44:46 +00:00
|
|
|
*/
|
2010-10-19 03:38:14 +00:00
|
|
|
class HelpFormatter {
|
2012-03-03 23:55:07 +00:00
|
|
|
|
2011-08-06 13:44:49 +00:00
|
|
|
/**
|
|
|
|
* The maximum number of arguments shown when generating usage.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var int
|
2011-08-06 13:44:49 +00:00
|
|
|
*/
|
|
|
|
protected $_maxArgs = 6;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The maximum number of options shown when generating usage.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var int
|
2011-08-06 13:44:49 +00:00
|
|
|
*/
|
|
|
|
protected $_maxOptions = 6;
|
|
|
|
|
2010-10-19 03:38:14 +00:00
|
|
|
/**
|
2014-07-30 20:11:03 +00:00
|
|
|
* Build the help formatter for an OptionParser
|
2010-10-19 03:38:14 +00:00
|
|
|
*
|
2010-12-22 04:11:23 +00:00
|
|
|
* @param ConsoleOptionParser $parser The option parser help is being generated for.
|
2010-10-19 03:38:14 +00:00
|
|
|
*/
|
|
|
|
public function __construct(ConsoleOptionParser $parser) {
|
|
|
|
$this->_parser = $parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-20 01:40:27 +00:00
|
|
|
* Get the help as formatted text suitable for output on the command line.
|
2010-10-19 03:38:14 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $width The width of the help output.
|
2010-10-19 03:38:14 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2010-10-20 01:40:27 +00:00
|
|
|
public function text($width = 72) {
|
2010-10-20 01:31:16 +00:00
|
|
|
$parser = $this->_parser;
|
|
|
|
$out = array();
|
|
|
|
$description = $parser->description();
|
|
|
|
if (!empty($description)) {
|
2015-01-05 00:00:57 +00:00
|
|
|
$out[] = CakeText::wrap($description, $width);
|
2010-10-20 01:31:16 +00:00
|
|
|
$out[] = '';
|
|
|
|
}
|
2011-04-25 19:17:59 +00:00
|
|
|
$out[] = __d('cake_console', '<info>Usage:</info>');
|
2010-10-20 01:31:16 +00:00
|
|
|
$out[] = $this->_generateUsage();
|
|
|
|
$out[] = '';
|
|
|
|
$subcommands = $parser->subcommands();
|
|
|
|
if (!empty($subcommands)) {
|
2011-04-25 19:17:59 +00:00
|
|
|
$out[] = __d('cake_console', '<info>Subcommands:</info>');
|
2010-10-20 01:31:16 +00:00
|
|
|
$out[] = '';
|
|
|
|
$max = $this->_getMaxLength($subcommands) + 2;
|
|
|
|
foreach ($subcommands as $command) {
|
2015-01-05 00:00:57 +00:00
|
|
|
$out[] = CakeText::wrap($command->help($max), array(
|
2010-10-20 01:31:16 +00:00
|
|
|
'width' => $width,
|
|
|
|
'indent' => str_repeat(' ', $max),
|
|
|
|
'indentAt' => 1
|
|
|
|
));
|
|
|
|
}
|
|
|
|
$out[] = '';
|
2011-04-25 19:17:59 +00:00
|
|
|
$out[] = __d('cake_console', 'To see help on a subcommand use <info>`cake %s [subcommand] --help`</info>', $parser->command());
|
2010-10-20 01:31:16 +00:00
|
|
|
$out[] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = $parser->options();
|
|
|
|
if (!empty($options)) {
|
|
|
|
$max = $this->_getMaxLength($options) + 8;
|
2011-04-25 19:17:59 +00:00
|
|
|
$out[] = __d('cake_console', '<info>Options:</info>');
|
2010-10-20 01:31:16 +00:00
|
|
|
$out[] = '';
|
|
|
|
foreach ($options as $option) {
|
2015-01-05 00:00:57 +00:00
|
|
|
$out[] = CakeText::wrap($option->help($max), array(
|
2010-10-20 01:31:16 +00:00
|
|
|
'width' => $width,
|
|
|
|
'indent' => str_repeat(' ', $max),
|
|
|
|
'indentAt' => 1
|
|
|
|
));
|
|
|
|
}
|
|
|
|
$out[] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$arguments = $parser->arguments();
|
|
|
|
if (!empty($arguments)) {
|
|
|
|
$max = $this->_getMaxLength($arguments) + 2;
|
2011-04-25 19:17:59 +00:00
|
|
|
$out[] = __d('cake_console', '<info>Arguments:</info>');
|
2010-10-20 01:31:16 +00:00
|
|
|
$out[] = '';
|
|
|
|
foreach ($arguments as $argument) {
|
2015-01-05 00:00:57 +00:00
|
|
|
$out[] = CakeText::wrap($argument->help($max), array(
|
2010-10-20 01:31:16 +00:00
|
|
|
'width' => $width,
|
|
|
|
'indent' => str_repeat(' ', $max),
|
|
|
|
'indentAt' => 1
|
|
|
|
));
|
|
|
|
}
|
|
|
|
$out[] = '';
|
|
|
|
}
|
|
|
|
$epilog = $parser->epilog();
|
|
|
|
if (!empty($epilog)) {
|
2015-01-05 00:00:57 +00:00
|
|
|
$out[] = CakeText::wrap($epilog, $width);
|
2010-10-20 01:31:16 +00:00
|
|
|
$out[] = '';
|
|
|
|
}
|
|
|
|
return implode("\n", $out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the usage for a shell based on its arguments and options.
|
2011-04-25 19:17:59 +00:00
|
|
|
* Usage strings favor short options over the long ones. and optional args will
|
2010-10-20 01:31:16 +00:00
|
|
|
* be indicated with []
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function _generateUsage() {
|
|
|
|
$usage = array('cake ' . $this->_parser->command());
|
|
|
|
$subcommands = $this->_parser->subcommands();
|
|
|
|
if (!empty($subcommands)) {
|
|
|
|
$usage[] = '[subcommand]';
|
|
|
|
}
|
2011-08-06 13:44:49 +00:00
|
|
|
$options = array();
|
2010-10-20 01:31:16 +00:00
|
|
|
foreach ($this->_parser->options() as $option) {
|
2011-08-06 13:44:49 +00:00
|
|
|
$options[] = $option->usage();
|
2010-10-20 01:31:16 +00:00
|
|
|
}
|
2011-11-30 15:44:11 +00:00
|
|
|
if (count($options) > $this->_maxOptions) {
|
2011-08-06 13:44:49 +00:00
|
|
|
$options = array('[options]');
|
|
|
|
}
|
|
|
|
$usage = array_merge($usage, $options);
|
|
|
|
$args = array();
|
2010-10-20 01:31:16 +00:00
|
|
|
foreach ($this->_parser->arguments() as $argument) {
|
2011-08-06 13:44:49 +00:00
|
|
|
$args[] = $argument->usage();
|
|
|
|
}
|
|
|
|
if (count($args) > $this->_maxArgs) {
|
|
|
|
$args = array('[arguments]');
|
2010-10-20 01:31:16 +00:00
|
|
|
}
|
2011-08-06 13:44:49 +00:00
|
|
|
$usage = array_merge($usage, $args);
|
2010-10-20 01:31:16 +00:00
|
|
|
return implode(' ', $usage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterate over a collection and find the longest named thing.
|
|
|
|
*
|
2014-05-28 03:34:53 +00:00
|
|
|
* @param array $collection The collection to find a max length of.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return int
|
2010-10-20 01:31:16 +00:00
|
|
|
*/
|
|
|
|
protected function _getMaxLength($collection) {
|
|
|
|
$max = 0;
|
|
|
|
foreach ($collection as $item) {
|
|
|
|
$max = (strlen($item->name()) > $max) ? strlen($item->name()) : $max;
|
|
|
|
}
|
|
|
|
return $max;
|
2010-10-19 03:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the help as an xml string.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $string Return the SimpleXml object or a string. Defaults to true.
|
2012-11-28 22:30:47 +00:00
|
|
|
* @return string|SimpleXmlElement See $string
|
2010-10-19 03:38:14 +00:00
|
|
|
*/
|
2010-10-20 02:47:07 +00:00
|
|
|
public function xml($string = true) {
|
2010-10-20 02:44:46 +00:00
|
|
|
$parser = $this->_parser;
|
|
|
|
$xml = new SimpleXmlElement('<shell></shell>');
|
2011-04-25 19:17:59 +00:00
|
|
|
$xml->addChild('command', $parser->command());
|
2010-10-20 02:44:46 +00:00
|
|
|
$xml->addChild('description', $parser->description());
|
2011-08-16 03:55:08 +00:00
|
|
|
|
2010-10-20 02:44:46 +00:00
|
|
|
$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);
|
|
|
|
}
|
2010-10-20 02:47:07 +00:00
|
|
|
return $string ? $xml->asXml() : $xml;
|
2010-10-19 03:38:14 +00:00
|
|
|
}
|
2012-03-03 23:55:07 +00:00
|
|
|
|
2011-07-26 01:46:52 +00:00
|
|
|
}
|