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