support --long-opt=arg
[pharext/pharext] / src / pharext / CliArgs.php
index 1f07e4463ead32319d6dd641feae46badc9104ac..99992bd321cca327e2b2e6e72300dd97c9b0c941 100644 (file)
@@ -51,7 +51,7 @@ class CliArgs implements \ArrayAccess
         * Original option spec
         * @var array
         */
-       private $orig;
+       private $orig = [];
        
        /**
         * Compiled spec
@@ -79,10 +79,11 @@ class CliArgs implements \ArrayAccess
         * @return pharext\CliArgs self
         */
        public function compile(array $spec = null) {
-               $this->orig = $spec;
-               $this->spec = [];
+               $this->orig = array_merge($this->orig, $spec);
                foreach ((array) $spec as $arg) {
-                       $this->spec["-".$arg[0]] = $arg;
+                       if (isset($arg[0])) { 
+                               $this->spec["-".$arg[0]] = $arg;
+                       }
                        $this->spec["--".$arg[1]] = $arg;
                }
                return $this;
@@ -126,6 +127,13 @@ class CliArgs implements \ArrayAccess
                                        return "-$s";
                                }, str_split(substr($o, 1))));
                                $o = $argv[$i];
+                       } elseif ($o{0} === '-' && strlen($o) > 2 && $o{1} === '-' && 0 < ($eq = strpos($o, "="))) {
+                               $argc++;
+                               array_splice($argv, $i, 1, [
+                                       substr($o, 0, $eq++),
+                                       substr($o, $eq)
+                               ]);
+                               $o = $argv[$i];
                        }
 
                        if (!isset($this->spec[$o])) {
@@ -296,12 +304,18 @@ class CliArgs implements \ArrayAccess
                return $this->offsetGet($o);
        }
        function offsetSet($o, $v) {
+               $osn = $this->optShortName($o);
+               $oln = $this->optLongName($o);
                if ($this->optIsMulti($o)) {
-                       $this->args["-".$this->optShortName($o)][] = $v;
-                       $this->args["--".$this->optLongName($o)][] = $v;
+                       if (isset($osn)) {
+                               $this->args["-$osn"][] = $v;
+                       }
+                       $this->args["--$oln"][] = $v;
                } else {
-                       $this->args["-".$this->optShortName($o)] = $v;
-                       $this->args["--".$this->optLongName($o)] = $v;
+                       if (isset($osn)) {
+                               $this->args["-$osn"] = $v;
+                       }
+                       $this->args["--$oln"] = $v;
                }
        }
        function __set($o, $v) {