Permalink
Browse files

Making long options & arguments replaced with short forms.

Fixes #1882
  • Loading branch information...
1 parent c1b0eb8 commit 5f84b4846c0c95a745d637ae6ee6ca28f0cc21e1 @markstory markstory committed Aug 6, 2011
Showing with 75 additions and 3 deletions.
  1. +26 −2 lib/Cake/Console/HelpFormatter.php
  2. +49 −1 lib/Cake/Test/Case/Console/HelpFormatterTest.php
@@ -30,6 +30,20 @@
*/
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
*
* @param ConsoleOptionParser $parser The option parser help is being generated for.
@@ -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);
}
@@ -211,6 +211,54 @@ public function testHelpWithOptionsAndArguments() {
}
/**
+ * 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.
*
* @return void
@@ -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.