Skip to content

Commit

Permalink
(fix): Bug Fix zendframework#377
Browse files Browse the repository at this point in the history
  • Loading branch information
a-lucas authored and Dimitris Giotas committed Jun 17, 2016
1 parent cb179ed commit 1d18164
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
27 changes: 27 additions & 0 deletions library/Zend/Console/Getopt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
44 changes: 44 additions & 0 deletions tests/Zend/Console/GetoptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1d18164

Please sign in to comment.