diff --git a/getopts.php b/getopts.php index 468683d..aae5dbc 100644 --- a/getopts.php +++ b/getopts.php @@ -200,14 +200,24 @@ class getopts if ($this->simulate !== null) $commandLine = $this->simulate; 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; $tokens = array (); $prevToken = ""; // Look for sentences in the arguments of the command line $offset = 0; if ($debug) echo "\n012345678901234567890123456789\n$commandLine\n"; - while ($offset <= mb_strlen ($commandLine)) + while ($offset < mb_strlen ($commandLine)) { if ($debug) echo "OFFSET=$offset\n"; $start = strpos ($commandLine, "\"", $offset); @@ -224,7 +234,7 @@ class getopts " with $nbchars chars)\n"; $token = substr ($commandLine, $offset + 1, $nbchars); $tokens[] = $token; - $offset = $end + 1; + $offset = $end + 2; continue; } } @@ -251,8 +261,7 @@ class getopts //if ($this->restOfLine !== null) // $this->restOfLine[] = $token; //elseif (trim ($token) !== "") - if (trim ($token) !== "") - $tokens[] = $token; + $tokens[] = $token; $offset = $end + 1; } if ($debug) print_r ($tokens);