1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yarak\Console\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
|
8
|
|
|
class ArgumentParser extends Parser |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Argument name. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $name = ''; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Array of argument modes to apply. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $modeArray = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Argument description. |
26
|
|
|
* |
27
|
|
|
* @var null|string |
28
|
|
|
*/ |
29
|
|
|
protected $description = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Default arguement value. |
33
|
|
|
* |
34
|
|
|
* @var null|string |
35
|
|
|
*/ |
36
|
|
|
protected $default = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Parse the given arguement string. |
40
|
|
|
* |
41
|
|
|
* @param string $argument |
42
|
|
|
*/ |
43
|
|
|
public function handle($argument) |
44
|
|
|
{ |
45
|
|
|
list($argument, $this->description) = $this->parseDescription($argument); |
46
|
|
|
|
47
|
|
|
$argument = $this->parseArray($argument, 'IS_ARRAY'); |
48
|
|
|
|
49
|
|
|
$argument = $this->findOptional($argument); |
50
|
|
|
|
51
|
|
|
if (strpos($argument, '=')) { |
52
|
|
|
list($argument, $this->default) = explode('=', $argument); |
53
|
|
|
|
54
|
|
|
$this->modeArray[] = 'OPTIONAL'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!in_array('OPTIONAL', $this->modeArray)) { |
58
|
|
|
$this->modeArray[] = 'REQUIRED'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->command->addArgument( |
62
|
|
|
$argument, |
63
|
|
|
$this->calculateMode($this->modeArray, InputArgument::class), |
64
|
|
|
$this->description, |
65
|
|
|
$this->default |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Parse the argument mode. |
71
|
|
|
* |
72
|
|
|
* @param string $argument |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
protected function findOptional($argument) |
77
|
|
|
{ |
78
|
|
|
if (substr($argument, -1) === '?') { |
79
|
|
|
$this->modeArray[] = 'OPTIONAL'; |
80
|
|
|
|
81
|
|
|
return str_replace('?', '', $argument); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $argument; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|