note 38403 deleted from function.getopt by philip
| From: | philip@php.net | Date: | Thu, 07 May 2009 22:30:42 +0000 |
| Subject: | note 38403 deleted from function.getopt by philip | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154331@lists.php.net to get a copy of this message | ||
Note Submitter: klewan at chello dot at
----
For all buddies outside who dont get getopt to work :)
here is a php variant with just standard function calls
<?php
//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";
}
?>