Просмотр исходного кода

Merge pull request #454 from a-lucas/master

Zend_Console_Getopt: Missing required parameter consumes next option as its parameter value
Frank Brückner 11 лет назад
Родитель
Сommit
dae96bda22
2 измененных файлов с 56 добавлено и 1 удалено
  1. 19 1
      library/Zend/Console/Getopt.php
  2. 37 0
      tests/Zend/Console/GetoptTest.php

+ 19 - 1
library/Zend/Console/Getopt.php

@@ -730,6 +730,24 @@ class Zend_Console_Getopt
         $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.
@@ -789,7 +807,7 @@ class Zend_Console_Getopt
         $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 {

+ 37 - 0
tests/Zend/Console/GetoptTest.php

@@ -268,6 +268,43 @@ class Zend_Console_GetoptTest extends PHPUnit_Framework_TestCase
         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 (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());
+        }        
+        
+        $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 (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());
+         }
+    }
 
     /**
      * @group ZF-5948