From 5f90ac2b45fdf479ac36862ec80f57a1b5f56bb1 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 14 Oct 2010 23:21:56 -0400 Subject: [PATCH] Adding width wrapping to the generated help. Should help with having content that is too long. --- cake/console/console_option_parser.php | 13 +++--- .../console/console_option_parser.test.php | 42 +++++++++++++++++-- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/cake/console/console_option_parser.php b/cake/console/console_option_parser.php index ff487583b..e53707820 100644 --- a/cake/console/console_option_parser.php +++ b/cake/console/console_option_parser.php @@ -466,9 +466,10 @@ class ConsoleOptionParser { * * @param string $subcommand If present and a valid subcommand that has a linked parser. * That subcommands help will be shown instead. + * @param int $width The width to format user content to. Defaults to 72 * @return string Generated help. */ - public function help($subcommand = null) { + public function help($subcommand = null, $width = 72) { if ( isset($this->_subcommands[$subcommand]) && $this->_subcommands[$subcommand]->parser() instanceof self @@ -479,7 +480,7 @@ class ConsoleOptionParser { } $out = array(); if (!empty($this->_description)) { - $out[] = $this->_description; + $out[] = String::wrap($this->_description, $width); $out[] = ''; } $out[] = 'Usage:'; @@ -490,7 +491,7 @@ class ConsoleOptionParser { $out[] = ''; $max = $this->_getMaxLength($this->_subcommands) + 2; foreach ($this->_subcommands as $command) { - $out[] = $command->help($max); + $out[] = String::wrap($command->help($max), $width); } $out[] = ''; $out[] = sprintf( @@ -505,7 +506,7 @@ class ConsoleOptionParser { $out[] = 'Options:'; $out[] = ''; foreach ($this->_options as $option) { - $out[] = $option->help($max); + $out[] = String::wrap($option->help($max), $width); } $out[] = ''; } @@ -514,12 +515,12 @@ class ConsoleOptionParser { $out[] = 'Arguments:'; $out[] = ''; foreach ($this->_args as $argument) { - $out[] = $argument->help($max); + $out[] = String::wrap($argument->help($max), $width); } $out[] = ''; } if (!empty($this->_epilog)) { - $out[] = $this->_epilog; + $out[] = String::wrap($this->_epilog, $width); } return implode("\n", $out); } diff --git a/cake/tests/cases/console/console_option_parser.test.php b/cake/tests/cases/console/console_option_parser.test.php index 9f907242a..5c67ac703 100644 --- a/cake/tests/cases/console/console_option_parser.test.php +++ b/cake/tests/cases/console/console_option_parser.test.php @@ -33,7 +33,7 @@ class ConsoleOptionParserTest extends CakeTestCase { $this->assertEquals($parser, $result, 'Setting description is not chainable'); $this->assertEquals('A test', $parser->description(), 'getting value is wrong.'); - + $result = $parser->description(array('A test', 'something')); $this->assertEquals("A test\nsomething", $parser->description(), 'getting value is wrong.'); } @@ -387,7 +387,8 @@ cake mycommand [-h] [--test] [-c default] --help, -h Display this help. --test A test option. ---connection, -c The connection to use. (default: default) +--connection, -c The connection to use. (default: +default) TEXT; $this->assertEquals($expected, $result, 'Help does not match'); @@ -641,7 +642,42 @@ TEXT; * @return void */ function testWidthFormatting() { - $this->markTestIncomplete('Width formatting is not done.'); + $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); + $expected = <<Usage: +cake test [subcommand] [-h] [--four] [] + +Subcommands: + +four more +text + +To see help on a subcommand use `cake test [subcommand] --help` + +Options: + +--help, -h + Display +this help. +--four + more text + +Arguments: + +four more +text +(optional) + +TEXT; + $this->assertEquals($expected, $result, 'Generated help is too wide'); } } \ No newline at end of file