From 79ce9f8d35c62d7bd0ef7149b30aae71ae542bce Mon Sep 17 00:00:00 2001 From: Antione LUCAS Date: Mon, 3 Nov 2014 11:40:48 +0800 Subject: [PATCH 1/4] (fix): default: --- library/Zend/Console/Getopt.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Zend/Console/Getopt.php b/library/Zend/Console/Getopt.php index 20a5366a0a..031e55c7cc 100644 --- a/library/Zend/Console/Getopt.php +++ b/library/Zend/Console/Getopt.php @@ -789,7 +789,7 @@ protected function _parseSingleOption($flag, &$argv) $realFlag = $this->_ruleMap[$flag]; switch ($this->_rules[$realFlag]['param']) { case 'required': - if (count($argv) > 0) { + if (count($argv) > 0 && substr($argv[0], 0, 1) != '-') { $param = array_shift($argv); $this->_checkParameterType($realFlag, $param); } else { From 4613a810a15447483b2d0c7797fd9eb108397774 Mon Sep 17 00:00:00 2001 From: Antione LUCAS Date: Mon, 3 Nov 2014 13:14:14 +0800 Subject: [PATCH 2/4] (fix): Bug Fix #377 --- library/Zend/Console/Getopt.php | 27 +++++++++++++++++++ tests/Zend/Console/GetoptTest.php | 44 +++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) 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 From 1a4053df8f2ed79b8bd202c479fb094b9f5a7a0b Mon Sep 17 00:00:00 2001 From: Antione LUCAS Date: Mon, 3 Nov 2014 13:20:46 +0800 Subject: [PATCH 3/4] testing the verifyRequiredArgumner : add more test --- tests/Zend/Console/GetoptTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Zend/Console/GetoptTest.php b/tests/Zend/Console/GetoptTest.php index f178e2f28c..ad0758a183 100644 --- a/tests/Zend/Console/GetoptTest.php +++ b/tests/Zend/Console/GetoptTest.php @@ -284,6 +284,10 @@ public function testVerifyRequiredArgument(){ $this->assertEquals( 'Option "a" requires a parameter.' , $e->getMessage() ); } + + $opts->addArguments( array( "-a", "apple") ); + $opts->parse(); + $opts->checkRequiredArguments();//-> no Exception here } public function testEmptyRequiredOption(){ From 0fc9584d35bba1097e4fb8e7bbfde1d399fde312 Mon Sep 17 00:00:00 2001 From: Antoine A Lucas Date: Tue, 4 Nov 2014 10:45:35 +0800 Subject: [PATCH 4/4] (fix): Formatting style checked and fixed according to http://framework.zend.com/manual/1.12/en/coding-standard.coding-style.html --- library/Zend/Console/Getopt.php | 27 +++++++------------- tests/Zend/Console/GetoptTest.php | 41 +++++++++++-------------------- 2 files changed, 24 insertions(+), 44 deletions(-) diff --git a/library/Zend/Console/Getopt.php b/library/Zend/Console/Getopt.php index 2fb33d49c6..4746218d16 100644 --- a/library/Zend/Console/Getopt.php +++ b/library/Zend/Console/Getopt.php @@ -731,26 +731,17 @@ public function parse() 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); - + 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){ - + if ($defined === false){ require_once 'Zend/Console/Getopt/Exception.php'; - throw new Zend_Console_Getopt_Exception( - "Option \"$alias\" requires a parameter.", - $this->getUsageMessage()); + throw new Zend_Console_Getopt_Exception("Option \"$alias\" requires a parameter.", $this->getUsageMessage()); } } diff --git a/tests/Zend/Console/GetoptTest.php b/tests/Zend/Console/GetoptTest.php index ad0758a183..c97a470f4b 100644 --- a/tests/Zend/Console/GetoptTest.php +++ b/tests/Zend/Console/GetoptTest.php @@ -269,51 +269,40 @@ public function testGetoptUnSetBeforeParse() $this->assertFalse(isset($opts->a)); } - public function testVerifyRequiredArgument(){ - $opts = new Zend_Console_Getopt(array( - 'apple|a=s' =>"First required argument" - )); + 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){ + catch (Zend_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() ); + 'Expected Zend_Console_Getopt_Exception, got '. get_class($e)); + $this->assertEquals('Option "a" requires a parameter.' , $e->getMessage()); } - $opts->addArguments( array( "-a", "apple") ); + $opts->addArguments(array( "-a", "apple") ); $opts->parse(); $opts->checkRequiredArguments();//-> no Exception here } - public function testEmptyRequiredOption(){ - + 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" - )); - + )); + $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->fail('Expected to catch a Zend_Console_Getopt_Exception'); + } catch (Zend_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() ); + 'Expected Zend_Console_Getopt_Exception, got '. get_class($e)); + $this->assertEquals('Option "a" requires a parameter.' , $e->getMessage()); } }