Merge pull request #10916 from tenkoma/2.x-console-unknown-option

2.x Console: Display error message when unknown option is specified
This commit is contained in:
Mark Story 2017-07-21 21:47:22 -04:00 committed by GitHub
commit 1eb4b0fdf7
2 changed files with 27 additions and 0 deletions

View file

@ -431,6 +431,7 @@ class Shell extends CakeObject {
try { try {
list($this->params, $this->args) = $this->OptionParser->parse($argv, $command); list($this->params, $this->args) = $this->OptionParser->parse($argv, $command);
} catch (ConsoleException $e) { } catch (ConsoleException $e) {
$this->err(__d('cake_console', '<error>Error:</error> %s', $e->getMessage()));
$this->out($this->OptionParser->help($command)); $this->out($this->OptionParser->help($command));
return false; return false;
} }

View file

@ -778,6 +778,32 @@ class ShellTest extends CakeTestCase {
$this->assertFalse($result); $this->assertFalse($result);
} }
/**
* test unknown option causes display of error and help.
*
* @return void
*/
public function testRunCommandUnknownOption() {
$output = $this->getMock('ConsoleOutput', array(), array(), '', false);
$error = $this->getMock('ConsoleOutput', array(), array(), '', false);
$in = $this->getMock('ConsoleInput', array(), array(), '', false);
$Parser = $this->getMock('ConsoleOptionParser', array(), array(), '', false);
$Parser->expects($this->once())->method('parse')
->with(array('--unknown'))
->will($this->throwException(new ConsoleException('Unknown option `unknown`')));
$Parser->expects($this->once())->method('help');
$Shell = $this->getMock('ShellTestShell', array('getOptionParser'), array($output, $error, $in));
$Shell->expects($this->once())->method('getOptionParser')
->will($this->returnValue($Parser));
$Shell->stderr->expects($this->once())->method('write');
$Shell->stdout->expects($this->once())->method('write');
$Shell->runCommand('do_something', array('do_something', '--unknown'));
}
/** /**
* test that a --help causes help to show. * test that a --help causes help to show.
* *