Skip to content
Permalink
Browse files

Adding width wrapping to the generated help. Should help with having …

…content that is too long.
  • Loading branch information...
markstory committed Oct 15, 2010
1 parent 7f70c8a commit 5f90ac2b45fdf479ac36862ec80f57a1b5f56bb1
Showing with 46 additions and 9 deletions.
  1. +7 −6 cake/console/console_option_parser.php
  2. +39 −3 cake/tests/cases/console/console_option_parser.test.php
@@ -466,9 +466,10 @@ public function parse($argv, $command = null) {
*
* @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 @@ public function help($subcommand = null) {
}
$out = array();
if (!empty($this->_description)) {
$out[] = $this->_description;
$out[] = String::wrap($this->_description, $width);
$out[] = '';
}
$out[] = '<info>Usage:</info>';
@@ -490,7 +491,7 @@ public function help($subcommand = null) {
$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 @@ public function help($subcommand = null) {
$out[] = '<info>Options:</info>';
$out[] = '';
foreach ($this->_options as $option) {
$out[] = $option->help($max);
$out[] = String::wrap($option->help($max), $width);
}
$out[] = '';
}
@@ -514,12 +515,12 @@ public function help($subcommand = null) {
$out[] = '<info>Arguments:</info>';
$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);
}
@@ -33,7 +33,7 @@ function testDescription() {
$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 @@ function testHelpWithOptions() {
--help, -h Display this help.
--test A test option.
--connection, -c The connection to use. <comment>(default: default)</comment>
--connection, -c The connection to use. <comment>(default:
default)</comment>
TEXT;
$this->assertEquals($expected, $result, 'Help does not match');
@@ -641,7 +642,42 @@ function testParsingWithSubParser() {
* @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 = <<<TEXT
This is 10
This is 10
<info>Usage:</info>
cake test [subcommand] [-h] [--four] [<four>]
<info>Subcommands:</info>
four more
text
To see help on a subcommand use <info>`cake test [subcommand] --help`</info>
<info>Options:</info>
--help, -h
Display
this help.
--four
more text
<info>Arguments:</info>
four more
text
<comment>(optional)</comment>
TEXT;
$this->assertEquals($expected, $result, 'Generated help is too wide');
}
}

0 comments on commit 5f90ac2

Please sign in to comment.
You can’t perform that action at this time.