HelpFormatter.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * HelpFormatter
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. App::uses('String', 'Utility');
  18. /**
  19. * HelpFormatter formats help for console shells. Can format to either
  20. * text or XML formats. Uses ConsoleOptionParser methods to generate help.
  21. *
  22. * Generally not directly used. Using $parser->help($command, 'xml'); is usually
  23. * how you would access help. Or via the `--help=xml` option on the command line.
  24. *
  25. * Xml output is useful for integration with other tools like IDE's or other build tools.
  26. *
  27. * @package cake.console.libs
  28. * @since CakePHP(tm) v 2.0
  29. */
  30. class HelpFormatter {
  31. /**
  32. * Build the help formatter for a an OptionParser
  33. *
  34. * @param ConsoleOptionParser $parser The option parser help is being generated for.
  35. */
  36. public function __construct(ConsoleOptionParser $parser) {
  37. $this->_parser = $parser;
  38. }
  39. /**
  40. * Get the help as formatted text suitable for output on the command line.
  41. *
  42. * @param integer $width The width of the help output.
  43. * @return string
  44. */
  45. public function text($width = 72) {
  46. $parser = $this->_parser;
  47. $out = array();
  48. $description = $parser->description();
  49. if (!empty($description)) {
  50. $out[] = String::wrap($description, $width);
  51. $out[] = '';
  52. }
  53. $out[] = __d('cake_console', '<info>Usage:</info>');
  54. $out[] = $this->_generateUsage();
  55. $out[] = '';
  56. $subcommands = $parser->subcommands();
  57. if (!empty($subcommands)) {
  58. $out[] = __d('cake_console', '<info>Subcommands:</info>');
  59. $out[] = '';
  60. $max = $this->_getMaxLength($subcommands) + 2;
  61. foreach ($subcommands as $command) {
  62. $out[] = String::wrap($command->help($max), array(
  63. 'width' => $width,
  64. 'indent' => str_repeat(' ', $max),
  65. 'indentAt' => 1
  66. ));
  67. }
  68. $out[] = '';
  69. $out[] = __d('cake_console', 'To see help on a subcommand use <info>`cake %s [subcommand] --help`</info>', $parser->command());
  70. $out[] = '';
  71. }
  72. $options = $parser->options();
  73. if (!empty($options)) {
  74. $max = $this->_getMaxLength($options) + 8;
  75. $out[] = __d('cake_console', '<info>Options:</info>');
  76. $out[] = '';
  77. foreach ($options as $option) {
  78. $out[] = String::wrap($option->help($max), array(
  79. 'width' => $width,
  80. 'indent' => str_repeat(' ', $max),
  81. 'indentAt' => 1
  82. ));
  83. }
  84. $out[] = '';
  85. }
  86. $arguments = $parser->arguments();
  87. if (!empty($arguments)) {
  88. $max = $this->_getMaxLength($arguments) + 2;
  89. $out[] = __d('cake_console', '<info>Arguments:</info>');
  90. $out[] = '';
  91. foreach ($arguments as $argument) {
  92. $out[] = String::wrap($argument->help($max), array(
  93. 'width' => $width,
  94. 'indent' => str_repeat(' ', $max),
  95. 'indentAt' => 1
  96. ));
  97. }
  98. $out[] = '';
  99. }
  100. $epilog = $parser->epilog();
  101. if (!empty($epilog)) {
  102. $out[] = String::wrap($epilog, $width);
  103. $out[] = '';
  104. }
  105. return implode("\n", $out);
  106. }
  107. /**
  108. * Generate the usage for a shell based on its arguments and options.
  109. * Usage strings favor short options over the long ones. and optional args will
  110. * be indicated with []
  111. *
  112. * @return string
  113. */
  114. protected function _generateUsage() {
  115. $usage = array('cake ' . $this->_parser->command());
  116. $subcommands = $this->_parser->subcommands();
  117. if (!empty($subcommands)) {
  118. $usage[] = '[subcommand]';
  119. }
  120. foreach ($this->_parser->options() as $option) {
  121. $usage[] = $option->usage();
  122. }
  123. foreach ($this->_parser->arguments() as $argument) {
  124. $usage[] = $argument->usage();
  125. }
  126. return implode(' ', $usage);
  127. }
  128. /**
  129. * Iterate over a collection and find the longest named thing.
  130. *
  131. * @return integer
  132. */
  133. protected function _getMaxLength($collection) {
  134. $max = 0;
  135. foreach ($collection as $item) {
  136. $max = (strlen($item->name()) > $max) ? strlen($item->name()) : $max;
  137. }
  138. return $max;
  139. }
  140. /**
  141. * Get the help as an xml string.
  142. *
  143. * @param boolean $string Return the SimpleXml object or a string. Defaults to true.
  144. * @return mixed. See $string
  145. */
  146. public function xml($string = true) {
  147. $parser = $this->_parser;
  148. $xml = new SimpleXmlElement('<shell></shell>');
  149. $xml->addChild('command', $parser->command());
  150. $xml->addChild('description', $parser->description());
  151. $xml->addChild('epilog', $parser->epilog());
  152. $subcommands = $xml->addChild('subcommands');
  153. foreach ($parser->subcommands() as $command) {
  154. $command->xml($subcommands);
  155. }
  156. $options = $xml->addChild('options');
  157. foreach ($parser->options() as $option) {
  158. $option->xml($options);
  159. }
  160. $arguments = $xml->addChild('arguments');
  161. foreach ($parser->arguments() as $argument) {
  162. $argument->xml($arguments);
  163. }
  164. return $string ? $xml->asXml() : $xml;
  165. }
  166. }