note 38403 added to function.getopt
| From: | klewan at chello dot at | Date: | Fri, 19 Dec 2003 13:00:54 +0000 |
| Subject: | note 38403 added to function.getopt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-62229@lists.php.net to get a copy of this message | ||
For all buddies outside who dont get getopt to work :)
here is a php variant with just standard function calls
<?
//Set Defaults
$options["a"]="valA";
$options["b"]="valB";
parseArgs($argv,&$options);
//options will be $options["FLAG"] (if not argument is given like -h the
$options["h"] will be set to bolean true
if($options["h"] == true) {
print_usage();
exit(1);
}
var_dump($options);
function parseArgs($a = array(), $r) {
$f=NULL;
for($x = 0; $x < count($a); $x++) {
if($a[$x]{0} == "-") {
$f=$a[$x];
$r[substr($f,1,strlen($f))]=true;
}
if ($f != NULL) {
if (($a[$x+1] != NULL) && ($a[$x+1] != "") &&
($a[$x+1] != "") && ($a[$x+1]{0} != "-")) {
$r[substr($f,1,strlen($f))]=$a[$x+1];
} else {
$f=$a[x+1];
}
}
}
}
function print_usage() {
echo "-a bla bla\n";
echo "-b bla bla\n";
echo "-h display this help\n";
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.getopt.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38403
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38403&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38403&report=yes
Search -- http://master.php.net/manage/user-notes.php