From 46dcf8b036bb56f84955e10fa7eeda19eab7fa6c Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 15 Oct 2010 23:09:28 -0400 Subject: [PATCH] Fixing formatting on generated help in ConsoleOptionParser. Updating tests. Making Shell::wrapText a simple wrapper for String::wrap() --- cake/console/console_option_parser.php | 18 ++++++++-- cake/console/libs/bake.php | 10 +++--- cake/console/libs/shell.php | 11 ++---- .../console/console_option_parser.test.php | 34 +++++++++---------- 4 files changed, 38 insertions(+), 35 deletions(-) diff --git a/cake/console/console_option_parser.php b/cake/console/console_option_parser.php index e53707820..1c055cfab 100644 --- a/cake/console/console_option_parser.php +++ b/cake/console/console_option_parser.php @@ -491,7 +491,11 @@ class ConsoleOptionParser { $out[] = ''; $max = $this->_getMaxLength($this->_subcommands) + 2; foreach ($this->_subcommands as $command) { - $out[] = String::wrap($command->help($max), $width); + $out[] = String::wrap($command->help($max), array( + 'width' => $width, + 'indent' => str_repeat(' ', $max), + 'indentAt' => 1 + )); } $out[] = ''; $out[] = sprintf( @@ -506,7 +510,11 @@ class ConsoleOptionParser { $out[] = 'Options:'; $out[] = ''; foreach ($this->_options as $option) { - $out[] = String::wrap($option->help($max), $width); + $out[] = String::wrap($option->help($max), array( + 'width' => $width, + 'indent' => str_repeat(' ', $max), + 'indentAt' => 1 + )); } $out[] = ''; } @@ -515,7 +523,11 @@ class ConsoleOptionParser { $out[] = 'Arguments:'; $out[] = ''; foreach ($this->_args as $argument) { - $out[] = String::wrap($argument->help($max), $width); + $out[] = String::wrap($argument->help($max), array( + 'width' => $width, + 'indent' => str_repeat(' ', $max), + 'indentAt' => 1 + )); } $out[] = ''; } diff --git a/cake/console/libs/bake.php b/cake/console/libs/bake.php index cea5e6bc7..6c3c02cd2 100644 --- a/cake/console/libs/bake.php +++ b/cake/console/libs/bake.php @@ -200,12 +200,12 @@ class BakeShell extends Shell { */ public function getOptionParser() { $parser = parent::getOptionParser(); - return $parser->description(array( - 'The Bake script generates controllers, views and models for your application.', - 'If run with no command line arguments, Bake guides the user through the class', - 'creation process. You can customize the generation process by telling Bake', + return $parser->description( + 'The Bake script generates controllers, views and models for your application.' . + 'If run with no command line arguments, Bake guides the user through the class' . + 'creation process. You can customize the generation process by telling Bake' . 'where different parts of your application are using command line arguments.' - ))->addSubcommand('all', array( + )->addSubcommand('all', array( 'help' => __('Bake a complete MVC. optional of a Model'), ))->addSubcommand('project', array( 'help' => __('Bake a new app folder in the path supplied or in current directory if no path is specified'), diff --git a/cake/console/libs/shell.php b/cake/console/libs/shell.php index 8b213d0b6..67307dace 100644 --- a/cake/console/libs/shell.php +++ b/cake/console/libs/shell.php @@ -460,17 +460,10 @@ class Shell extends Object { * @param string $text Text the text to format. * @param mixed $options Array of options to use, or an integer to wrap the text to. * @return string Wrapped / indented text + * @see String::wrap() */ public function wrapText($text, $options = array()) { - $wrapped = String::wrap($text, $options); - if (is_array($options) && !empty($options['indent'])) { - $chunks = explode("\n", $wrapped); - foreach ($chunks as &$chunk) { - $chunk = $options['indent'] . $chunk; - } - return implode("\n", $chunks); - } - return $wrapped; + return String::wrap($text, $options); } /** diff --git a/cake/tests/cases/console/console_option_parser.test.php b/cake/tests/cases/console/console_option_parser.test.php index 5c67ac703..1e64c5eee 100644 --- a/cake/tests/cases/console/console_option_parser.test.php +++ b/cake/tests/cases/console/console_option_parser.test.php @@ -388,7 +388,7 @@ cake mycommand [-h] [--test] [-c default] --help, -h Display this help. --test A test option. --connection, -c The connection to use. (default: -default) + default) TEXT; $this->assertEquals($expected, $result, 'Help does not match'); @@ -643,38 +643,36 @@ TEXT; */ function testWidthFormatting() { $parser = new ConsoleOptionParser('test', false); - $parser->description(__('This is 10 This is 10')) - ->addOption('four', array('help' => 'more text')) - ->addArgument('four', array('help' => 'more text')) - ->addSubcommand('four', array('help' => 'more text')); - $result = $parser->help(null, 10); + $parser->description(__('This is fifteen This is fifteen This is fifteen')) + ->addOption('four', array('help' => 'this is help text this is help text')) + ->addArgument('four', array('help' => 'this is help text this is help text')) + ->addSubcommand('four', array('help' => 'this is help text this is help text')); + $result = $parser->help(null, 30); $expected = <<Usage: cake test [subcommand] [-h] [--four] [] Subcommands: -four more -text +four this is help text this + is help text To see help on a subcommand use `cake test [subcommand] --help` Options: ---help, -h - Display -this help. ---four - more text +--help, -h Display this help. +--four this is help text + this is help text Arguments: -four more -text -(optional) +four this is help text this + is help text + (optional) TEXT; $this->assertEquals($expected, $result, 'Generated help is too wide');