Skip to content
This repository has been archived by the owner on May 16, 2018. It is now read-only.

Zend_Console_Getopt: Missing required parameter consumes next option as its parameter value #454

Merged
merged 4 commits into from
Nov 13, 2014
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion 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(){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the http://framework.zend.com/manual/1.12/en/coding-standard.coding-style.html

  • newline before method opening curly brace
  • space after if
  • space before statement opening curly brace
  • ...



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 Expand Up @@ -789,7 +816,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 {
Expand Down
48 changes: 48 additions & 0 deletions tests/Zend/Console/GetoptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,54 @@ 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() );
}

$opts->addArguments( array( "-a", "apple") );
$opts->parse();
$opts->checkRequiredArguments();//-> no Exception here
}

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