Update getopts to read correctely the ending line
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3526 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
@@ -39,7 +39,7 @@ class test_getopts extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function test_scan1 ()
|
||||
{
|
||||
$this->expectException ("Exception", "Provided tokens are not known: -f,ii,-o,1 2,-o,2 2,-o,3");
|
||||
$this->expectException ("Exception", "Provided tokens are not known: -f,-o,-o,-o");
|
||||
$getopts = new getopts ();
|
||||
$getopts->simulate ("sim\ ulate -h -d -d -f ii -o 1\ 2 -o \"2 2\" -o 3 -- -bla final");
|
||||
$getopts->add ("Help", "?h", array ("help","help2"), "Help of the software");
|
||||
@@ -145,6 +145,26 @@ class test_getopts extends PHPUnit_Framework_TestCase
|
||||
$this->assertSame ($res, array ("-bla", "final"));
|
||||
}
|
||||
|
||||
public function test_restOfLine2 ()
|
||||
{
|
||||
$getopts = new getopts ();
|
||||
$getopts->simulate ("sim\ ulate bla final");
|
||||
$getopts->add ("Help", "?h", array ("help","help2"), "Help of the software");
|
||||
$getopts->add ("Debug", "d", "debug", "Debug", "DebugLevel", 2);
|
||||
$res = $getopts->restOfLine ();
|
||||
$this->assertSame ($res, array ("bla", "final"));
|
||||
}
|
||||
|
||||
public function test_restOfLine3 ()
|
||||
{
|
||||
$getopts = new getopts ();
|
||||
$getopts->simulate ("sim\ ulate -- -bla final");
|
||||
$getopts->add ("Help", "?h", array ("help","help2"), "Help of the software");
|
||||
$getopts->add ("Debug", "d", "debug", "Debug", "DebugLevel", 2);
|
||||
$res = $getopts->restOfLine ();
|
||||
$this->assertSame ($res, array ("-bla", "final"));
|
||||
}
|
||||
|
||||
public function test_programName1 ()
|
||||
{
|
||||
$getopts = new getopts ();
|
||||
|
||||
76
getopts.php
76
getopts.php
@@ -9,7 +9,7 @@ require_once ("domframework/verify.php");
|
||||
* - Manage optional parameter to argument: -b (-b:: in getopt)
|
||||
* The long options start with two dashes, the short start with one dash and
|
||||
* can only be one letter
|
||||
* Stop to analyze the parameters when the param value is "--"
|
||||
* If the token is "--", the following value can start by dash
|
||||
*/
|
||||
class getopts
|
||||
{
|
||||
@@ -82,7 +82,12 @@ class getopts
|
||||
throw new \Exception (
|
||||
"paramName of option provided to getopts is not a string", 500);
|
||||
if (is_string ($long))
|
||||
$long = explode (",", $long);
|
||||
{
|
||||
if (trim ($long) !== "")
|
||||
$long = explode (",", $long);
|
||||
else
|
||||
$long = array ();
|
||||
}
|
||||
if ($short[0] === ":")
|
||||
throw new \Exception ("Short option can't start by semi-colon", 500);
|
||||
if ((strpos ($short, ":") !== false ||
|
||||
@@ -228,15 +233,9 @@ class getopts
|
||||
if ($end === false)
|
||||
$end = strlen ($commandLine);
|
||||
$nbchars = $end - $offset;
|
||||
if ($debug) echo "WORD FOUND (Start $offset with $nbchars chars)\n";
|
||||
$token = substr ($commandLine, $offset, $nbchars);
|
||||
if ($token === "--")
|
||||
{
|
||||
$this->restOfLine = array ();
|
||||
if ($debug) echo "RESTOFLINE=$this->restOfLine\n";
|
||||
$offset = $end + 1;
|
||||
continue;
|
||||
}
|
||||
if ($debug)
|
||||
echo "WORD FOUND (Start $offset with $nbchars chars): $token\n";
|
||||
// The '\ ' are token concatenation. Remove the \ in the parameters
|
||||
if (substr ($token, -1) === "\\")
|
||||
{
|
||||
@@ -271,10 +270,13 @@ class getopts
|
||||
{
|
||||
if ($option["multiple"] < 2)
|
||||
{
|
||||
if ($debug) echo "CHECK $opt UNIQUE\n";
|
||||
if ($debug) echo "CHECK $opt UNIQUE -> ";
|
||||
$keys = array_keys ($tokens, str_replace (":", "", $opt));
|
||||
if (count ($keys) === 0)
|
||||
{
|
||||
if ($debug) echo "Option Not found\n";
|
||||
continue;
|
||||
}
|
||||
if (count ($keys) > 1)
|
||||
throw new \Exception (sprintf (dgettext ("domframework",
|
||||
"Too much identical parameters provided: %s"), $opt), 500);
|
||||
@@ -286,6 +288,19 @@ class getopts
|
||||
$this->parameters[$option["identifier"]] = true;
|
||||
unset ($tokens[$pos]);
|
||||
}
|
||||
elseif (strpos ($opt, "::") !== false &&
|
||||
array_key_exists ($pos+1, $tokens) &&
|
||||
array_key_exists ($pos+2, $tokens) &&
|
||||
$tokens[$pos+1] === "--")
|
||||
{
|
||||
if ($debug)
|
||||
echo "FOUND UNIQUE with optional param filled ".
|
||||
"and double-dash\n";
|
||||
$this->parameters[$option["identifier"]] = $tokens[$pos+2];
|
||||
unset ($tokens[$pos]);
|
||||
unset ($tokens[$pos+1]);
|
||||
unset ($tokens[$pos+2]);
|
||||
}
|
||||
elseif (strpos ($opt, "::") !== false &&
|
||||
array_key_exists ($pos+1, $tokens) &&
|
||||
substr ($tokens[$pos+1], 0, 1) !== "-")
|
||||
@@ -297,6 +312,12 @@ class getopts
|
||||
}
|
||||
elseif (strpos ($opt, ":") !== false)
|
||||
{
|
||||
if (array_key_exists ($pos+1, $tokens) &&
|
||||
$tokens[$pos+1] === "--" &&
|
||||
! array_key_exists ($pos+2, $tokens))
|
||||
throw new \Exception (dgettext ("domframework",
|
||||
"Mandatory value for parameter '$opt' is not provided ".
|
||||
"after double-dash"), 500);
|
||||
if (! array_key_exists ($pos+1, $tokens) ||
|
||||
substr ($tokens[$pos+1], 0, 1) === "-")
|
||||
throw new \Exception (dgettext ("domframework",
|
||||
@@ -306,13 +327,20 @@ class getopts
|
||||
unset ($tokens[$pos]);
|
||||
unset ($tokens[$pos+1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($debug) echo "Not found\n";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($debug) echo "CHECK $opt MULTIPLE\n";
|
||||
if ($debug) echo "CHECK $opt MULTIPLE -> ";
|
||||
$keys = array_keys ($tokens, str_replace (":", "", $opt));
|
||||
if (count ($keys) === 0)
|
||||
{
|
||||
if ($debug) echo "Option Not found\n";
|
||||
continue;
|
||||
}
|
||||
if (count ($keys) > $option["multiple"])
|
||||
throw new \Exception (sprintf (dgettext ("domframework",
|
||||
"Too much multiple parameters provided: %s"), $opt), 500);
|
||||
@@ -339,18 +367,38 @@ class getopts
|
||||
if (! array_key_exists ($pos+1, $tokens) ||
|
||||
substr ($tokens[$pos+1], 0, 1) === "-")
|
||||
throw new \Exception (sprintf (dgettext ("domframework",
|
||||
"Mandatory value for parameter '%s' is not provided"), $opt),
|
||||
500);
|
||||
"Mandatory value for parameter '%s' is not provided"),
|
||||
$opt), 500);
|
||||
if ($debug) echo "FOUND MUTLIPLE with mandatory param filled\n";
|
||||
$this->parameters[$option["identifier"]][] = $tokens[$pos+1];
|
||||
unset ($tokens[$pos]);
|
||||
unset ($tokens[$pos+1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($debug) echo "Not found\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Manage the restOfLine
|
||||
foreach ($tokens as $key=>$tok)
|
||||
{
|
||||
if ($tok === "--" && array_key_exists ($key+1, $tokens))
|
||||
{
|
||||
$this->restOfLine[] = $tokens[$key+1];
|
||||
unset ($tokens[$key]);
|
||||
unset ($tokens[$key+1]);
|
||||
continue;
|
||||
}
|
||||
if (substr ($tok, 0, 1) === "-")
|
||||
continue;
|
||||
$this->restOfLine[] = $tok;
|
||||
unset ($tokens[$key]);
|
||||
}
|
||||
if (count ($tokens))
|
||||
{
|
||||
throw new \Exception (sprintf (dgettext ("domframework",
|
||||
|
||||
Reference in New Issue
Block a user