1st, make sure you have the Console_Getopt installed:
C:\php>pear list
INSTALLED PACKAGES, CHANNEL PEAR.PHP.NET:
=========================================
PACKAGE VERSION STATE
Archive_Tar 1.3.2 stable
Console_Getopt 1.2.3 stable
DB 1.7.12 stable
HTML_BBCodeParser 1.2.2 stable
Log 1.9.11 stable
Net_DNS 1.0.0 stable
PEAR 1.7.2 stable
Structures_Graph 1.0.2 stable
Here’s the sample code:
<?php
require_once ‘Console/Getopt.php’; //include the required files
$cg = new Console_Getopt; //create the object
$shortoptions= “hc:”; //short version of the command line options
$longoptions= array(”help”,”count=”); //long version
$options= $cg->getopt($cg->readPHPArgv(), $shortoptions,$longoptions);
if( PEAR::isError( $options)){
die(”Error in command line:”.$options->getMessage().”\n”);
}
if( sizeof($options[0] >0)){
foreach($options[0] as $o){
switch( $o[0]){
case “–help”:
case “h”:
print “Help messages\n”; break;
case “–count”:
case “c”:
$count= $o[1]; print “The count you specified is $count\n”; break;
}
}
}
?>
Now test the code:
C:\php>php test.php –help
Help messages
C:\php>php test.php -h
Help messages
C:\php>php test.php –count=1
The count you specified is 1
C:\php>php test.php -c 1
The count you specified is 1
Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer