note 101713 deleted from function.getopt by danbrown
| From: | danbrown@php.net | Date: | Thu, 06 Jan 2011 16:47:01 +0000 |
| Subject: | note 101713 deleted from function.getopt by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-175492@lists.php.net to get a copy of this message | ||
Note Submitter: jeff at joedog dot org
----
I ported GNU Getopt to PHP. It supports short and long opts and its behavior is nearly identical to
the java port. Here's a snippet which implements the class:
#!/usr/bin/php
<?php
require_once("gnu/getopt/Getopt.php");
require_once("gnu/getopt/Longopt.php");
$longopt = array();
$longopt[0] = new LongOpt("help", NO_ARGUMENT, null, 'h');
$longopt[1] = new LongOpt("about", REQUIRED_ARGUMENT, null, 'a');
$getopt = new Getopt($argv, "a:bc:d:hW;", $longopt);
$c;
$arg;
while (($c = $getopt->getopts()) != -1) {
switch($c) {
case 'a':
case 'd':
$arg = $getopt->getOptarg();
print("You picked $c with an argument of ".(($arg != null) ? $arg :
"null")."\n");
break;
case 'b':
case 'c':
$arg = $getopt->getOptarg();
print("You picked $c with an argument of ".(($arg != null) ? $arg :
"null") . "\n");
break;
case 'h':
print "Usage: test [options] \n";
break;
case '?':
break; // getopt() already printed an error
default:
print("getopt() returned $c\n");
}
}
?>
For the code go here: http://www.joedog.org/index/articles-gnu-getopt