note 81042 modified in features.commandline by danbrown
| From: | danbrown@php.net | Date: | Tue, 25 Nov 2008 15:41:00 +0000 |
| Subject: | note 81042 modified in features.commandline by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146915@lists.php.net to get a copy of this message | ||
Here's an update to the script a couple of people gave below to read arguments from $argv of
the form --name=VALUE and -flag. Changes include:
Don't use $_ARG - $_ is generally considered reserved for the engine.
Don't use regex where a string operation will do just as nicely
Don't overwrite --name=VALUE with -flag when 'name' and 'flag' are the same
thing
Allow for VALUE that has an equals sign in it
<?php
function arguments($argv) {
$ARG = array();
foreach ($argv as $arg) {
if (strpos($arg, '--') === 0) {
$compspec = explode('=', $arg);
$key = str_replace('--', '', array_shift($compspec));
$value = join('=', $compspec);
$ARG[$key] = $value;
} elseif (strpos($arg, '-') === 0) {
$key = str_replace('-', '', $arg);
if (!isset($ARG[$key])) $ARG[$key] = true;
}
}
return $ARG;
}
?>
--was--
Here's an update to the script a couple of people gave below to read arguments from $argv of
the form --name=VALUE and -flag. Changes include:
Don't use $_ARG - $_ is generally considered reserved for the engine.
Don't use regex where a string operation will do just as nicely
Don't overwrite --name=VALUE with -flag when 'name' and 'flag' are the same
thing
Allow for VALUE that has an equals sign in it
function arguments($argv) {
$ARG = array();
foreach ($argv as $arg) {
if (strpos($arg, '--') === 0) {
$compspec = explode('=', $arg);
$key = str_replace('--', '', array_shift($compspec));
$value = join('=', $compspec);
$ARG[$key] = $value;
} elseif (strpos($arg, '-') === 0) {
$key = str_replace('-', '', $arg);
if (!isset($ARG[$key])) $ARG[$key] = true;
}
}
return $ARG;
}
http://php.net/manual/en/features.commandline.php