]> git.openstreetmap.org Git - nominatim.git/blob - lib/cmd.php
Merge pull request #524 from mtmail/PSR2-function-declarations
[nominatim.git] / lib / cmd.php
1 <?php
2
3 function getCmdOpt($aArg, $aSpec, &$aResult, $bExitOnError = false, $bExitOnUnknown = false)
4 {
5     $aQuick = array();
6     $aCounts = array();
7
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;
13         }
14     }
15
16     $aResult = array();
17     $bUnknown = false;
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]]++;
23             $xVal = null;
24             if ($aLine[4] == $aLine[5]) {
25                 if ($aLine[4]) {
26                     $xVal = array();
27                     for ($n = $aLine[4]; $i < $iSize && $n; $n--) {
28                         $i++;
29                         if ($i >= $iSize || $aArg[$i][0] == '-') showUsage($aSpec, $bExitOnError, 'Parameter of  \''.$aLine[0].'\' is missing');
30
31                         switch ($aLine[6]) {
32                             case 'realpath':
33                                 $xVal[] = realpath($aArg[$i]);
34                                 break;
35                             case 'realdir':
36                                 $sPath = realpath(dirname($aArg[$i]));
37                                 if ($sPath) {
38                                     $xVal[] = $sPath . '/' . basename($aArg[$i]);
39                                 } else {
40                                     $xVal[] = $sPath;
41                                 }
42                                 break;
43                             case 'bool':
44                                 $xVal[] = (bool)$aArg[$i];
45                                 break;
46                             case 'int':
47                                 $xVal[] = (int)$aArg[$i];
48                                 break;
49                             case 'float':
50                                 $xVal[] = (float)$aArg[$i];
51                                 break;
52                             default:
53                                 $xVal[] = $aArg[$i];
54                                 break;
55                         }
56                     }
57                     if ($aLine[4] == 1) $xVal = $xVal[0];
58                 } else {
59                     $xVal = true;
60                 }
61             } else {
62                 fail('Variable numbers of params not yet supported');
63             }
64
65             if ($aLine[3] > 1) {
66                 if (!array_key_exists($aLine[0], $aResult)) $aResult[$aLine[0]] = array();
67                 $aResult[$aLine[0]][] = $xVal;
68             } else {
69                 $aResult[$aLine[0]] = $xVal;
70             }
71         } else {
72             $bUnknown = $aArg[$i];
73         }
74     }
75
76     if (array_key_exists('help', $aResult)) showUsage($aSpec);
77     if ($bUnknown && $bExitOnUnknown) showUsage($aSpec, $bExitOnError, 'Unknown option \''.$bUnknown.'\'');
78
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');
83             switch ($aLine[6]) {
84                 case 'bool':
85                     if (!array_key_exists($aLine[0], $aResult))
86                         $aResult[$aLine[0]] = false;
87                     break;
88             }
89         }
90     }
91     return $bUnknown;
92 }
93
94 function showUsage($aSpec, $bExit = false, $sError = false)
95 {
96     if ($sError) {
97         echo basename($_SERVER['argv'][0]).': '.$sError."\n";
98         echo 'Try `'.basename($_SERVER['argv'][0]).' --help` for more information.'."\n";
99         exit;
100     }
101     echo "Usage: ".basename($_SERVER['argv'][0])."\n";
102     $bFirst = true;
103     foreach ($aSpec as $aLine) {
104         if (is_array($aLine)) {
105             if ($bFirst) {
106                 $bFirst = false;
107                 echo "\n";
108             }
109             $aNames = array();
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";
114         } else {
115             echo $aLine."\n";
116         }
117     }
118     echo "\n";
119     exit;
120 }
121
122 function chksql($oSql, $sMsg = false)
123 {
124     if (PEAR::isError($oSql)) {
125         fail($sMsg || $oSql->getMessage(), $oSql->userinfo);
126     }
127
128     return $oSql;
129 }