PHP/Utility Function/argv
Parsing commmand-line arguments
<?php
for ($i = 1; $i < $argc; $i++) {
switch ($argv[$i]) {
case "-v":
// set a flag
$verbose = true;
break;
case "-c":
// advance to the next argument
$i++;
// if it"s set, save the value
if (isset($argv[$i])) {
$config_file = $argv[$i];
} else {
// quit if no filename specified
die("Must specify a filename after -c");
}
break;
case "-q":
$quiet = true;
break;
default:
die("Unknown argument: ".$argv[$i]);
break;
}
}
?>
Using $argc and $argv in CLI PHP
<?php
if(!isset($argv[1]) || ($argv[1] != "-d")) {
echo "Usage:\n";
echo "\n{$argv[0]} -d\n";
exit(-1);
}
echo "You passed the command line argument "-d"\n";
exit(0);
?>