Fixing formatting on generated help in ConsoleOptionParser.

Updating tests.
Making Shell::wrapText a simple wrapper for String::wrap()
This commit is contained in:
mark_story 2010-10-15 23:09:28 -04:00
parent 98a654c4b9
commit 46dcf8b036
4 changed files with 38 additions and 35 deletions

View file

@ -491,7 +491,11 @@ class ConsoleOptionParser {
$out[] = ''; $out[] = '';
$max = $this->_getMaxLength($this->_subcommands) + 2; $max = $this->_getMaxLength($this->_subcommands) + 2;
foreach ($this->_subcommands as $command) { 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[] = '';
$out[] = sprintf( $out[] = sprintf(
@ -506,7 +510,11 @@ class ConsoleOptionParser {
$out[] = '<info>Options:</info>'; $out[] = '<info>Options:</info>';
$out[] = ''; $out[] = '';
foreach ($this->_options as $option) { 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[] = ''; $out[] = '';
} }
@ -515,7 +523,11 @@ class ConsoleOptionParser {
$out[] = '<info>Arguments:</info>'; $out[] = '<info>Arguments:</info>';
$out[] = ''; $out[] = '';
foreach ($this->_args as $argument) { 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[] = ''; $out[] = '';
} }

View file

@ -200,12 +200,12 @@ class BakeShell extends Shell {
*/ */
public function getOptionParser() { public function getOptionParser() {
$parser = parent::getOptionParser(); $parser = parent::getOptionParser();
return $parser->description(array( return $parser->description(
'The Bake script generates controllers, views and models for your application.', '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', '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', 'creation process. You can customize the generation process by telling Bake' .
'where different parts of your application are using command line arguments.' 'where different parts of your application are using command line arguments.'
))->addSubcommand('all', array( )->addSubcommand('all', array(
'help' => __('Bake a complete MVC. optional <name> of a Model'), 'help' => __('Bake a complete MVC. optional <name> of a Model'),
))->addSubcommand('project', array( ))->addSubcommand('project', array(
'help' => __('Bake a new app folder in the path supplied or in current directory if no path is specified'), 'help' => __('Bake a new app folder in the path supplied or in current directory if no path is specified'),

View file

@ -460,17 +460,10 @@ class Shell extends Object {
* @param string $text Text the text to format. * @param string $text Text the text to format.
* @param mixed $options Array of options to use, or an integer to wrap the text to. * @param mixed $options Array of options to use, or an integer to wrap the text to.
* @return string Wrapped / indented text * @return string Wrapped / indented text
* @see String::wrap()
*/ */
public function wrapText($text, $options = array()) { public function wrapText($text, $options = array()) {
$wrapped = String::wrap($text, $options); return 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;
} }
/** /**

View file

@ -643,37 +643,35 @@ TEXT;
*/ */
function testWidthFormatting() { function testWidthFormatting() {
$parser = new ConsoleOptionParser('test', false); $parser = new ConsoleOptionParser('test', false);
$parser->description(__('This is 10 This is 10')) $parser->description(__('This is fifteen This is fifteen This is fifteen'))
->addOption('four', array('help' => 'more text')) ->addOption('four', array('help' => 'this is help text this is help text'))
->addArgument('four', array('help' => 'more text')) ->addArgument('four', array('help' => 'this is help text this is help text'))
->addSubcommand('four', array('help' => 'more text')); ->addSubcommand('four', array('help' => 'this is help text this is help text'));
$result = $parser->help(null, 10); $result = $parser->help(null, 30);
$expected = <<<TEXT $expected = <<<TEXT
This is 10 This is fifteen This is
This is 10 fifteen This is fifteen
<info>Usage:</info> <info>Usage:</info>
cake test [subcommand] [-h] [--four] [<four>] cake test [subcommand] [-h] [--four] [<four>]
<info>Subcommands:</info> <info>Subcommands:</info>
four more four this is help text this
text is help text
To see help on a subcommand use <info>`cake test [subcommand] --help`</info> To see help on a subcommand use <info>`cake test [subcommand] --help`</info>
<info>Options:</info> <info>Options:</info>
--help, -h --help, -h Display this help.
Display --four this is help text
this help. this is help text
--four
more text
<info>Arguments:</info> <info>Arguments:</info>
four more four this is help text this
text is help text
<comment>(optional)</comment> <comment>(optional)</comment>
TEXT; TEXT;