Making long options & arguments replaced with short forms.

Fixes #1882
This commit is contained in:
Mark Story 2011-08-06 09:44:49 -04:00
parent c1b0eb8f21
commit 5f84b4846c
2 changed files with 75 additions and 3 deletions

View file

@ -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);
}

View file

@ -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);
}
}
}