note 81042 deleted from features.commandline by cmb
| From: | cmb@php.net | Date: | Sun, 18 Dec 2016 10:19:33 +0000 |
| Subject: | note 81042 deleted from features.commandline by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208665@lists.php.net to get a copy of this message | ||
Note Submitter: technorati at gmail dot com
----
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;
}
?>