Skip to content

Commit

Permalink
Adding argument help generation in the long help and usage string.
Browse files Browse the repository at this point in the history
Fixing output of option help, formatting was calculated in 2 places.
  • Loading branch information
markstory committed Oct 14, 2010
1 parent 09adc38 commit 501e63e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
42 changes: 40 additions & 2 deletions cake/console/console_option_parser.php
Expand Up @@ -278,12 +278,26 @@ public function help() {
foreach ($this->_options as $description) {
$max = (strlen($description['name']) > $max) ? strlen($description['name']) : $max;
}
$max += 6;
$max += 8;
$out[] = '<info>Options:</info>';
$out[] = '';
foreach ($this->_options as $description) {
$out[] = $this->_optionHelp($description, $max);
}
$out[] = '';
}
if (!empty($this->_args)) {
$max = 0;
foreach ($this->_args as $description) {
$max = (strlen($description['name']) > $max) ? strlen($description['name']) : $max;
}
$max += 1;
$out[] = '<info>Arguments:</info>';
$out[] = '';
foreach ($this->_args as $description) {
$out[] = $this->_argumentHelp($description, $max);
}
$out[] = '';
}
return implode("\n", $out);
}
Expand All @@ -305,6 +319,13 @@ protected function _generateUsage() {
}
$usage[] = sprintf('[%s%s]', $name, $default);
}
foreach ($this->_args as $definition) {
$name = $definition['name'];
if (!$definition['required']) {
$name = '[' . $name . ']';
}
$usage[] = $name;
}
return implode(' ', $usage);
}

Expand All @@ -325,7 +346,24 @@ protected function _optionHelp($definition, $nameWidth) {
if (strlen($name) < $nameWidth) {
$name = str_pad($name, $nameWidth, ' ');
}
return sprintf('%s %s%s', $name, $definition['help'], $default);
return sprintf('%s%s%s', $name, $definition['help'], $default);
}

/**
* Generate the usage for a single argument.
*
* @return string
*/
protected function _argumentHelp($definition, $width) {
$name = $definition['name'];
if (strlen($name) < $width) {
$name = str_pad($name, $width, ' ');
}
$optional = '';
if (!$definition['required']) {
$optional = ' <comment>(optional)</comment>';
}
return sprintf('%s %s%s', $name, $definition['help'], $optional);
}

/**
Expand Down
37 changes: 34 additions & 3 deletions cake/tests/cases/console/console_option_parser.test.php
Expand Up @@ -242,9 +242,40 @@ function testGetHelpWithOptions() {
<info>Options:</info>
--help, -h Display this help.
--test A test option.
--connection, -c The connection to use. <comment>(default: default)</comment>
--help, -h Display this help.
--test A test option.
--connection, -c The connection to use. <comment>(default: default)</comment>
TEXT;
$this->assertEquals($expected, $result, 'Help does not match');
}

/**
* test getting help with defined options.
*
* @return void
*/
function testGetHelpWithOptionsAndArguments() {
$parser = new ConsoleOptionParser('mycommand', false);
$parser->addOption('test', array('help' => 'A test option.'))
->addArgument('model', array('help' => 'The model to make.', 'required' => true))
->addArgument('other_longer', array('help' => 'Another argument.'));

$result = $parser->help();
$expected = <<<TEXT
<info>Usage:</info>
cake mycommand [-h] [--test] model [other_longer]
<info>Options:</info>
--help, -h Display this help.
--test A test option.
<info>Arguments:</info>
model The model to make.
other_longer Another argument. <comment>(optional)</comment>
TEXT;
$this->assertEquals($expected, $result, 'Help does not match');
}
Expand Down

0 comments on commit 501e63e

Please sign in to comment.