1
|
|
|
<?php namespace nyx\console\input\formats\tokens; |
2
|
|
|
|
3
|
|
|
// External dependencies |
4
|
|
|
use nyx\core; |
5
|
|
|
|
6
|
|
|
// Internal dependencies |
7
|
|
|
use nyx\console\input; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Argv Input Tokens |
11
|
|
|
* |
12
|
|
|
* This Tokens collection is able to resolve Input Options by name, as the name is contained in the argv data, |
13
|
|
|
* but will not be able to do the same for Input Arguments, as their names are unknown until the input gets |
14
|
|
|
* bound to a Definition. |
15
|
|
|
* |
16
|
|
|
* @version 0.1.0 |
17
|
|
|
* @author Michal Chojnacki <[email protected]> |
18
|
|
|
* @copyright 2012-2017 Nyx Dev Team |
19
|
|
|
* @link https://github.com/unyx/nyx |
20
|
|
|
*/ |
21
|
|
|
class Argv extends core\collections\Map implements input\formats\interfaces\Tokens |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function get($parameter, $default = null) |
27
|
|
|
{ |
28
|
|
|
foreach ($this->items as $token) { |
29
|
|
|
if ($token === $parameter || 0 === strpos($token, $parameter . '=')) { |
30
|
|
|
if (false !== $pos = strpos($parameter, '=')) { |
31
|
|
|
return substr($token, $pos + 1); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $token; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $default; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function has($parameter) : bool |
45
|
|
|
{ |
46
|
|
|
foreach ($this->items as $token) { |
47
|
|
View Code Duplication |
if ($token === $parameter || 0 === strpos($token, $parameter . '=')) { |
|
|
|
|
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function remove($parameter) : core\collections\interfaces\Map |
59
|
|
|
{ |
60
|
|
|
foreach ($this->items as $key => $token) { |
61
|
|
View Code Duplication |
if ($token === $parameter || 0 === strpos($token, $parameter . '=')) { |
|
|
|
|
62
|
|
|
unset($this->items[$key]); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
*/ |
72
|
|
|
public function getArguments() : array |
73
|
|
|
{ |
74
|
|
|
$arguments = []; |
75
|
|
|
|
76
|
|
|
foreach ($this->items as $token) { |
77
|
|
|
if ($token[0] !== '-') { |
78
|
|
|
$arguments[] = $token; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $arguments; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritDoc} |
87
|
|
|
*/ |
88
|
|
|
public function getOptions() : array |
89
|
|
|
{ |
90
|
|
|
$options = []; |
91
|
|
|
|
92
|
|
|
foreach ($this->items as $token) { |
93
|
|
|
if ($token !== '--' && $token[0] === '-') { |
94
|
|
|
$options[] = $token; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $options; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.