getopt() simply ignores unnecessary options specified in argv.
Many times, it does not work well to handle errors in command line.
A package PEAR::Console_Getopt can handle this problem, but it requires additonal installation.
GNU getopt(1) does well at shell level.
Following is my extended getopt() can detect unnecessary options:
<?php
function getoptex($sopt, $lopt, &$ind)
{
global $argv, $argc;
$sopts = getopt($sopt, $lopt);
$sopts_cnt = count($sopts); $asopt = $sopt . implode("", range("a", "z")) . implode("", range("A", "Z")) . implode("", range("0", "9"));
$asopts = getopt($asopt, $lopt);
$asopts_cnt = count($asopts); $lopt_trim = array();
foreach ($lopt as $o) {
$lopt_trim[] = trim($o, ":");
}
$alopts_cnt = 0;
$alopts_flag = true;
for ($i = 1; $i < $argc; $i++) {
if ($argv[$i] === "--") { break;
}
if (strpos($argv[$i], "--") === 0) { $alopts_cnt++;
$o = substr($argv[$i], 2);
if (! in_array($o, $lopt_trim)) { $alopts_flag = false;
} else {
if (in_array(($o . ":"), $lopt)) { $i++; if ($i >= $argc) { $alopts_flag = false;
break;
}
}
}
}
}
if ($sopts_cnt != $asopts_cnt || (! $alopts_flag)) {
return false;
} else {
return getopt($sopt, $lopt, $ind);
}
}
?>