note 93084 added to function.array-shift
| From: | patrick at pwfisher dot com | Date: | Sat, 22 Aug 2009 03:23:13 +0000 |
| Subject: | note 93084 added to function.array-shift | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159782@lists.php.net to get a copy of this message | ||
Here's a utility function to parse command line arguments.
<?php
/**
* CommandLine class
*
* @package Framework
*/
/**
* Command Line Interface (CLI) utility class.
*
* @author Patrick Fisher <patrick@pwfisher.com>
* @since August 21, 2009
* @package Framework
* @subpackage Env
*/
class CommandLine {
/**
* PARSE ARGUMENTS
*
* [pfisher ~]$ echo "<?php
* > include('CommandLine.php');
* > \$args = CommandLine::parseArgs(\$_SERVER['argv']);
* > echo "\n", '\$out = '; var_dump(\$args); echo "\n";
* > ?>" > test.php
*
* [pfisher ~]$ php test.php plain-arg --foo --bar=baz --funny="spam=eggs"
--alsofunny=spam=eggs \
* > 'plain arg 2' -abc -k=value "plain arg 3" --s="original"
--s='overwrite' --s
*
* $out = array(12) {
* [0] => string(9) "plain-arg"
* ["foo"] => bool(true)
* ["bar"] => string(3) "baz"
* ["funny"] => string(9) "spam=eggs"
* ["alsofunny"] => string(9) "spam=eggs"
* [1] => string(11) "plain arg 2"
* ["a"] => bool(true)
* ["b"] => bool(true)
* ["c"] => bool(true)
* ["k"] => string(5) "value"
* [2] => string(11) "plain arg 3"
* ["s"] => string(9) "overwrite"
* }
*
* @author Patrick Fisher <patrick@pwfisher.com>
* @since August 21, 2009
* @see http://www.php.net/manual/en/features.commandline.php
* #81042 function arguments($argv) by technorati at gmail dot com,
12-Feb-2008
* #78651 function getArgs($args) by B Crawford, 22-Oct-2007
* @usage $args = CommandLine::parseArgs($_SERVER['argv']);
*/
public static function parseArgs($argv){
array_shift($argv);
$out = array();
foreach ($argv as $arg){
// --foo --bar=baz
if (substr($arg,0,2) == '--'){
$eqPos = strpos($arg,'=');
// --foo
if ($eqPos === false){
$key = substr($arg,2);
$value = isset($out[$key]) ? $out[$key] : true;
$out[$key] = $value;
}
// --bar=baz
else {
$key = substr($arg,2,$eqPos-2);
$value = substr($arg,$eqPos+1);
$out[$key] = $value;
}
}
// -k=value -abc
else if (substr($arg,0,1) == '-'){
// -k=value
if (substr($arg,2,1) == '='){
$key = substr($arg,1,1);
$value = substr($arg,3);
$out[$key] = $value;
}
// -abc
else {
$chars = str_split(substr($arg,1));
foreach ($chars as $char){
$key = $char;
$value = isset($out[$key]) ? $out[$key] : true;
$out[$key] = $value;
}
}
}
// plain-arg
else {
$value = $arg;
$out[] = $value;
}
}
return $out;
}
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 71.165.91.32
----
Manual Page -- http://www.php.net/manual/en/function.array-shift.php
Edit -- https://master.php.net/note/edit/93084
Del: integrated -- https://master.php.net/note/delete/93084/integrated
Del: useless -- https://master.php.net/note/delete/93084/useless
Del: bad code -- https://master.php.net/note/delete/93084/bad+code
Del: spam -- https://master.php.net/note/delete/93084/spam
Del: non-english -- https://master.php.net/note/delete/93084/non-english
Del: in docs -- https://master.php.net/note/delete/93084/in+docs
Del: other reasons-- https://master.php.net/note/delete/93084
Reject -- https://master.php.net/note/reject/93084
Search -- https://master.php.net/manage/user-notes.php