mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Adding required argument checking.
This commit is contained in:
parent
505e59ac66
commit
f5ad54e97e
2 changed files with 49 additions and 4 deletions
|
@ -28,12 +28,36 @@
|
|||
*/
|
||||
class ConsoleOptionParser {
|
||||
|
||||
/**
|
||||
* Description text - displays before options when help is generated
|
||||
*
|
||||
* @see ConsoleOptionParser::description()
|
||||
* @var string
|
||||
*/
|
||||
protected $_description = null;
|
||||
|
||||
/**
|
||||
* Epilog text - displays after options when help is generated
|
||||
*
|
||||
* @see ConsoleOptionParser::epilog()
|
||||
* @var string
|
||||
*/
|
||||
protected $_epilog = null;
|
||||
|
||||
/**
|
||||
* Option definitions.
|
||||
*
|
||||
* @see ConsoleOptionParser::addOption()
|
||||
* @var array
|
||||
*/
|
||||
protected $_options = array();
|
||||
|
||||
/**
|
||||
* Positional argument definitions.
|
||||
*
|
||||
* @see ConsoleOptionParser::addArgument()
|
||||
* @var array
|
||||
*/
|
||||
protected $_args = array();
|
||||
|
||||
/**
|
||||
|
@ -195,6 +219,13 @@ class ConsoleOptionParser {
|
|||
$args = $this->_parseArg($token, $args);
|
||||
}
|
||||
}
|
||||
foreach ($this->_args as $i => $arg) {
|
||||
if ($arg['required'] && !isset($args[$i])) {
|
||||
throw new RuntimeException(
|
||||
sprintf(__('Missing required arguments. %s is required.'), $arg['name'])
|
||||
);
|
||||
}
|
||||
}
|
||||
return array($params, $args);
|
||||
}
|
||||
|
||||
|
|
|
@ -197,7 +197,7 @@ class ConsoleOptionParserTest extends CakeTestCase {
|
|||
* @expectedException InvalidArgumentException
|
||||
* @return void
|
||||
*/
|
||||
function testParseArgument() {
|
||||
function testParseArgumentTooMany() {
|
||||
$parser = new ConsoleOptionParser();
|
||||
$parser->addArgument('name', array('help' => 'An argument'))
|
||||
->addArgument('other');
|
||||
|
@ -208,4 +208,18 @@ class ConsoleOptionParserTest extends CakeTestCase {
|
|||
|
||||
$result = $parser->parse(array('one', 'two', 'three'));
|
||||
}
|
||||
|
||||
/**
|
||||
* test that when there are not enough arguments an exception is raised
|
||||
*
|
||||
* @expectedException RuntimeException
|
||||
* @return void
|
||||
*/
|
||||
function testPositionalArgNotEnough() {
|
||||
$parser = new ConsoleOptionParser();
|
||||
$parser->addArgument('name', array('required' => true))
|
||||
->addArgument('other', array('required' => true));
|
||||
|
||||
$parser->parse(array('one'));
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue