Revision: 6755
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at June 12, 2008 17:35 by maid450
Initial Code
<?php function getArgs($args) { $out = array(); $last_arg = null; for($i = 1, $il = sizeof($args); $i < $il; $i++) { if( (bool)preg_match("/^--(.+)/", $args[$i], $match) ) { $parts = explode("=", $match[1]); $key = preg_replace("/[^a-z0-9]+/", "", $parts[0]); if(isset($parts[1])) { $out[$key] = $parts[1]; } else { $out[$key] = true; } $last_arg = $key; } else if( (bool)preg_match("/^-([a-zA-Z0-9]+)/", $args[$i], $match) ) { for( $j = 0, $jl = strlen($match[1]); $j < $jl; $j++ ) { $key = $match[1]{$j}; $out[$key] = true; } $last_arg = $key; } else if($last_arg !== null) { $out[$last_arg] = $args[$i]; } } return $out; } /* sample php file.php --foo=bar -abc -AB 'hello world' --baz produces: Array ( [foo] => bar [a] => true [b] => true [c] => true [A] => true [B] => hello world [baz] => true ) */ ?>
Initial URL
http://es2.php.net/features.commandline
Initial Description
seen in comments in http://es2.php.net/features.commandline
Initial Title
Command line args parser
Initial Tags
Initial Language
PHP