/
Params.class.php
179 lines (159 loc) · 4.99 KB
/
Params.class.php
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* Reads parameters passed in from the command line and verifies they all exist.
* Provides a useful object to manipulate cmd parameters
*
* @author Ben Lobaugh <ben@lobaugh.net>
* @url http://ben.lobaugh.net
* @license Free to use and modify as you choose. Please give credits.
*/
class Params {
/**
* Holds a list of parameters for the command line tools
*
* array (<param_name>, array(<required|optional>, <value>, <message>, <get_from>))
* @var Array
*/
private $mParams;
/**
* Holds a list of missing required parameters
*
* @var String
*/
private $mError;
public function __construct(){
$this->buildmParams();
}
/**
* Returns the number of parameters passed
*
* @return Integer
*/
public function count() {
global $argv;
return count($argv);
}
/**
* Adds a new parameter to the list of parameters that should be available
*
* @param String $name - The flag to be used on the parameter
* @param Boolean $required - Boolean indicating if the parameter is required to be on the command line
* @param String $default_value - Default value for optional parameters
* @param String $message - Helpful message to be displayed to a user describing the parameter
*/
public function add($name, $required, $default_value, $message) {
$this->mParams[$name] = array('required' => $required, 'default_value' => $default_value, 'message' => $message);
}
/**
* Removes a parameter from the list of available parameters
*
* @param String $name
*/
public function remove($name) {
unset($this->mParams[$name]);
}
/**
* Checks the passed in parameters against the required list to ensure
* all parameters that are required are present
*/
public function verify() {
$params = $this->getCmdParams();
$keys = array_keys($params);
$msg = "";
if(is_array($this->mParams)) {
foreach($this->mParams AS $k => $v) {
if(isset($params[$k])) $this->set($k, $params[$k]); // Set all values from the cmd line params
if($v['required'] /* required */ && (!in_array($k, $keys) || is_null($params[$k]))) {
$msg .= "\n{$k} - {$v[2]}";
}
}
if($msg != '') {
$this->mError = $msg;
return false;
}
}
return true;
}
/**
* Builds up $this->mParams with the parameters that currently exist
*/
private function buildmParams() {
$params = $this->getCmdParams();
foreach($params as $k => $v) {
$this->add($k, false, 'unknown', 'Auto-added from command line parameter');
$this->set($k, $v);
}
}
/**
* Returns the error message from verify()
* @return String
*/
public function getError() {
return $this->mError;
}
/**
* Sets the value of a command line parameter
*
* @param String $param
* @param String $value
*/
public function set($param, $value) {
$this->mParams[$param]['value'] = $value;
}
/**
* Returns the value of a single command line parameter
*
* @param String $param
* @return String
*/
public function get($param) {
if(isset($this->mParams[$param]['value'])) return $this->mParams[$param]['value'];
if(!isset($this->mParams[$param])) return false;
return $this->mParams[$param]['default_value'];
}
/**
* Returns an associative array of the parameters passed via the command
* line or set through a default
*
* @return Array
*/
public function valueArray() {
$arr = array();
if(is_array($this->mParams)) {
foreach($this->mParams AS $k => $v) {
$arr[$k] = $v['value'];
}
}
return $arr;
}
/**
* Returns all the parameters passed by the command line as key/value pairs.
* If a flag is used (param with no value) it will be set to true
*
* @global Array $argv
* @return Array
*/
private function getCmdParams() {
global $argv;
$params = array();
for($i = 0; $i < count($argv); $i++) {
if(substr($argv[$i], 0, 1) == '-') {
if($i <= count($argv)-2 && substr($argv[($i + 1)], 0, 1) != '-') {
// Next item is flag
$value = $argv[$i + 1];
} else {
$value = "true";
}
$key = str_replace("-", "", $argv[$i]);
$params[$key] = $value;
}
}
return $params;
}
/**
* Convert this object into a string
*/
public function __toString() {
return print_r($this->mParams, true);
}
} // end class