[limb-svn] r5264 - in 3.x/limb/cli: src tests/cases

svn at limb-project.com svn at limb-project.com
Fri Mar 16 14:45:59 MSK 2007


Author: pachanga
Date: 2007-03-16 14:45:59 +0300 (Fri, 16 Mar 2007)
New Revision: 5264
URL: http://fisheye.limb-project.com/changelog/limb/?cs=5264

Modified:
   3.x/limb/cli/src/lmbCliInput.class.php
   3.x/limb/cli/src/lmbCliOption.class.php
   3.x/limb/cli/tests/cases/lmbCliInputTest.class.php
Log:
-- lmbCliInput now can be constructed using short options description format, e.g: 
$cli = new lmbCliInput('i|input=;b;foo=;c='); 
is equal to:
$cli = new lmbCliInput(new lmbCliOption('i', 'input', lmbCliOption :: VALUE_REQ),
                       new lmbCliOption('b'),
                       new lmbCliOption('foo', lmbCliOption :: VALUE_REQ),
                       new lmbCliOption('c', lmbCliOption :: VALUE_REQ));
-- more strict rules for input in lmbCliInput: no options values gluing is allowed
-- arguments can be anywhere in lmbCliInput 

Modified: 3.x/limb/cli/src/lmbCliInput.class.php
===================================================================
--- 3.x/limb/cli/src/lmbCliInput.class.php	2007-03-16 11:23:59 UTC (rev 5263)
+++ 3.x/limb/cli/src/lmbCliInput.class.php	2007-03-16 11:45:59 UTC (rev 5264)
@@ -121,13 +121,42 @@
   protected function _addOptions($args)
   {
     foreach($args as $arg)
-      $this->options[] = $arg;
+    {
+      if(is_string($arg))
+        $this->options += $this->_objectify($arg);
+      elseif(is_object($arg))
+        $this->options[] = $arg;
+    }
   }
 
+  protected function _objectify($str)
+  {
+    $opts = array();
+    foreach(explode(';', $str) as $item)
+    {
+      if(preg_match('~^(?:((\w)\|(\w+))|(\w\b)|(\w+)?)(=)?~', $item, $m))
+      {
+        $req = isset($m[6]) ? lmbCliOption :: VALUE_REQ : lmbCliOption :: VALUE_NO;
+
+        if($m[1])
+          $opt = new lmbCliOption($m[2], $m[3], $req);
+        elseif($m[4])
+          $opt = new lmbCliOption($m[4], $req);
+        elseif($m[5])
+          $opt = new lmbCliOption($m[5], $req);
+
+        $opts[] = $opt;
+      }
+    }
+    return $opts;
+  }
+
   protected function _parse($argv)
   {
     $this->_reset();
 
+    $postponed_option = null;
+
     for($i=0;$i<sizeof($argv);$i++)
     {
       $arg = $argv[$i];
@@ -144,14 +173,17 @@
       }
       elseif($this->_extractShortOption($arg, $name, $value))
       {
-        $postponed_option = $this->_addShortOption($name);
+        $postponed_option = $this->_addShortOption($name, $value);
 
-        if($value)
+        if(isset($value))
         {
           if(!$postponed_option->isValueForbidden())
             $postponed_option->setValue($value);
-          elseif($this->_isArgumentNext($argv, $i))
+          elseif($this->_maybeArgumentNext($argv, $i))
+          {
             $this->arguments[] = $value;
+            $i++;
+          }
 
           unset($postponed_option);
         }
@@ -166,15 +198,13 @@
       else
       {
         $this->arguments[] = $arg;
-        if(!$this->_isArgumentNext($argv, $i))
-          break;
       }
     }
   }
 
   protected function _extractLongOption($arg, &$option, &$value = null)
   {
-    if(!preg_match('~^--([a-z]+)(=(.*))?$~', $arg, $m))
+    if(!preg_match('~^--([a-z][a-z0-9]+)(=(.*))?$~', $arg, $m))
       return false;
 
     $option = $m[1];
@@ -192,7 +222,7 @@
     return true;
   }
 
-  protected function _isArgumentNext($argv, $i)
+  protected function _maybeArgumentNext($argv, $i)
   {
     return (isset($argv[$i+1]) &&
             strpos($argv[$i+1], '-') === false);
@@ -207,23 +237,21 @@
 
   protected function _addLongOption($name)
   {
-    $option = $this->_newOption($name);
+    $option = $this->_approveOption($name);
     return $option;
   }
 
   protected function _addShortOption($name)
   {
-    $options = $this->_getGluedOptions($name, $glued_value);
-    foreach($options as $glued_option)
-      $option = $this->_newOption($glued_option);
+    list($glued, $last) = $this->_getGluedOptions($name);
+    foreach($glued as $name)
+      $this->_approveOption($name);
 
-    if($glued_value)
-      $option->setValue($glued_value);
-
+    $option = $this->_approveOption($last);
     return $option;
   }
 
-  protected function _newOption($name)
+  protected function _approveOption($name)
   {
     if(!$option = $this->getOption($name))
       throw new lmbCliException("Option '{$name}' is illegal");
@@ -232,23 +260,15 @@
     return $option;
   }
 
-  protected function _getGluedOptions($glue, &$glued_value)
+  protected function _getGluedOptions($glue)
   {
     $glued = array();
-    $glued_value = null;
+    for($j=0;$j<strlen($glue)-1;$j++)
+      $glued[] = $glue{$j};
 
-    for($j=0;$j<strlen($glue);$j++)
-    {
-      $name = $glue{$j};
+    $last = substr($glue, -1, 1);
 
-      if(!$this->getOption($name))
-      {
-        $glued_value = substr($glue, $j);
-        break;
-      }
-      $glued[] = $name;
-    }
-    return $glued;
+    return array($glued, $last);
   }
 }
 

Modified: 3.x/limb/cli/src/lmbCliOption.class.php
===================================================================
--- 3.x/limb/cli/src/lmbCliOption.class.php	2007-03-16 11:23:59 UTC (rev 5263)
+++ 3.x/limb/cli/src/lmbCliOption.class.php	2007-03-16 11:45:59 UTC (rev 5264)
@@ -1,13 +1,13 @@
 <?php
-/**
- * Limb Web Application Framework
- *
- * @link http://limb-project.com
- *
- * @copyright  Copyright &copy; 2004-2007 BIT
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html
- * @version    $Id$
- * @package    cli
+/**
+ * Limb Web Application Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id$
+ * @package    cli
  */
 lmb_require('limb/cli/src/lmbCliException.class.php');
 
@@ -23,24 +23,24 @@
   protected $value;
   protected $is_present = false;
 
-  function __construct($short_name, $long_name = null, $value_mode = self :: VALUE_NO)
+  function __construct($short_or_long_name, $long_name = null, $value_mode = self :: VALUE_NO)
   {
-    if(!is_string($long_name) && strlen($short_name) > 1)
+    if(!is_string($long_name) && strlen($short_or_long_name) > 1)
     {
-      $this->long_name = $short_name;
+      $this->long_name = $short_or_long_name;
       $this->value_mode = !is_null($long_name) ? $long_name : self :: VALUE_NO;
     }
-    elseif(!is_string($long_name) && strlen($short_name) == 1)
+    elseif(!is_string($long_name) && strlen($short_or_long_name) == 1)
     {
-      $this->short_name = $short_name;
+      $this->short_name = $short_or_long_name;
       $this->value_mode = !is_null($long_name) ? $long_name : self :: VALUE_NO;
     }
     else
     {
-      if(strlen($short_name) > strlen($long_name))
-        throw new lmbCliException("Option short name '$short_name' longer than long name '$long_name'");
+      if(strlen($short_or_long_name) > strlen($long_name))
+        throw new lmbCliException("Option short name '$short_or_long_name' longer than long name '$long_name'");
 
-      $this->short_name = $short_name;
+      $this->short_name = $short_or_long_name;
       $this->long_name = $long_name;
       $this->value_mode = $value_mode;
     }

Modified: 3.x/limb/cli/tests/cases/lmbCliInputTest.class.php
===================================================================
--- 3.x/limb/cli/tests/cases/lmbCliInputTest.class.php	2007-03-16 11:23:59 UTC (rev 5263)
+++ 3.x/limb/cli/tests/cases/lmbCliInputTest.class.php	2007-03-16 11:45:59 UTC (rev 5264)
@@ -1,13 +1,13 @@
 <?php
-/**
- * Limb Web Application Framework
- *
- * @link http://limb-project.com
- *
- * @copyright  Copyright &copy; 2004-2007 BIT
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html
- * @version    $Id$
- * @package    cli
+/**
+ * Limb Web Application Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id$
+ * @package    cli
  */
 lmb_require('limb/cli/src/lmbCliInput.class.php');
 lmb_require('limb/cli/src/lmbCliOption.class.php');
@@ -29,15 +29,26 @@
     $this->assertEqual($cli->getArgument(0, 'wow'), 'wow');
   }
 
+  function testUseStringOptionsDescription()
+  {
+    $cli = new lmbCliInput('i|input=;b;foo=;c=');
+    $opts = $cli->getOptions();
+
+    $this->assertEqual($opts[0], new lmbCliOption('i', 'input', lmbCliOption :: VALUE_REQ));
+    $this->assertEqual($opts[1], new lmbCliOption('b'));
+    $this->assertEqual($opts[2], new lmbCliOption('foo', lmbCliOption :: VALUE_REQ));
+    $this->assertEqual($opts[3], new lmbCliOption('c', lmbCliOption :: VALUE_REQ));
+  }
+
   function testReadSimpleOptionsWithArguments()
   {
     $argv = array('foo.php', '-f', 'wow', '--bar=1', 'foo', 'bar');
 
-    $cli = new lmbCliInput(new lmbCliOption('f', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('bar', lmbCliOption :: VALUE_REQ));
+    $cli = new lmbCliInput('f=;bar=');
 
     $this->assertTrue($cli->read($argv));
     $this->assertEqual($cli->getOptionValue('f'), 'wow');
+    $this->assertEqual($cli->getOptionValue('bar'), '1');
     $this->assertEqual($cli->getArguments(), array('foo', 'bar'));
   }
 
@@ -45,9 +56,7 @@
   {
     $argv = array('foo.php', '--foo', 'wow hey test', '-f', 'spaces spaces', '--bar', 1, 'foo', 'bar');
 
-    $cli = new lmbCliInput(new lmbCliOption('foo', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('bar', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('f', lmbCliOption :: VALUE_REQ));
+    $cli = new lmbCliInput('foo=;bar=;f=');
 
     $this->assertTrue($cli->read($argv));
     $this->assertEqual($cli->getOptionValue('foo'), 'wow hey test');
@@ -56,32 +65,16 @@
     $this->assertEqual($cli->getArguments(), array('foo', 'bar'));
   }
 
-  function testStopReadingOnceEnoughArgumentsStarted()
+  function testNoValueOptionValuesBecomeArguments()
   {
-    $argv = array('foo.php', '--foo=1', '-z', 'stop_here', '-b 2');
-
-    $cli = new lmbCliInput(new lmbCliOption('foo', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('z'));
-
-    $this->assertTrue($cli->read($argv));
-    $this->assertEqual($cli->getArgument(0), 'stop_here');
-    $this->assertEqual($cli->getOptionValue('foo'), 1);
-    $this->assertNull($cli->getOptionValue('z'));
-    $this->assertTrue($cli->isOptionPresent('z'));
-    $this->assertNull($cli->getOptionValue('b'));
-    $this->assertFalse($cli->isOptionPresent('b'));
-  }
-
-  function testForbiddenOptionValuesBecomeArguments()
-  {
-    $cli = new lmbCliInput(new lmbCliOption('f'));
+    $cli = new lmbCliInput('f');
     $this->assertTrue($cli->read(array('foo.php', '-f', 'foo', 'bar')));
     $this->assertEqual($cli->getArguments(), array('foo', 'bar'));
   }
 
   function testReadOptionValueRequiredError()
   {
-    $cli = new lmbCliInput(new lmbCliOption('f', 'foo', lmbCliOption :: VALUE_REQ));
+    $cli = new lmbCliInput('f|foo=');
     $cli->throwException();
 
     try
@@ -95,9 +88,9 @@
     $this->assertFalse($cli->read(array('foo.php', '-f')));
   }
 
-  function testReadOptionValueForbiddenError()
+  function testReadNoOptionValueError()
   {
-    $cli = new lmbCliInput(new lmbCliOption('f', 'foo', lmbCliOption :: VALUE_NO));
+    $cli = new lmbCliInput('f|foo');
     $cli->throwException();
 
     try
@@ -122,8 +115,7 @@
   {
     $argv = array('foo.php', '-f', 1, '--bar=4');
 
-    $cli = new lmbCliInput(new lmbCliOption('f', 'foo', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('b', 'bar', lmbCliOption :: VALUE_REQ));
+    $cli = new lmbCliInput('f|foo=;b|bar=');
 
     $this->assertTrue($cli->read($argv));
     $this->assertEqual($cli->getOptionValue('f'), 1);
@@ -136,8 +128,7 @@
   {
     $argv = array('foo.php', '--foo=1', '-b', 2);
 
-    $cli = new lmbCliInput(new lmbCliOption('f', 'foo', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('b', 'bar', lmbCliOption :: VALUE_REQ));
+    $cli = new lmbCliInput('f|foo=;b|bar=');
 
     $this->assertTrue($cli->read($argv));
     $this->assertEqual($cli->getOptionValue('f'), 1);
@@ -148,8 +139,7 @@
   {
     $argv = array('foo.php', '--foo', 1, '-b', 2);
 
-    $cli = new lmbCliInput(new lmbCliOption('f', 'foo', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('b', 'bar', lmbCliOption :: VALUE_REQ));
+    $cli = new lmbCliInput('f|foo=;b|bar=');
 
     $this->assertTrue($cli->read($argv));
     $this->assertEqual($cli->getOptionValue('f'), 1);
@@ -160,9 +150,7 @@
   {
     $argv = array('foo.php', '--foo=1', '-b', 2, '--zoo', 3);
 
-    $cli = new lmbCliInput(new lmbCliOption('f', 'foo', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('b', 'bar', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('z', 'zoo', lmbCliOption :: VALUE_REQ));
+    $cli = new lmbCliInput('f|foo=;b|bar=;z|zoo=');
 
     $this->assertTrue($cli->read($argv));
     $this->assertEqual($cli->getOptionValue('f'), 1);
@@ -170,13 +158,21 @@
     $this->assertEqual($cli->getOptionValue('z'), 3);
   }
 
+  function testReadMixedOptionsArgsComeFirst()
+  {
+    $argv = array('foo.php', 'arg1', '--opt1', 'opt1', 'arg2');
+    $cli = new lmbCliInput('opt1=');
+
+    $this->assertTrue($cli->read($argv));
+    $this->assertEqual($cli->getOptionValue('opt1'), 'opt1');
+    $this->assertEqual($cli->getArguments(), array('arg1', 'arg2'));
+  }
+
   function testShortOptionsGluing()
   {
     $argv = array('foo.php', '-ibk');
 
-    $cli = new lmbCliInput(new lmbCliOption('i'),
-                        new lmbCliOption('b'),
-                        new lmbCliOption('k'));
+    $cli = new lmbCliInput('i;b;k');
 
     $this->assertTrue($cli->read($argv));
     $this->assertTrue($cli->isOptionPresent('i'));
@@ -185,72 +181,16 @@
     $this->assertFalse($cli->isOptionPresent('z'));
   }
 
-  function testCancelOptionsGluingForNotSpecifiedOptions()
-  {
-    $cli = new lmbCliInput(new lmbCliOption('i', lmbCliOption :: VALUE_REQ));
-
-    $this->assertTrue($cli->read(array('foo.php', '-ias')));
-    $this->assertEqual($cli->getOptionValue('i'), 'as');
-
-    $this->assertTrue($cli->read(array('foo.php', '-i100')));
-    $this->assertEqual($cli->getOptionValue('i'), 100);
-  }
-
   function testOptionsGluingWithLastValue()
   {
     $argv = array('foo.php', '-ibk', 2);
 
-    $cli = new lmbCliInput(new lmbCliOption('i'),
-                        new lmbCliOption('b'),
-                        new lmbCliOption('k', lmbCliOption :: VALUE_REQ));
+    $cli = new lmbCliInput('i;b;k=');
 
     $this->assertTrue($cli->read($argv));
     $this->assertNull($cli->getOptionValue('i'));
     $this->assertNull($cli->getOptionValue('b'));
     $this->assertEqual($cli->getOptionValue('k'), 2);
   }
-
-  function testGetAllOptions()
-  {
-    $argv = array('foo.php',
-                  '--foo=1',
-                  '-b', 2,
-                  '--wow', 1,
-                  '--hey', 'value with space',
-                  '--zoo',
-                  '-i50',
-                  '-j good',
-                  '-e bad',
-                  '-ask',
-                  '-glu');
-
-    $cli = new lmbCliInput(new lmbCliOption('foo', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('b', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('zoo'),
-                        new lmbCliOption('wow', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('hey', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('i', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('j', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('e', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('a', lmbCliOption :: VALUE_REQ),
-                        new lmbCliOption('g'),
-                        new lmbCliOption('l'),
-                        new lmbCliOption('u'));
-
-    $this->assertTrue($cli->read($argv));
-
-    $this->assertEqual($cli->getOptionValue('foo'), 1);
-    $this->assertEqual($cli->getOptionValue('b'), 2);
-    $this->assertEqual($cli->getOptionValue('wow'), 1);
-    $this->assertEqual($cli->getOptionValue('hey'), 'value with space');
-    $this->assertEqual($cli->getOptionValue('zoo'), null);
-    $this->assertEqual($cli->getOptionValue('i'), 50);
-    $this->assertEqual($cli->getOptionValue('j'), 'good');
-    $this->assertEqual($cli->getOptionValue('e'), 'bad');
-    $this->assertEqual($cli->getOptionValue('a'), 'sk');
-    $this->assertEqual($cli->getOptionValue('g'), null);
-    $this->assertEqual($cli->getOptionValue('l'), null);
-    $this->assertEqual($cli->getOptionValue('u'), null);
-  }
 }
 ?>
\ No newline at end of file



More information about the limb-svn mailing list