Adding width wrapping to the generated help. Should help with having content that is too long.

This commit is contained in:
mark_story 2010-10-14 23:21:56 -04:00
parent 7f70c8a95d
commit 5f90ac2b45
2 changed files with 46 additions and 9 deletions

View file

@ -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[] = '<info>Usage:</info>';
@ -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[] = '<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 @@ class ConsoleOptionParser {
$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);
}

View file

@ -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. <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 @@ 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 = <<<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');
}
}