1
|
|
|
<?php namespace Tarsana\Command\Commands; |
2
|
|
|
|
3
|
|
|
use Tarsana\Command\Helpers\SyntaxHelper; |
4
|
|
|
use Tarsana\Command\SubCommand; |
5
|
|
|
use Tarsana\Syntax\ArraySyntax; |
6
|
|
|
use Tarsana\Syntax\ObjectSyntax; |
7
|
|
|
use Tarsana\Syntax\OptionalSyntax; |
8
|
|
|
use Tarsana\Syntax\Syntax; |
9
|
|
|
|
10
|
|
|
class HelpCommand extends SubCommand { |
11
|
|
|
|
12
|
|
|
protected $helper; |
13
|
|
|
|
14
|
|
|
protected function init() |
15
|
|
|
{ |
16
|
|
|
$this->name('Help') |
17
|
|
|
->description('Shows the help message.'); |
18
|
|
|
$this->helper = SyntaxHelper::instance(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function setupSubCommands() |
22
|
|
|
{ |
23
|
|
|
return $this; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function execute() |
27
|
|
|
{ |
28
|
|
|
$parent = $this->parent; |
29
|
|
|
|
30
|
|
|
$text = "<info>{$parent->name}</info> version <info>{$parent->version}</info>" |
31
|
|
|
. "<br><br>{$parent->description}<br><br>" |
32
|
|
|
. $this->syntaxHelp() |
33
|
|
|
. $this->optionsHelp() |
34
|
|
|
. $this->subCommandsHelp(); |
35
|
|
|
|
36
|
|
|
$this->console()->out($text); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function syntaxHelp() : string |
40
|
|
|
{ |
41
|
|
|
$syntax = $this->parent->syntax(); |
42
|
|
|
$helper = $this->helper; |
43
|
|
|
$text = ''; |
44
|
|
|
|
45
|
|
|
if ($syntax) { |
46
|
|
|
$string = $helper->asString($syntax); |
47
|
|
|
$text .= "Syntax: <success>[options] {$string}</success><br>" |
48
|
|
|
. "Arguments:<br>"; |
49
|
|
|
foreach ($syntax->fields() as $name => $s) { |
50
|
|
|
$text .= $this->fieldHelp($name, $s); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $text; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function optionsHelp() : string |
58
|
|
|
{ |
59
|
|
|
$options = array_keys($this->parent->options()); |
60
|
|
|
$text = ''; |
61
|
|
|
if (!empty($options)) { |
62
|
|
|
$text .= 'Options:<br>'; |
63
|
|
|
foreach ($options as $name) { |
64
|
|
|
$description = $this->parent()->describe($name); |
65
|
|
|
$text .= "<tab><warn>{$name}</warn> {$description}<br>"; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $text; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function subCommandsHelp() : string |
73
|
|
|
{ |
74
|
|
|
$subCommands = $this->parent->commands(); |
75
|
|
|
$text = ''; |
76
|
|
|
if (!empty($subCommands)) { |
77
|
|
|
$text .= 'SubCommands:<br>'; |
78
|
|
|
foreach ($subCommands as $name => $cmd) { |
79
|
|
|
$text .= "<tab><warn>{$name}</warn> {$cmd->description()}<br>"; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $text; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function fieldHelp( |
87
|
|
|
string $name, Syntax $s, string $prefix = '', int $level = 1 |
88
|
|
|
) : string |
89
|
|
|
{ |
90
|
|
|
$tabs = str_repeat('<tab>', $level); |
91
|
|
|
$optional = ($s instanceof OptionalSyntax); |
92
|
|
|
if ($optional) |
93
|
|
|
$default = 'default: ' . json_encode($s->getDefault()); |
94
|
|
|
else |
95
|
|
|
$default = 'required'; |
96
|
|
|
$description = $this->parent()->describe($prefix.$name); |
97
|
|
|
$syntax = $this->helper->asString($s); |
98
|
|
|
$text = "{$tabs}<warn>{$name}</warn> <success>{$syntax}</success>" |
99
|
|
|
. " {$description} <info>({$default})</info><br>"; |
100
|
|
|
$level ++; |
101
|
|
|
$prefix .= $name . '.'; |
102
|
|
|
foreach ($this->helper->fields($s) as $field => $syntax) { |
103
|
|
|
$text .= $this->fieldHelp($field, $syntax, $prefix, $level); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $text; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|