Skip to content

Commit

Permalink
Adding width wrapping to the generated help. Should help with having …
Browse files Browse the repository at this point in the history
…content that is too long.
  • Loading branch information
markstory committed Oct 15, 2010
1 parent 7f70c8a commit 5f90ac2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
13 changes: 7 additions & 6 deletions cake/console/console_option_parser.php
Expand Up @@ -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
Expand All @@ -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>';
Expand All @@ -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(
Expand All @@ -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[] = '';
}
Expand All @@ -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);
}
Expand Down
42 changes: 39 additions & 3 deletions cake/tests/cases/console/console_option_parser.test.php
Expand Up @@ -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.');
}
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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.