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