1 | <?php |
||
7 | class Manager |
||
8 | { |
||
9 | /** |
||
10 | * An array of arguments passed to the program. |
||
11 | * |
||
12 | * @var Argument[] $arguments |
||
13 | */ |
||
14 | protected $arguments = []; |
||
15 | |||
16 | /** |
||
17 | * A program's description. |
||
18 | * |
||
19 | * @var string $description |
||
20 | */ |
||
21 | protected $description; |
||
22 | |||
23 | /** |
||
24 | * Filter class to find various types of arguments |
||
25 | * |
||
26 | * @var League\CLImate\Argument\Filter $filter |
||
27 | */ |
||
28 | protected $filter; |
||
29 | |||
30 | /** |
||
31 | * Summary builder class |
||
32 | * |
||
33 | * @var \League\CLImate\Argument\Summary $summary |
||
34 | */ |
||
35 | protected $summary; |
||
36 | |||
37 | /** |
||
38 | * Argument parser class |
||
39 | * |
||
40 | * @var \League\CLImate\Argument\Parser $parser |
||
41 | */ |
||
42 | protected $parser; |
||
43 | |||
44 | 924 | public function __construct() |
|
45 | { |
||
46 | 924 | $this->filter = new Filter(); |
|
47 | 924 | $this->summary = new Summary(); |
|
48 | 924 | $this->parser = new Parser(); |
|
49 | 924 | } |
|
50 | |||
51 | /** |
||
52 | * Add an argument. |
||
53 | * |
||
54 | * @throws \Exception if $argument isn't an array or Argument object. |
||
55 | * @param Argument|string|array $argument |
||
56 | * @param $options |
||
57 | */ |
||
58 | 20 | public function add($argument, array $options = []) |
|
75 | |||
76 | /** |
||
77 | * Add multiple arguments to a CLImate script. |
||
78 | * |
||
79 | * @param array $arguments |
||
80 | */ |
||
81 | 16 | protected function addMany(array $arguments = []) |
|
82 | { |
||
83 | 16 | foreach ($arguments as $name => $options) { |
|
84 | 16 | $this->add($name, $options); |
|
85 | 16 | } |
|
86 | 16 | } |
|
87 | |||
88 | /** |
||
89 | * Determine if an argument exists. |
||
90 | * |
||
91 | * @param string $name |
||
92 | * @return bool |
||
93 | */ |
||
94 | 4 | public function exists($name) |
|
98 | |||
99 | /** |
||
100 | * Retrieve an argument's value. |
||
101 | * |
||
102 | * @param string $name |
||
103 | * @return string|int|float|bool|null |
||
104 | */ |
||
105 | 4 | public function get($name) |
|
109 | |||
110 | /** |
||
111 | * Retrieve all arguments. |
||
112 | * |
||
113 | * @return Argument[] |
||
114 | */ |
||
115 | 12 | public function all() |
|
119 | |||
120 | /** |
||
121 | * Determine if an argument has been defined on the command line. |
||
122 | * |
||
123 | * This can be useful for making sure an argument is present on the command |
||
124 | * line before parse()'ing them into argument objects. |
||
125 | * |
||
126 | * @param string $name |
||
127 | * @param array $argv |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | 4 | public function defined($name, array $argv = null) |
|
149 | |||
150 | /** |
||
151 | * Check if the defined argument matches the command argument. |
||
152 | * |
||
153 | * @param Argument $argument |
||
154 | * @param string $command_argument |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | 4 | protected function isArgument($argument, $command_argument) |
|
173 | |||
174 | /** |
||
175 | * Retrieve all arguments as key/value pairs. |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | 4 | public function toArray() |
|
180 | { |
||
181 | 4 | $return = []; |
|
182 | |||
183 | 4 | foreach ($this->all() as $name => $argument) { |
|
184 | 4 | $return[$name] = $argument->value(); |
|
185 | 4 | } |
|
186 | |||
187 | 4 | return $return; |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * Set a program's description. |
||
192 | * |
||
193 | * @param string $description |
||
194 | */ |
||
195 | 4 | public function description($description) |
|
199 | |||
200 | /** |
||
201 | * Output a script's usage statement. |
||
202 | * |
||
203 | * @param CLImate $climate |
||
204 | * @param array $argv |
||
205 | */ |
||
206 | 4 | public function usage(CLImate $climate, array $argv = null) |
|
215 | |||
216 | /** |
||
217 | * Parse command line arguments into CLImate arguments. |
||
218 | * |
||
219 | * @throws \Exception if required arguments aren't defined. |
||
220 | * @param array $argv |
||
221 | */ |
||
222 | 8 | public function parse(array $argv = null) |
|
228 | |||
229 | /** |
||
230 | * Get the trailing arguments |
||
231 | * |
||
232 | * @return string|null |
||
233 | */ |
||
234 | public function trailing() |
||
238 | } |
||
239 |