refactoring custom getopt

This commit is contained in:
Dmitry Mamontov 2015-11-23 10:01:46 -05:00
parent b079105b4f
commit 2d1aceb100

View File

@ -86,29 +86,41 @@ class DataHelper
} }
$result = array(); $result = array();
$params = $_SERVER['argv']; $params = $_SERVER['argv'];
while (list(, $p) = each($params)) { foreach ($params as $key => $param) {
if ($p{0} == '-') { if ($param{0} == '-') {
$pname = substr($p, 1); $name = substr($param, 1);
$value = true; $value = true;
if ($pname{0} == '-') {
$pname = substr($pname, 1); if ($name{0} == '-') {
if (strpos($p, '=') !== false) { $name = substr($name, 1);
list($pname, $value) = explode('=', substr($p, 2), 2); if (strpos($param, '=') !== false) {
$long = explode('=', substr($param, 2), 2);
$name = $long[0];
$value = $long[1];
unset($long);
} }
} }
$nextparm = current($params); if (
if ($value === true && $nextparm !== false && $nextparm{0} != '-') { isset($params[$key + 1]) &&
list(, $value) = each($params); $value === true &&
$params[$key + 1] !== false &&
$params[$key + 1]{0} != '-'
) {
$value = $params[$key + 1];
} }
$result[$pname] = $value;
$result[$name] = $value;
} else { } else {
$result[] = $p; $result[] = $param;
} }
} }
unset($params);
return empty($result) ? false : $result; return empty($result) ? false : $result;
} }
} }