Spaces:
Sleeping
Sleeping
File size: 7,325 Bytes
07c3cdd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | <?php
/**
* @package pake
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @copyright 2004-2005 Fabien Potencier <fabien.potencier@symfony-project.com>
* @license see the LICENSE file included in the distribution
* @version SVN: $Id: pakeGetopt.class.php 1791 2006-08-23 21:17:06Z fabien $
*/
if (class_exists('pakeGetopt'))
{
return;
}
/**
*
* Console options parsing class.
*
* @package pake
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @copyright 2004-2005 Fabien Potencier <fabien.potencier@symfony-project.com>
* @license see the LICENSE file included in the distribution
* @version SVN: $Id: pakeGetopt.class.php 1791 2006-08-23 21:17:06Z fabien $
*/
class pakeGetopt
{
const NO_ARGUMENT = 0;
const REQUIRED_ARGUMENT = 1;
const OPTIONAL_ARGUMENT = 2;
private $short_options = array();
private $long_options = array();
private $args = '';
private $options = array();
private $arguments = array();
public function __construct($options)
{
$this->args = '';
foreach ($options as $option)
{
if (!$option[0])
{
throw new pakeException(sprintf("pakeGetopt: You must define a long option name! for option %s (%s).", $option[1], $option[3]));
}
$this->add_option($option[0], $option[1], $option[2], $option[3]);
}
}
public function add_option($long_opt, $short_opt, $mode = self::NO_ARGUMENT, $comment = '')
{
if ($long_opt{0} == '-' && $long_opt{1} == '-')
{
$long_opt = substr($long_opt, 2);
}
if ($short_opt)
{
if ($short_opt{0} == '-')
{
$short_opt = substr($short_opt, 1);
}
$this->short_options[$short_opt] = array('mode' => $mode, 'comment' => $comment, 'name' => $long_opt);
}
$this->long_options[$long_opt] = array('mode' => $mode, 'comment' => $comment, 'name' => $long_opt);
}
public function parse($args = null)
{
if (is_string($args))
{
// hack to split arguments with spaces : --test="with some spaces"
$args = preg_replace('/(\'|")(.+?)\\1/e', "str_replace(' ', '=PLACEHOLDER=', '\\2')", $args);
$args = preg_split('/\s+/', $args);
$args = str_replace('=PLACEHOLDER=', ' ', $args);
}
else if (!$args)
{
$args = $this->read_php_argv();
// we strip command line program
if (isset($args[0]) && $args[0]{0} != '-')
{
array_shift($args);
}
}
$this->args = $args;
$this->options = array();
$this->arguments = array();
while ($arg = array_shift($this->args))
{
/* '--' stop options parsing. */
if ($arg == '--')
{
$this->arguments = array_merge($this->arguments, $this->args);
break;
}
if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$this->long_options))
{
$this->arguments = array_merge($this->arguments, array($arg), $this->args);
break;
}
elseif (strlen($arg) > 1 && $arg{1} == '-')
{
$this->parse_long_option(substr($arg, 2));
}
else
{
$this->parse_short_option(substr($arg, 1));
}
}
}
public function has_option($option)
{
return (array_key_exists($option, $this->options) ? true : false);
}
public function get_option($option)
{
// is it a long option?
if (array_key_exists($option, $this->long_options) && $this->long_options[$option]['mode'] != self::NO_ARGUMENT)
{
return (array_key_exists($option, $this->options) ? $this->options[$option] : '');
}
else
{
throw new pakeException('pakeGetopt: You cannot get a value for a NO_ARGUMENT option.');
}
}
public function get_options()
{
return $this->options;
}
public function get_arguments()
{
return $this->arguments;
}
private function parse_short_option($arg)
{
for ($i = 0; $i < strlen($arg); $i++)
{
$opt = $arg{$i};
$opt_arg = true;
/* option exists? */
if (!array_key_exists($opt, $this->short_options))
{
throw new pakeException(sprintf("pakeGetopt: unrecognized option -%s.", $opt));
}
/* required or optional argument? */
if ($this->short_options[$opt]['mode'] == self::REQUIRED_ARGUMENT)
{
if ($i + 1 < strlen($arg))
{
$this->options[$this->short_options[$opt]['name']] = substr($arg, $i + 1);
break;
}
else
{
// take next element as argument (if it doesn't start with a -)
if (count($this->args) && $this->args[0]{0} != '-')
{
$this->options[$this->short_options[$opt]['name']] = array_shift($this->args);
break;
}
else
{
throw new pakeException(sprintf("pakeGetopt: option -%s requires an argument", $opt));
}
}
}
else if ($this->short_options[$opt]['mode'] == self::OPTIONAL_ARGUMENT)
{
if (substr($arg, $i + 1) != '')
{
$this->options[$this->short_options[$opt]['name']] = substr($arg, $i + 1);
}
else
{
// take next element as argument (if it doesn't start with a -)
if (count($this->args) && $this->args[0]{0} != '-')
{
$this->options[$this->short_options[$opt]['name']] = array_shift($this->args);
}
else
{
$this->options[$this->short_options[$opt]['name']] = true;
}
}
break;
}
$this->options[$this->short_options[$opt]['name']] = $opt_arg;
}
}
private function parse_long_option($arg)
{
@list($opt, $opt_arg) = explode('=', $arg);
if (!$opt_arg)
{
$opt_arg = true;
}
/* option exists? */
if (!array_key_exists($opt, $this->long_options))
{
throw new pakeException(sprintf("pakeGetopt: unrecognized option --%s.", $opt));
}
/* required or optional argument? */
if ($this->long_options[$opt]['mode'] == self::REQUIRED_ARGUMENT)
{
if ($opt_arg)
{
$this->options[$this->long_options[$opt]['name']] = $opt_arg;
return;
}
else
{
throw new pakeException(sprintf("pakeGetopt: option --%s requires an argument.", $opt));
}
}
else if ($this->long_options[$opt]['mode'] == self::OPTIONAL_ARGUMENT)
{
$this->options[$this->long_options[$opt]['name']] = $opt_arg;
return;
}
else
{
$this->options[$this->long_options[$opt]['name']] = true;
}
}
/**
* Function from PEAR::Console_Getopt.
* Safely read the $argv PHP array across different PHP configurations.
* Will take care on register_globals and register_argc_argv ini directives
*
* @access public
* @return mixed the $argv PHP array
*/
private function read_php_argv()
{
global $argv;
if (!is_array($argv))
{
if (!@is_array($_SERVER['argv']))
{
if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv']))
{
throw new pakeException("pakeGetopt: Could not read cmd args (register_argc_argv=Off?).");
}
return $GLOBALS['HTTP_SERVER_VARS']['argv'];
}
return $_SERVER['argv'];
}
return $argv;
}
}
|