getopts: manage correctely the parameters if its value is "" (should not be ignored)

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3699 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2017-05-19 07:01:28 +00:00
parent 44c10ff0c6
commit b7334c1cd4

View File

@@ -200,14 +200,24 @@ class getopts
if ($this->simulate !== null) if ($this->simulate !== null)
$commandLine = $this->simulate; $commandLine = $this->simulate;
else else
$commandLine = implode (" ", $argv); {
$args = array ();
foreach ($argv as $arg)
{
if (strpos ($arg, " ") !== false || $arg === "")
$args[] = "\"$arg\"";
else
$args[] = $arg;
}
$commandLine = implode (" ", $args);
}
$debug = false; $debug = false;
$tokens = array (); $tokens = array ();
$prevToken = ""; $prevToken = "";
// Look for sentences in the arguments of the command line // Look for sentences in the arguments of the command line
$offset = 0; $offset = 0;
if ($debug) echo "\n012345678901234567890123456789\n$commandLine\n"; if ($debug) echo "\n012345678901234567890123456789\n$commandLine\n";
while ($offset <= mb_strlen ($commandLine)) while ($offset < mb_strlen ($commandLine))
{ {
if ($debug) echo "OFFSET=$offset\n"; if ($debug) echo "OFFSET=$offset\n";
$start = strpos ($commandLine, "\"", $offset); $start = strpos ($commandLine, "\"", $offset);
@@ -224,7 +234,7 @@ class getopts
" with $nbchars chars)\n"; " with $nbchars chars)\n";
$token = substr ($commandLine, $offset + 1, $nbchars); $token = substr ($commandLine, $offset + 1, $nbchars);
$tokens[] = $token; $tokens[] = $token;
$offset = $end + 1; $offset = $end + 2;
continue; continue;
} }
} }
@@ -251,8 +261,7 @@ class getopts
//if ($this->restOfLine !== null) //if ($this->restOfLine !== null)
// $this->restOfLine[] = $token; // $this->restOfLine[] = $token;
//elseif (trim ($token) !== "") //elseif (trim ($token) !== "")
if (trim ($token) !== "") $tokens[] = $token;
$tokens[] = $token;
$offset = $end + 1; $offset = $end + 1;
} }
if ($debug) print_r ($tokens); if ($debug) print_r ($tokens);