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

Commit 4613a81

Browse files
committed
(fix): Bug Fix zendframework#377
1 parent 79ce9f8 commit 4613a81

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

library/Zend/Console/Getopt.php

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

734761
/**
735762
* Parse command-line arguments for a single long option.

tests/Zend/Console/GetoptTest.php

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

272316
/**
273317
* @group ZF-5948

0 commit comments

Comments
 (0)