Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit dae96bd

Browse files
committed
Merge pull request zendframework#454 from a-lucas/master
Zend_Console_Getopt: Missing required parameter consumes next option as its parameter value
2 parents 7c073ca + 0fc9584 commit dae96bd

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

library/Zend/Console/Getopt.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,24 @@ public function parse()
730730
$this->_parsed = true;
731731
return $this;
732732
}
733+
734+
public function checkRequiredArguments()
735+
{
736+
foreach ($this->_rules as $name=>$rule){
737+
if ($rule['param'] === 'required'){
738+
$defined = false;
739+
foreach ($rule['alias'] as $alias){
740+
$defined = $defined === true ? true : array_key_exists($alias, $this->_options);
741+
}
742+
if ($defined === false){
743+
require_once 'Zend/Console/Getopt/Exception.php';
744+
throw new Zend_Console_Getopt_Exception("Option \"$alias\" requires a parameter.", $this->getUsageMessage());
745+
746+
}
747+
}
748+
}
749+
750+
}
733751

734752
/**
735753
* Parse command-line arguments for a single long option.
@@ -789,7 +807,7 @@ protected function _parseSingleOption($flag, &$argv)
789807
$realFlag = $this->_ruleMap[$flag];
790808
switch ($this->_rules[$realFlag]['param']) {
791809
case 'required':
792-
if (count($argv) > 0) {
810+
if (count($argv) > 0 && substr($argv[0], 0, 1) != '-') {
793811
$param = array_shift($argv);
794812
$this->_checkParameterType($realFlag, $param);
795813
} else {

tests/Zend/Console/GetoptTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,43 @@ public function testGetoptUnSetBeforeParse()
268268
unset($opts->a);
269269
$this->assertFalse(isset($opts->a));
270270
}
271+
272+
public function testVerifyRequiredArgument()
273+
{
274+
$opts = new Zend_Console_Getopt(array('apple|a=s' => "First required argument"));
275+
try {
276+
$opts->parse();
277+
$opts->checkRequiredArguments();
278+
$this->fail('Expected to catch a Zend_Console_Getopt_Exception');
279+
}
280+
catch (Zend_Exception $e){
281+
$this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
282+
'Expected Zend_Console_Getopt_Exception, got '. get_class($e));
283+
$this->assertEquals('Option "a" requires a parameter.' , $e->getMessage());
284+
}
285+
286+
$opts->addArguments(array( "-a", "apple") );
287+
$opts->parse();
288+
$opts->checkRequiredArguments();//-> no Exception here
289+
}
290+
291+
public function testEmptyRequiredOption()
292+
{
293+
$opts = new Zend_Console_Getopt(array(
294+
'apple|a=s' =>"First required argument",
295+
'banana|b=i' =>"Second required argument"
296+
));
297+
$opts->addArguments(array("-a","-b","123"));
298+
try {
299+
$opts->parse();
300+
$opts->checkRequiredArguments();
301+
$this->fail('Expected to catch a Zend_Console_Getopt_Exception');
302+
} catch (Zend_Exception $e) {
303+
$this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
304+
'Expected Zend_Console_Getopt_Exception, got '. get_class($e));
305+
$this->assertEquals('Option "a" requires a parameter.' , $e->getMessage());
306+
}
307+
}
271308

272309
/**
273310
* @group ZF-5948

0 commit comments

Comments
 (0)