diff --git a/lib/Cake/Console/HelpFormatter.php b/lib/Cake/Console/HelpFormatter.php index b5667f0aa..88f73caa5 100644 --- a/lib/Cake/Console/HelpFormatter.php +++ b/lib/Cake/Console/HelpFormatter.php @@ -29,6 +29,20 @@ App::uses('String', 'Utility'); * @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 * @@ -122,12 +136,22 @@ class HelpFormatter { 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); } diff --git a/lib/Cake/Test/Case/Console/HelpFormatterTest.php b/lib/Cake/Test/Case/Console/HelpFormatterTest.php index 718db6818..60598c7b6 100644 --- a/lib/Cake/Test/Case/Console/HelpFormatterTest.php +++ b/lib/Cake/Test/Case/Console/HelpFormatterTest.php @@ -210,6 +210,54 @@ TEXT; $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. * @@ -437,4 +485,4 @@ TEXT; $result = $formatter->xml(false); $this->assertInstanceOf('SimpleXmlElement', $result); } -} \ No newline at end of file +}