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