changed git call from https to git readonly
[atutor.git] / mods / phpdoc / PHPDoc / core / PhpdocArgvHandler.php
1 <?php\r
2 /**\r
3 * Handles command line arguments.\r
4 *\r
5 * Be careful the source has not been tested yet, it's probably very buggy.\r
6 * Any help and comments are welcome...\r
7 *\r
8 * @author               Ulf Wendel <ulf@redsys.de>\r
9 * @version      $Id: PhpdocArgvHandler.php,v 1.2 2000/12/03 20:30:42 uw Exp $\r
10 */\r
11 class PhpdocArgvHandler extends PhpdocObject {\r
12         \r
13         /**\r
14         * Message explaining the usage of phpdoc on the command line.\r
15         * \r
16         * Actually it's not the message itself but an array containing\r
17         * the instructions. The array is indexed by the command line option e.g. "-h".\r
18         * The array values hold a short message describing the  usage of the option.\r
19         * \r
20         * @var          array\r
21         * @access       private\r
22         */\r
23         var $COMMANDS = array(\r
24                                                                                                         "-f filename [, filename]"      => "name of files to parse",\r
25                                                                                                         "-d directory"                                                  => "name of a directory to parse",\r
26                                                                                                         "-p path"                                                                               => "path of the files",\r
27                                                                                                         "-t target"                                                             => "path where to save the generated files, default is the current path",\r
28                                                                                                         "-h"                                                                                            => "show this help message"\r
29                                                                                                 );\r
30         \r
31         \r
32         /**\r
33         * Handle the command line values\r
34         * \r
35         * handleArgv() looks for command line values and \r
36         * interprets them. If there're unknown command it prints\r
37         * a message and calls die()\r
38         */      \r
39         function handleArgv() {\r
40                 global $argv, $argc;\r
41                 \r
42                 // the first argv is the name of the script,\r
43                 // so there must be at least another one\r
44                 if ($argc<2) {\r
45                         $error = "\n\nCould not understand your request.\n\n";\r
46                         $error.= $this->getArgvHelpMessage();\r
47                         print $error;\r
48                         die();\r
49                 }\r
50                 \r
51                 $commands = 0;\r
52                 $errors = array();\r
53                 \r
54                 reset($argv);\r
55                 \r
56                 // skip the fist, it's the name of the php script\r
57                 next($argv);\r
58                 \r
59                 while (list($k, $arg)=each($argv)) {\r
60                         // valid command?\r
61                         if ("-"!=substr($arg, 0, 1))\r
62                                 continue;\r
63                         \r
64                         $cmd            = substr($arg, 1, 2);                           \r
65                         $value  = trim(substr($arg, 3));\r
66                         \r
67                         // all command line options except -h require values\r
68                         if (""==$value && "h"!=$cmd) {\r
69                                 $errors[] = array( \r
70                                                                                                                 "msg"   => sprintf("-%s: no value found", trim($cmd)),\r
71                                                                                                                 "type"  => "argv"\r
72                                                                                                         );\r
73                                 // skip this command\r
74                                 continue;\r
75                         }\r
76                         \r
77                         switch ($cmd) {\r
78                                 case "f ":\r
79                                         $files = explode(",", substr($arg, 3));\r
80                                         $this->setFiles($files);\r
81                                         $commands++;\r
82                                         break;\r
83                                 \r
84                                 case "d ":\r
85                                         $this->setDirectory($value);\r
86                                         $commands++;\r
87                                         break;\r
88                                         \r
89                                 case "p ":\r
90                                         $this->setPath($value);\r
91                                         $commands++;\r
92                                         break;\r
93                                         \r
94                                 case "t ":\r
95                                         $this->setTarget($value);\r
96                                         $commands++;\r
97                                         break;\r
98                                 \r
99                                 case "h ":\r
100                                         $commands++;\r
101                                         break;\r
102                                         \r
103                                 default:\r
104                                         $errors[]="unknown command: '$arg'";\r
105                                         break;\r
106                         }\r
107                         \r
108                 }\r
109                 \r
110                 // are there enough informations to start work?\r
111                 $errors = $this->checkStatus($errors);\r
112                 \r
113                 // check for errors and die() if neccessary\r
114                 if (count($errors)>0) {\r
115                         $error = "\n\nCould not understand your request.\n\n";\r
116                         reset($errors);\r
117                         while (list($k, $data)=each($errors)) \r
118                                 $error.=$data["msg"]."\n";\r
119                         \r
120                         $error.= $this->getArgvHelpMessage();\r
121                         print $error;\r
122                         die();\r
123                 }\r
124                                 \r
125                 // no errors, but no recognized commands? die() if neccessary\r
126                 if (0==$commands) {\r
127                         $error = "\n\nCould not understand your request.\n\n";\r
128                         $error.= $this->getArgvHelpMessage();\r
129                         print $error;\r
130                         die();\r
131                 }\r
132                 \r
133                 // YEAH everything is fine, we can start working!\r
134                 $this->parse();         \r
135         } // end func handleArgv\r
136         \r
137         /**\r
138         * Returns the current help message of phpdoc\r
139         * \r
140         * The message is not HTML formated, it could be shown \r
141         * on the command line. \r
142         *\r
143         * @access       private\r
144         * @return       string  $help_msg       Some instructions on available command line options\r
145         * @see          handleArgv(), $COMMANDS\r
146         */      \r
147         function getArgvHelpMessage() {\r
148         \r
149                 $help_msg = "";\r
150                 \r
151                 // generate the message from the COMMAND array\r
152                 reset($this->COMMANDS);\r
153                 while (list($param, $explanation)=each($this->COMMANDS)) \r
154                         $help_msg.= sprintf("%-28s%s\n", $param, $explanation);\r
155                 \r
156                 $help_msg.="\nFurter information can be found in the documentation.\n";\r
157                 return $help_msg;               \r
158         } // end func getArgvHelpMessage\r
159         \r
160 } // end class PhpdocArgvHandler\r
161 ?>