3 function getCmdOpt($aArg, $aSpec, &$aResult, $bExitOnError = false, $bExitOnUnknown = false)
8 foreach($aSpec as $aLine)
12 if ($aLine[0]) $aQuick['--'.$aLine[0]] = $aLine;
13 if ($aLine[1]) $aQuick['-'.$aLine[1]] = $aLine;
14 $aCounts[$aLine[0]] = 0;
20 $iSize = sizeof($aArg);
21 for ($i = 1; $i < $iSize; $i++)
23 if (isset($aQuick[$aArg[$i]]))
25 $aLine = $aQuick[$aArg[$i]];
26 $aCounts[$aLine[0]]++;
28 if ($aLine[4] == $aLine[5])
33 for($n = $aLine[4]; $i < $iSize && $n; $n--)
36 if ($i >= $iSize || $aArg[$i][0] == '-') showUsage($aSpec, $bExitOnError, 'Parameter of \''.$aLine[0].'\' is missing');
41 $xVal[] = realpath($aArg[$i]);
44 $sPath = realpath(dirname($aArg[$i]));
46 $xVal[] = $sPath . '/' . basename($aArg[$i]);
51 $xVal[] = (bool)$aArg[$i];
54 $xVal[] = (int)$aArg[$i];
57 $xVal[] = (float)$aArg[$i];
64 if ($aLine[4] == 1) $xVal = $xVal[0];
73 fail('Variable numbers of params not yet supported');
78 if (!array_key_exists($aLine[0], $aResult)) $aResult[$aLine[0]] = array();
79 $aResult[$aLine[0]][] = $xVal;
83 $aResult[$aLine[0]] = $xVal;
88 $bUnknown = $aArg[$i];
92 if (array_key_exists('help', $aResult)) showUsage($aSpec);
93 if ($bUnknown && $bExitOnUnknown) showUsage($aSpec, $bExitOnError, 'Unknown option \''.$bUnknown.'\'');
95 foreach($aSpec as $aLine)
99 if ($aCounts[$aLine[0]] < $aLine[2]) showUsage($aSpec, $bExitOnError, 'Option \''.$aLine[0].'\' is missing');
100 if ($aCounts[$aLine[0]] > $aLine[3]) showUsage($aSpec, $bExitOnError, 'Option \''.$aLine[0].'\' is pressent too many times');
104 if (!array_key_exists($aLine[0], $aResult))
105 $aResult[$aLine[0]] = false;
113 function showUsage($aSpec, $bExit = false, $sError = false)
117 echo basename($_SERVER['argv'][0]).': '.$sError."\n";
118 echo 'Try `'.basename($_SERVER['argv'][0]).' --help` for more information.'."\n";
121 echo "Usage: ".basename($_SERVER['argv'][0])."\n";
123 foreach($aSpec as $aLine)
125 if (is_array($aLine))
133 if ($aLine[1]) $aNames[] = '-'.$aLine[1];
134 if ($aLine[0]) $aNames[] = '--'.$aLine[0];
135 $sName = join(', ',$aNames);
136 echo ' '.$sName.str_repeat(' ',30-strlen($sName)).$aLine[7]."\n";