blob: dd4911a180e4faef2ceb1c26c6a12d6d7314b990 (
plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
<?php
/**
* @author stev leibelt <artodeto@bazzline.net>
* @since 2014-08-20
*/
/**
* Class Input
*/
class Input
{
/**
* @var array
*/
private $arguments;
/**
* @var array
*/
private $longOptions;
/**
* @var int
*/
private $numberOfArguments;
/**
* @var array
*/
private $parameters;
/**
* @var array
*/
private $shortOptions;
/**
* @var String
*/
private $string;
/**
* @param String $string
* @param array $arguments
*/
public function __construct(String $string, array $arguments = array())
{
$this->longOptions = array();
$this->parameters = array();
$this->shortOptions = array();
$this->string = $string;
$this->setArguments($arguments);
}
/**
* @return array
*/
public function getArguments()
{
return $this->arguments;
}
/**
* @return int
*/
public function getNumberOfArguments()
{
return $this->numberOfArguments;
}
/**
* @param string $name
* @param null $default
* @return null
*/
public function getParameterValue($name, $default = null)
{
if ($this->hasParameter($name)) {
$value = $this->parameters[$name];
} else {
$value = $default;
}
return $value;
}
/**
* @param string $name
* @return bool
*/
public function hasParameter($name)
{
return (isset($this->parameters[$name]));
}
/**
* @param string $name
*/
public function removeParameters($name)
{
if ($this->hasParameter($name)) {
unset($this->parameters[$name]);
$this->update();
}
}
/**
* @param $name
* @return bool
*/
public function hasLongOption($name)
{
return (isset($this->longOptions[$name]));
}
/**
* @param $longOrShortName
* @return bool
*/
public function hasOption($longOrShortName)
{
$hasOption = $this->hasLongOption($longOrShortName);
if (!$hasOption) {
$hasOption = $this->hasShortOption($longOrShortName);
}
return $hasOption;
}
/**
* @param $name
* @return bool
*/
public function hasShortOption($name)
{
return (isset($this->shortOptions[$name]));
}
/**
* @param string $name
*/
public function removeLongOption($name)
{
if ($this->hasLongOption($name)) {
unset($this->longOptions[$name]);
$this->update();
}
}
/**
* @param string $longOrShortName
*/
public function removeOption($longOrShortName)
{
if ($this->hasLongOption($longOrShortName)) {
unset($this->longOptions[$longOrShortName]);
$this->update();
} else if ($this->hasShortOption($longOrShortName)) {
unset($this->shortOptions[$longOrShortName]);
$this->update();
}
}
/**
* @param string $name
*/
public function removeShortOption($name)
{
if ($this->hasShortOption($name)) {
unset($this->shortOptions[$name]);
$this->update();
}
}
/**
* @param array $arguments
*/
public function setArguments(array $arguments)
{
foreach ($arguments as $argument) {
if ($this->string->startsWith($argument, '--')) {
$this->longOptions[$this->string->cut($argument, 2)] = true;
} else if ($this->string->startsWith($argument, '-')) {
$this->shortOptions[$this->string->cut($argument, 1)] = true;
} else {
$parts = explode('=', $argument);
if (count($parts) > 1) {
$name = array_shift($parts);
$this->parameters[$name] = implode('=', $parts);
}
}
}
$this->update();
}
private function update()
{
$this->arguments = array_merge($this->parameters, $this->shortOptions, $this->longOptions);
$this->numberOfArguments = count($this->arguments);
}
}
|