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