getopts : in case of execution in Website, do not generate any error

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5879 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2020-02-12 15:33:54 +00:00
parent 1bc86d192d
commit 4ba2e8a3f5

View File

@@ -40,16 +40,19 @@ class getopts
/** The constructor check the availability of the MB module /** The constructor check the availability of the MB module
*/ */
public function __construct () public function __construct ()
// {{{
{ {
if (! function_exists ("mb_strlen")) if (! function_exists ("mb_strlen"))
throw new \Exception ("PHP don't have the MB Support. Please add it !", throw new \Exception ("PHP don't have the MB Support. Please add it !",
500); 500);
} }
// }}}
/** Set/Get the simulate value /** Set/Get the simulate value
* @param string|null $simulate The simulate to get/set * @param string|null $simulate The simulate to get/set
*/ */
public function simulate ($simulate = null) public function simulate ($simulate = null)
// {{{
{ {
if ($simulate === null) if ($simulate === null)
return $this->simulate; return $this->simulate;
@@ -61,6 +64,7 @@ class getopts
$this->simulate = $simulate; $this->simulate = $simulate;
return $this; return $this;
} }
// }}}
/** Add a new option to check /** Add a new option to check
* @param string $identifier The identifier of the options * @param string $identifier The identifier of the options
@@ -76,6 +80,7 @@ class getopts
*/ */
public function add ($identifier, $short, $long, $description, public function add ($identifier, $short, $long, $description,
$paramName = "", $multiple = 0) $paramName = "", $multiple = 0)
// {{{
{ {
if (! is_string ($identifier)) if (! is_string ($identifier))
throw new \Exception ("Identifier provided to getopts is not a string", throw new \Exception ("Identifier provided to getopts is not a string",
@@ -204,14 +209,25 @@ class getopts
"multiple" => $multiple); "multiple" => $multiple);
return $this; return $this;
} }
// }}}
/** Scan the command line and fill the parameters. /** Scan the command line and fill the parameters.
* Use the simulate line if provided or use the $argv if not * Use the simulate line if provided or use the $argv if not
* Set the parameters property to an array * Set the parameters property to an array
* @return $this;
*/ */
public function scan () public function scan ()
// {{{
{ {
global $argv; global $argv;
if ($argv === null)
{
// getopts is launched in WebServer. Can not analyze anything
$this->programName = "";
$this->parameters = array ();
$this->restOfLine = array ();
return $this;
}
if ($this->simulate !== null) if ($this->simulate !== null)
$commandLine = $this->simulate; $commandLine = $this->simulate;
else else
@@ -431,7 +447,9 @@ class getopts
} }
if ($this->parameters === null) if ($this->parameters === null)
$this->parameters = array (); $this->parameters = array ();
return $this;
} }
// }}}
/** Get the value of the option if set in the command line. If simulate is /** Get the value of the option if set in the command line. If simulate is
* defined, use it. * defined, use it.
@@ -443,6 +461,7 @@ class getopts
* @param string $identifier The identifier option to get * @param string $identifier The identifier option to get
*/ */
public function get ($identifier) public function get ($identifier)
// {{{
{ {
$exists = false; $exists = false;
foreach ($this->options as $opt) foreach ($this->options as $opt)
@@ -464,28 +483,34 @@ class getopts
return array (); return array ();
return false; return false;
} }
// }}}
/** Get the value found in the rest of line (after the double dashes) /** Get the value found in the rest of line (after the double dashes)
*/ */
public function restOfLine () public function restOfLine ()
// {{{
{ {
if ($this->restOfLine === null) if ($this->restOfLine === null)
$this->scan (); $this->scan ();
return $this->restOfLine; return $this->restOfLine;
} }
// }}}
/** Get the name of the program found in the command line /** Get the name of the program found in the command line
*/ */
public function programName () public function programName ()
// {{{
{ {
if ($this->programName === null) if ($this->programName === null)
$this->scan (); $this->scan ();
return $this->programName; return $this->programName;
} }
// }}}
/** Get the Help message with all the descriptions and options /** Get the Help message with all the descriptions and options
*/ */
public function help () public function help ()
// {{{
{ {
if (count ($this->options) === 0) if (count ($this->options) === 0)
return dgettext ("domframework", "No option defined")."\n"; return dgettext ("domframework", "No option defined")."\n";
@@ -523,4 +548,5 @@ class getopts
} }
return $d; return $d;
} }
// }}}
} }