diff --git a/library/Zend/Console/Getopt.php b/library/Zend/Console/Getopt.php index 031e55c7cc..2fb33d49c6 100644 --- a/library/Zend/Console/Getopt.php +++ b/library/Zend/Console/Getopt.php @@ -730,6 +730,33 @@ public function parse() $this->_parsed = true; return $this; } + + public function checkRequiredArguments(){ + + + foreach($this->_rules as $name=>$rule){ + + if($rule['param'] === 'required'){ + + $defined = false; + + foreach($rule['alias'] as $alias){ + + $defined = $defined === true ? true : array_key_exists($alias, $this->_options); + + } + if($defined === false){ + + require_once 'Zend/Console/Getopt/Exception.php'; + throw new Zend_Console_Getopt_Exception( + "Option \"$alias\" requires a parameter.", + $this->getUsageMessage()); + + } + } + } + + } /** * Parse command-line arguments for a single long option. diff --git a/tests/Zend/Console/GetoptTest.php b/tests/Zend/Console/GetoptTest.php index d143028c67..f178e2f28c 100644 --- a/tests/Zend/Console/GetoptTest.php +++ b/tests/Zend/Console/GetoptTest.php @@ -268,6 +268,50 @@ public function testGetoptUnSetBeforeParse() unset($opts->a); $this->assertFalse(isset($opts->a)); } + + public function testVerifyRequiredArgument(){ + $opts = new Zend_Console_Getopt(array( + 'apple|a=s' =>"First required argument" + )); + try { + $opts->parse(); + $opts->checkRequiredArguments(); + $this->fail('Expected to catch a Zend_Console_Getopt_Exception'); + } + catch (Exception $e){ + $this->assertTrue($e instanceof Zend_Console_Getopt_Exception, + 'Expected Zend_Console_Getopt_Exception, got '. get_class($e)); + + $this->assertEquals( 'Option "a" requires a parameter.' , $e->getMessage() ); + } + } + + public function testEmptyRequiredOption(){ + + $opts = new Zend_Console_Getopt(array( + 'apple|a=s' =>"First required argument", + 'banana|b=i' =>"Second required argument" + )); + + $opts->addArguments(array( + "-a", + "-b", + "123" + )); + + try { + $opts->parse(); + $opts->checkRequiredArguments(); + $this->fail('Expected to catch a Zend_Console_Getopt_Exception'); + + } catch (Exception $e) { + + $this->assertTrue($e instanceof Zend_Console_Getopt_Exception, + 'Expected Zend_Console_Getopt_Exception, got '. get_class($e)); + + $this->assertEquals( 'Option "a" requires a parameter.' , $e->getMessage() ); + } + } /** * @group ZF-5948