1
|
|
|
<?php namespace nyx\console\input\formats; |
2
|
|
|
|
3
|
|
|
// Internal dependencies |
4
|
|
|
use nyx\console; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Argv Input |
8
|
|
|
* |
9
|
|
|
* Note: When passing an argv array yourself, ensure it does not contain the scriptname (when using $_SERVER['argv'] |
10
|
|
|
* it will be present as the first element). |
11
|
|
|
* |
12
|
|
|
* @version 0.1.0 |
13
|
|
|
* @author Michal Chojnacki <[email protected]> |
14
|
|
|
* @copyright 2012-2017 Nyx Dev Team |
15
|
|
|
* @link https://github.com/unyx/nyx |
16
|
|
|
*/ |
17
|
|
|
class Argv extends console\Input |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var interfaces\Parser The token parser to be used. |
21
|
|
|
*/ |
22
|
|
|
private $parser; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Constructs a new Argv Input instance. |
26
|
|
|
* |
27
|
|
|
* @param iterable $parameters A sequence of parameters (in the argv format). |
28
|
|
|
* @param interfaces\Parser $parser A Argv to Collections Lexer instance. |
29
|
|
|
*/ |
30
|
|
|
public function __construct(iterable $parameters = null, interfaces\Parser $parser = null) |
31
|
|
|
{ |
32
|
|
|
// If no arguments were passed, let's use the globals returned by the CLI SAPI. |
33
|
|
|
if (!isset($parameters)) { |
34
|
|
|
$parameters = $_SERVER['argv']; |
35
|
|
|
|
36
|
|
|
// Strip the script name from the arguments. |
37
|
|
|
array_shift($parameters); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->parser = $parser; |
41
|
|
|
$this->raw = $parameters instanceof interfaces\Tokens ? $parameters : new tokens\Argv($parameters); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
protected function parse() : console\Input |
48
|
|
|
{ |
49
|
|
|
// Instantiate a Lexer if none has been defined yet. |
50
|
|
|
if (!isset($this->parser)) { |
51
|
|
|
$this->parser = new parsers\Argv; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Fill our parameter value collections with the parsed input. |
55
|
|
|
$this->parser->parse($this->raw, $this->arguments(), $this->options()); |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|