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
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2005-2010, 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)
|
|
|
|
*/
|
2010-12-08 01:12:50 +00:00
|
|
|
App::uses('String', 'Utility');
|
2010-11-17 04:19:15 +00:00
|
|
|
|
2010-10-20 02:44:46 +00:00
|
|
|
/**
|
|
|
|
* 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 intergration with other tools like IDE's or other build tools.
|
|
|
|
*
|
2010-12-24 19:26:26 +00:00
|
|
|
* @package cake.console.libs
|
2010-12-22 04:11:23 +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 {
|
|
|
|
/**
|
|
|
|
* Build the help formatter for a an OptionParser
|
|
|
|
*
|
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
|
|
|
*
|
2010-10-20 01:40:27 +00:00
|
|
|
* @param integer $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)) {
|
|
|
|
$out[] = String::wrap($description, $width);
|
|
|
|
$out[] = '';
|
|
|
|
}
|
|
|
|
$out[] = '<info>Usage:</info>';
|
|
|
|
$out[] = $this->_generateUsage();
|
|
|
|
$out[] = '';
|
|
|
|
$subcommands = $parser->subcommands();
|
|
|
|
if (!empty($subcommands)) {
|
|
|
|
$out[] = '<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[] = sprintf(
|
|
|
|
__('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[] = '<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[] = '<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 favour 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]';
|
|
|
|
}
|
|
|
|
foreach ($this->_parser->options() as $option) {
|
|
|
|
$usage[] = $option->usage();
|
|
|
|
}
|
|
|
|
foreach ($this->_parser->arguments() as $argument) {
|
|
|
|
$usage[] = $argument->usage();
|
|
|
|
}
|
|
|
|
return implode(' ', $usage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterate over a collection and find the longest named thing.
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2010-10-20 02:44:46 +00:00
|
|
|
* @param boolean $string Return the SimpleXml object or a string. Defaults to true.
|
|
|
|
* @return mixed. 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>');
|
|
|
|
$xml->addChild('commmand', $parser->command());
|
|
|
|
$xml->addChild('description', $parser->description());
|
2010-10-19 03:38:14 +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
|
|
|
}
|
|
|
|
}
|