* Original option spec
* @var array
*/
- private $orig;
+ private $orig = [];
/**
* Compiled spec
* @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;
}
/**
- * Get compiled spec
+ * Get original spec
* @return array
*/
public function getSpec() {
+ return $this->orig;
+ }
+
+ /**
+ * Get compiled spec
+ * @return array
+ */
+ public function getCompiledSpec() {
return $this->spec;
}
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])) {
}
}
- /**
- * Output command line help message
- * @param string $prog
- */
- public function help($prog) {
- printf("\nUsage:\n\n $ %s", $prog);
- $flags = [];
- $required = [];
- $optional = [];
- foreach ($this->orig as $spec) {
- if ($spec[3] & self::REQARG) {
- if ($spec[3] & self::REQUIRED) {
- $required[] = $spec;
- } else {
- $optional[] = $spec;
- }
- } else {
- $flags[] = $spec;
- }
- }
-
- if ($flags) {
- printf(" [-%s]", implode("|-", array_column($flags, 0)));
- }
- foreach ($required as $req) {
- printf(" -%s <arg>", $req[0]);
- }
- if ($optional) {
- printf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
- }
- printf("\n\n");
- foreach ($this->orig as $spec) {
- printf(" -%s|--%s %s", $spec[0], $spec[1], ($spec[3] & self::REQARG) ? "<arg> " : (($spec[3] & self::OPTARG) ? "[<arg>]" : " "));
- printf("%s%s %s", str_repeat(" ", 16-strlen($spec[1])), $spec[2], ($spec[3] & self::REQUIRED) ? "(REQUIRED)" : "");
- if (isset($spec[4])) {
- printf(" [%s]", $spec[4]);
- }
- printf("\n");
- }
- printf("\n");
- }
-
/**
* Retreive the default argument of an option
* @param string $o
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) {