3 function getCmdOpt($aArg, $aSpec, &$aResult, $bExitOnError = false, $bExitOnUnknown = false)
8 foreach ($aSpec as $aLine) {
9 if (is_array($aLine)) {
10 if ($aLine[0]) $aQuick['--'.$aLine[0]] = $aLine;
11 if ($aLine[1]) $aQuick['-'.$aLine[1]] = $aLine;
12 $aCounts[$aLine[0]] = 0;
18 $iSize = sizeof($aArg);
19 for ($i = 1; $i < $iSize; $i++) {
20 if (isset($aQuick[$aArg[$i]])) {
21 $aLine = $aQuick[$aArg[$i]];
22 $aCounts[$aLine[0]]++;
24 if ($aLine[4] == $aLine[5]) {
27 for ($n = $aLine[4]; $i < $iSize && $n; $n--) {
29 if ($i >= $iSize || $aArg[$i][0] == '-') showUsage($aSpec, $bExitOnError, 'Parameter of \''.$aLine[0].'\' is missing');
33 $xVal[] = realpath($aArg[$i]);
36 $sPath = realpath(dirname($aArg[$i]));
38 $xVal[] = $sPath . '/' . basename($aArg[$i]);
44 $xVal[] = (bool)$aArg[$i];
47 $xVal[] = (int)$aArg[$i];
50 $xVal[] = (float)$aArg[$i];
57 if ($aLine[4] == 1) $xVal = $xVal[0];
62 fail('Variable numbers of params not yet supported');
66 if (!array_key_exists($aLine[0], $aResult)) $aResult[$aLine[0]] = array();
67 $aResult[$aLine[0]][] = $xVal;
69 $aResult[$aLine[0]] = $xVal;
72 $bUnknown = $aArg[$i];
76 if (array_key_exists('help', $aResult)) showUsage($aSpec);
77 if ($bUnknown && $bExitOnUnknown) showUsage($aSpec, $bExitOnError, 'Unknown option \''.$bUnknown.'\'');
79 foreach ($aSpec as $aLine) {
80 if (is_array($aLine)) {
81 if ($aCounts[$aLine[0]] < $aLine[2]) showUsage($aSpec, $bExitOnError, 'Option \''.$aLine[0].'\' is missing');
82 if ($aCounts[$aLine[0]] > $aLine[3]) showUsage($aSpec, $bExitOnError, 'Option \''.$aLine[0].'\' is pressent too many times');
85 if (!array_key_exists($aLine[0], $aResult))
86 $aResult[$aLine[0]] = false;
94 function showUsage($aSpec, $bExit = false, $sError = false)
97 echo basename($_SERVER['argv'][0]).': '.$sError."\n";
98 echo 'Try `'.basename($_SERVER['argv'][0]).' --help` for more information.'."\n";
101 echo "Usage: ".basename($_SERVER['argv'][0])."\n";
103 foreach ($aSpec as $aLine) {
104 if (is_array($aLine)) {
110 if ($aLine[1]) $aNames[] = '-'.$aLine[1];
111 if ($aLine[0]) $aNames[] = '--'.$aLine[0];
112 $sName = join(', ',$aNames);
113 echo ' '.$sName.str_repeat(' ',30-strlen($sName)).$aLine[7]."\n";
122 function chksql($oSql, $sMsg = false)
124 if (PEAR::isError($oSql)) {
125 fail($sMsg || $oSql->getMessage(), $oSql->userinfo);