Skip to content

Commit

Permalink
Making long options & arguments replaced with short forms.
Browse files Browse the repository at this point in the history
Fixes #1882
  • Loading branch information
markstory committed Aug 6, 2011
1 parent c1b0eb8 commit 5f84b48
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
28 changes: 26 additions & 2 deletions lib/Cake/Console/HelpFormatter.php
Expand Up @@ -29,6 +29,20 @@
* @since CakePHP(tm) v 2.0
*/
class HelpFormatter {
/**
* The maximum number of arguments shown when generating usage.
*
* @var integer
*/
protected $_maxArgs = 6;

/**
* The maximum number of options shown when generating usage.
*
* @var integer
*/
protected $_maxOptions = 6;

/**
* Build the help formatter for a an OptionParser
*
Expand Down Expand Up @@ -122,12 +136,22 @@ protected function _generateUsage() {
if (!empty($subcommands)) {
$usage[] = '[subcommand]';
}
$options = array();
foreach ($this->_parser->options() as $option) {
$usage[] = $option->usage();
$options[] = $option->usage();
}
if (count($options) > $this->_maxOptions){
$options = array('[options]');
}
$usage = array_merge($usage, $options);
$args = array();
foreach ($this->_parser->arguments() as $argument) {
$usage[] = $argument->usage();
$args[] = $argument->usage();
}
if (count($args) > $this->_maxArgs) {
$args = array('[arguments]');
}
$usage = array_merge($usage, $args);
return implode(' ', $usage);
}

Expand Down
50 changes: 49 additions & 1 deletion lib/Cake/Test/Case/Console/HelpFormatterTest.php
Expand Up @@ -210,6 +210,54 @@ public function testHelpWithOptionsAndArguments() {
$this->assertEquals($expected, $result, 'Help does not match');
}

/**
* Test that a long set of options doesn't make useless output.
*
* @return void
*/
public function testHelpWithLotsOfOptions() {
$parser = new ConsoleOptionParser('mycommand', false);
$parser
->addOption('test', array('help' => 'A test option.'))
->addOption('test2', array('help' => 'A test option.'))
->addOption('test3', array('help' => 'A test option.'))
->addOption('test4', array('help' => 'A test option.'))
->addOption('test5', array('help' => 'A test option.'))
->addOption('test6', array('help' => 'A test option.'))
->addOption('test7', array('help' => 'A test option.'))
->addArgument('model', array('help' => 'The model to make.', 'required' => true))
->addArgument('other_longer', array('help' => 'Another argument.'));

$formatter = new HelpFormatter($parser);
$result = $formatter->text();
$expected = 'cake mycommand [options] <model> [<other_longer>]';
$this->assertContains($expected, $result);
}

/**
* Test that a long set of arguments doesn't make useless output.
*
* @return void
*/
public function testHelpWithLotsOfArguments() {
$parser = new ConsoleOptionParser('mycommand', false);
$parser
->addArgument('test', array('help' => 'A test option.'))
->addArgument('test2', array('help' => 'A test option.'))
->addArgument('test3', array('help' => 'A test option.'))
->addArgument('test4', array('help' => 'A test option.'))
->addArgument('test5', array('help' => 'A test option.'))
->addArgument('test6', array('help' => 'A test option.'))
->addArgument('test7', array('help' => 'A test option.'))
->addArgument('model', array('help' => 'The model to make.', 'required' => true))
->addArgument('other_longer', array('help' => 'Another argument.'));

$formatter = new HelpFormatter($parser);
$result = $formatter->text();
$expected = 'cake mycommand [-h] [arguments]';
$this->assertContains($expected, $result);
}

/**
* test help() with options and arguments that have choices.
*
Expand Down Expand Up @@ -437,4 +485,4 @@ public function testXmlHelpAsObject() {
$result = $formatter->xml(false);
$this->assertInstanceOf('SimpleXmlElement', $result);
}
}
}

0 comments on commit 5f84b48

Please sign in to comment.