note 78651 added to features.commandline

From: Date: Mon, 22 Oct 2007 02:01:26 +0000
Subject: note 78651 added to features.commandline
Groups: php.notes 
Request: Send a blank email to php-notes+get-131614@lists.php.net to get a copy of this message
I have not seen in this thread any code snippets that support the full *nix style argument parsing. Consider this: <?php print_r(getArgs($_SERVER['argv'])); 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; } /* 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 ) */ ?> ---- Server IP: 209.41.74.194 Probable Submitter: 76.81.26.29 ---- Manual Page -- http://www.php.net/manual/en/features.commandline.php Edit -- https://master.php.net/note/edit/78651 Del: integrated -- https://master.php.net/note/delete/78651/integrated Del: useless -- https://master.php.net/note/delete/78651/useless Del: bad code -- https://master.php.net/note/delete/78651/bad+code Del: spam -- https://master.php.net/note/delete/78651/spam Del: non-english -- https://master.php.net/note/delete/78651/non-english Del: in docs -- https://master.php.net/note/delete/78651/in+docs Del: other reasons-- https://master.php.net/note/delete/78651 Reject -- https://master.php.net/note/reject/78651 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#131614) next »