| 1 | <?php |
| 2 | /** |
| 3 | * getopt relacement / extenstion |
| 4 | * |
| 5 | * prior to PHP 5.3 getopt is not supported on the windows plattform |
| 6 | * and it does not support long options on other plattforms as well. |
| 7 | * |
| 8 | * this file offers a _getopt() function as a replacement. via the |
| 9 | * php.net manual page for getop(). |
| 10 | * |
| 11 | * original author is 可愛柚爸 / uberlinuxguy at tulg dot org |
| 12 | * |
| 13 | * the function has been taken from that website, and refactored |
| 14 | * into a helper class to increase the protability |
| 15 | * |
| 16 | * @version 1.0 hakre-1 |
| 17 | * |
| 18 | * CHANGELOG: |
| 19 | * |
| 20 | * - refactored the functions into a class (better portability) |
| 21 | * - reformatted the code (copy & paste issues) |
| 22 | * - removed eval calls (commented) |
| 23 | * - smarter quoting |
| 24 | * - indentation on tab and cleanup of whitespaces |
| 25 | * - deprecated string access ({}) fixed with []. |
| 26 | * |
| 27 | * TODO: |
| 28 | * (empty) |
| 29 | * |
| 30 | * |
| 31 | * @link http://www.ntu.beautifulworldco.com/weblog/?p=526 |
| 32 | * @link http://www.php.net/getopt |
| 33 | */ |
| 34 | |
| 35 | /** |
| 36 | * getoptParser |
| 37 | * |
| 38 | * getopt() compatible argv parsing. |
| 39 | * |
| 40 | * @see getoptParser::getopt() |
| 41 | * @see getoptParser::split_para() |
| 42 | */ |
| 43 | class getoptParser { |
| 44 | /** |
| 45 | * getopt() |
| 46 | * |
| 47 | * Usage: _getopt ( [$flag,] $short_option [, $long_option] ); |
| 48 | * |
| 49 | * Note that another function split_para() is required, which can be |
| 50 | * found in the same page. |
| 51 | * |
| 52 | * _getopt() fully simulates getopt() which is described at |
| 53 | * (@see http://us.php.net/manual/en/function.getopt.php} , including long |
| 54 | * options for PHP version under 5.3.0. (Prior to 5.3.0, long options was |
| 55 | * only available on few systems) |
| 56 | * |
| 57 | * Besides legacy usage of getopt(), I also added a new option to manipulate |
| 58 | * your own argument lists instead of those from command lines. This new |
| 59 | * option can be a string or an array such as |
| 60 | * |
| 61 | * $flag = "-f value_f -ab --required 9 --optional=PK --option -v test -k"; |
| 62 | * |
| 63 | * or |
| 64 | * |
| 65 | * $flag = array ( "-f", "value_f", "-ab", "--required", "9", "--optional=PK", "--option" ); |
| 66 | * |
| 67 | * So there are four ways to work with _getopt(), |
| 68 | * |
| 69 | * 1. _getopt ( $short_option ); |
| 70 | * it's a legacy usage, same as getopt ( $short_option ). |
| 71 | * |
| 72 | * 2. _getopt ( $short_option, $long_option ); |
| 73 | * it's a legacy usage, same as getopt ( $short_option, $long_option ). |
| 74 | * |
| 75 | * 3. _getopt ( $flag, $short_option ); |
| 76 | * use your own argument lists instead of command line arguments. |
| 77 | * |
| 78 | * 4. _getopt ( $flag, $short_option, $long_option ); |
| 79 | * use your own argument lists instead of command line arguments. |
| 80 | * |
| 81 | * @version 1.3 |
| 82 | * @date 2009/05/30 (taken from the website 2010-01-11) |
| 83 | * @author 可愛柚爸 / uberlinuxguy at tulg dot org |
| 84 | * @see http://www.ntu.beautifulworldco.com/weblog/?p=526 |
| 85 | * |
| 86 | * @params mixed |
| 87 | * @return array |
| 88 | */ |
| 89 | static function getopt() { |
| 90 | |
| 91 | if ( func_num_args() == 1 ) { |
| 92 | $flag = $flag_array = $GLOBALS['argv']; |
| 93 | $short_option = func_get_arg ( 0 ); |
| 94 | $long_option = array (); |
| 95 | } elseif ( func_num_args() == 2 ) { |
| 96 | if ( is_array ( func_get_arg ( 1 ) ) ) { |
| 97 | $flag = $GLOBALS['argv']; |
| 98 | $short_option = func_get_arg ( 0 ); |
| 99 | $long_option = func_get_arg ( 1 ); |
| 100 | } else { |
| 101 | $flag = func_get_arg ( 0 ); |
| 102 | $short_option = func_get_arg ( 1 ); |
| 103 | $long_option = array (); |
| 104 | } |
| 105 | } elseif ( func_num_args() == 3 ) { |
| 106 | $flag = func_get_arg ( 0 ); |
| 107 | $short_option = func_get_arg ( 1 ); |
| 108 | $long_option = func_get_arg ( 2 ); |
| 109 | } else { |
| 110 | exit ( "wrong options\n" ); |
| 111 | } |
| 112 | |
| 113 | $short_option = trim($short_option); |
| 114 | $short_no_value = array(); |
| 115 | $short_required_value = array(); |
| 116 | $short_optional_value = array(); |
| 117 | $long_no_value = array(); |
| 118 | $long_required_value = array(); |
| 119 | $long_optional_value = array(); |
| 120 | $options = array(); |
| 121 | |
| 122 | for ( $i = 0; $i < strlen ( $short_option ); ) { |
| 123 | if ( $short_option[$i] != ':' ) { |
| 124 | if ( $i == strlen ( $short_option ) - 1 ) { |
| 125 | $short_no_value[] = $short_option[$i]; |
| 126 | break; |
| 127 | } elseif ( $short_option[$i+1] != ':' ) { |
| 128 | $short_no_value[] = $short_option[$i]; |
| 129 | $i++; |
| 130 | continue; |
| 131 | } elseif ( $short_option[$i+1] == ':' && $short_option[$i+2] != ':' ) { |
| 132 | $short_required_value[] = $short_option[$i]; |
| 133 | $i += 2; |
| 134 | continue; |
| 135 | } elseif ( $short_option[$i+1] == ':' && $short_option[$i+2] == ':' ) { |
| 136 | $short_optional_value[] = $short_option[$i]; |
| 137 | $i += 3; |
| 138 | continue; |
| 139 | } |
| 140 | } else { |
| 141 | continue; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | foreach ( $long_option as $a ) { |
| 146 | if ( substr( $a, -2 ) == '::' ) { |
| 147 | $long_optional_value[] = substr($a, 0, -2); |
| 148 | } elseif ( substr($a, -1) == ':' ) { |
| 149 | $long_required_value[] = substr($a, 0, -1); |
| 150 | } else { |
| 151 | $long_no_value[] = $a; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if ( is_array ( $flag ) ) { |
| 156 | $flag_array = $flag; |
| 157 | } else { |
| 158 | $flag = "- $flag"; |
| 159 | $flag_array = self::_split_para($flag); |
| 160 | } |
| 161 | |
| 162 | for ( $i = 0; $i < count($flag_array); ) { |
| 163 | |
| 164 | if ( !$flag_array[$i] || ( '-' == $flag_array[$i] ) ) { |
| 165 | $i++; |
| 166 | continue; |
| 167 | } elseif ( '-' != $flag_array[$i][0] ) { |
| 168 | $i++; |
| 169 | continue; |
| 170 | } |
| 171 | |
| 172 | if ( substr($flag_array[$i], 0, 2) == '--' ) { |
| 173 | if (strpos($flag_array[$i], '=') != false) { |
| 174 | list($key, $value) = explode('=', substr($flag_array[$i], 2), 2); |
| 175 | if ( in_array($key, $long_required_value ) || in_array($key, $long_optional_value ) ) |
| 176 | $options[$key][] = $value; |
| 177 | $i++; |
| 178 | continue; |
| 179 | } elseif (strpos($flag_array[$i], '=') == false) { |
| 180 | $key = substr($flag_array[$i], 2); |
| 181 | if ( in_array(substr( $flag_array[$i], 2 ), $long_required_value ) ) { |
| 182 | $options[$key][] = $flag_array[$i+1]; |
| 183 | $i++; |
| 184 | } elseif ( in_array(substr( $flag_array[$i], 2 ), $long_optional_value ) ) { |
| 185 | if ( $flag_array[$i+1] != '' && $flag_array[$i+1][0] != '-' ) { |
| 186 | $options[$key][] = $flag_array[$i+1]; |
| 187 | $i++; |
| 188 | } else { |
| 189 | $options[$key][] = FALSE; |
| 190 | } |
| 191 | } elseif ( in_array(substr( $flag_array[$i], 2 ), $long_no_value ) ) { |
| 192 | $options[$key][] = FALSE; |
| 193 | } |
| 194 | $i++; |
| 195 | continue; |
| 196 | } |
| 197 | } elseif ( $flag_array[$i][0] == '-' && $flag_array[$i][1] != '-' ) { |
| 198 | for ( $j=1; $j < strlen($flag_array[$i]); $j++ ) { |
| 199 | if ( in_array($flag_array[$i][$j], $short_required_value ) || in_array($flag_array[$i][$j], $short_optional_value )) { |
| 200 | if ( $j == strlen($flag_array[$i]) - 1 ) { |
| 201 | if ( in_array($flag_array[$i][$j], $short_required_value ) ) { |
| 202 | if (isset($flag_array[$i+1])) |
| 203 | $options[$flag_array[$i][$j]][] = $flag_array[$i+1]; |
| 204 | $i++; |
| 205 | } elseif ( in_array($flag_array[$i][$j], $short_optional_value ) && $flag_array[$i+1] != '' && $flag_array[$i+1][0] != '-' ) { |
| 206 | $options[$flag_array[$i][$j]][] = $flag_array[$i+1]; |
| 207 | $i++; |
| 208 | } else { |
| 209 | $options[$flag_array[$i][$j]][] = FALSE; |
| 210 | } |
| 211 | } else { |
| 212 | $options[$flag_array[$i][$j]][] = substr ( $flag_array[$i], $j + 1 ); |
| 213 | } |
| 214 | $plus_i = 0; |
| 215 | $i++; |
| 216 | break; |
| 217 | } elseif ( in_array($flag_array[$i][$j], $short_no_value ) ) { |
| 218 | $options[$flag_array[$i][$j]][] = FALSE; |
| 219 | $plus_i = 1; |
| 220 | continue; |
| 221 | } else { |
| 222 | $plus_i = 1; |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | $i += $plus_i; |
| 227 | continue; |
| 228 | } // if |
| 229 | $i++; |
| 230 | } // for |
| 231 | |
| 232 | // reduce options array depth if possible |
| 233 | foreach ( $options as $key => $value ) { |
| 234 | if ( count($value) == 1 ) |
| 235 | $options[$key] = $value[0]; |
| 236 | } |
| 237 | |
| 238 | return $options; |
| 239 | |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * split parameters |
| 244 | * |
| 245 | * static helper function |
| 246 | * |
| 247 | * @version 1.0 |
| 248 | * @date 2008/08/19 |
| 249 | * @see http://www.ntu.beautifulworldco.com/weblog/?p=526 |
| 250 | * |
| 251 | * This function is to parse parameters and split them into smaller pieces. |
| 252 | * preg_split() does similar thing but in our function, besides "space", we |
| 253 | * also take the three symbols " (double quote), '(single quote), |
| 254 | * and \ (backslash) into consideration because things in a pair of " or ' |
| 255 | * should be grouped together. |
| 256 | * |
| 257 | * As an example, this parameter list |
| 258 | * |
| 259 | * -f "test 2" -ab --required "t\"est 1" --optional="te'st 3" --option -v 'test 4' |
| 260 | * |
| 261 | * will be splited into |
| 262 | * |
| 263 | * -f; test 2; -ab; --required; t"est 1; --optional=te'st 3; --option; -v; test 4 |
| 264 | * |
| 265 | * see the code below: |
| 266 | * |
| 267 | * <code> |
| 268 | * $pattern = "-f \"test 2\" -ab --required \"t\\\"est 1\" --optional=\"te'st 3\" --option -v 'test 4'"; |
| 269 | * $result = getoptParser::split_para($pattern); |
| 270 | * echo "ORIGINAL PATTERN: $pattern\n\n"; |
| 271 | * var_dump($result); |
| 272 | * </code> |
| 273 | * |
| 274 | * @param string $pattern |
| 275 | * @return array |
| 276 | */ |
| 277 | public static function split_para($pattern) { |
| 278 | $begin = 0; |
| 279 | $backslash = 0; |
| 280 | $quote = ''; |
| 281 | $quote_mark = array(); |
| 282 | $result = array(); |
| 283 | $pattern = trim($pattern); |
| 284 | $cand1 = ''; |
| 285 | |
| 286 | for ( $end = 0; $end < strlen($pattern); ) { |
| 287 | if ( !in_array($pattern[$end], array(' ', '"', "'", "\\")) ) { |
| 288 | $backslash = 0; |
| 289 | $end++; |
| 290 | continue; |
| 291 | } |
| 292 | if ( $pattern[$end] == "\\" ) { |
| 293 | $backslash++; |
| 294 | $end++; |
| 295 | continue; |
| 296 | } elseif ( $pattern[$end] == '"' ) { |
| 297 | if ( $backslash % 2 == 1 || $quote == "'" ) { |
| 298 | $backslash = 0; |
| 299 | $end++; |
| 300 | continue; |
| 301 | } |
| 302 | if ( $quote == '' ) { |
| 303 | $quote_mark[] = $end - $begin; |
| 304 | $quote = '"'; |
| 305 | } elseif ( $quote == '"' ) { |
| 306 | $quote_mark[] = $end - $begin; |
| 307 | $quote = ''; |
| 308 | } |
| 309 | |
| 310 | $backslash = 0; |
| 311 | $end++; |
| 312 | continue; |
| 313 | } elseif ( $pattern[$end] == "'" ) { |
| 314 | if ( $backslash % 2 == 1 || $quote == '"' ) { |
| 315 | $backslash = 0; |
| 316 | $end++; |
| 317 | continue; |
| 318 | } |
| 319 | if ( $quote == '' ) { |
| 320 | $quote_mark[] = $end - $begin; |
| 321 | $quote = "'"; |
| 322 | } elseif ( $quote == "'" ) { |
| 323 | $quote_mark[] = $end - $begin; |
| 324 | $quote = ''; |
| 325 | } |
| 326 | |
| 327 | $backslash = 0; |
| 328 | $end++; |
| 329 | continue; |
| 330 | } elseif ( $pattern[$end] == ' ' ) { |
| 331 | if ( $quote != '' ) { |
| 332 | $backslash = 0; |
| 333 | $end++; |
| 334 | continue; |
| 335 | } else { |
| 336 | $backslash = 0; |
| 337 | $cand = substr($pattern, $begin, $end-$begin); |
| 338 | for ( $j = 0; $j < strlen($cand); $j++ ) { |
| 339 | if ( in_array($j, $quote_mark) ) |
| 340 | continue; |
| 341 | |
| 342 | $cand1 .= $cand[$j]; |
| 343 | } |
| 344 | if ( $cand1 ) { |
| 345 | // commented and replaced: |
| 346 | // eval( "\$cand1 = \"$cand1\";" ); |
| 347 | $result[] = (string) $cand1; |
| 348 | } |
| 349 | $quote_mark = array(); |
| 350 | $cand1 = ''; |
| 351 | $begin =++$end; |
| 352 | continue; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | $cand = substr($pattern, $begin, $end-$begin); |
| 358 | for ( $j = 0; $j < strlen($cand); $j++ ) { |
| 359 | if ( in_array($j, $quote_mark ) ) |
| 360 | continue; |
| 361 | $cand1 .= $cand[$j]; |
| 362 | } |
| 363 | |
| 364 | // commented and replaced: |
| 365 | // eval( "\$cand1 = \"$cand1\";" ); |
| 366 | $cand1 = (string) $cand1; |
| 367 | |
| 368 | if ( $cand1 ) |
| 369 | $result[] = $cand1; |
| 370 | |
| 371 | return $result; |
| 372 | } |
| 373 | } |
| 374 | No newline at end of file |