File: PhpdocArgvHandler.php

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