|
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 execute() |
|
22
|
|
|
{ |
|
23
|
|
|
$parent = $this->parent; |
|
24
|
|
|
|
|
25
|
|
|
$text = "<info>{$parent->name}</info> version <info>{$parent->version}</info>" |
|
26
|
|
|
. "<br><br>{$parent->description}<br><br>" |
|
27
|
|
|
. $this->syntaxHelp() |
|
28
|
|
|
. $this->optionsHelp() |
|
29
|
|
|
. $this->subCommandsHelp(); |
|
30
|
|
|
|
|
31
|
|
|
$this->console()->out($text); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function syntaxHelp() : string |
|
35
|
|
|
{ |
|
36
|
|
|
$syntax = $this->parent->syntax(); |
|
37
|
|
|
$helper = $this->helper; |
|
38
|
|
|
$text = ''; |
|
39
|
|
|
|
|
40
|
|
|
if ($syntax) { |
|
41
|
|
|
$string = $helper->asString($syntax); |
|
42
|
|
|
$text .= "Syntax: <success>[options] {$string}</success><br>" |
|
43
|
|
|
. "Arguments:<br>"; |
|
44
|
|
|
foreach ($syntax->fields() as $name => $s) { |
|
45
|
|
|
$text .= $this->fieldHelp($name, $s); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $text; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function optionsHelp() : string |
|
53
|
|
|
{ |
|
54
|
|
|
$options = array_keys($this->parent->options()); |
|
55
|
|
|
$text = ''; |
|
56
|
|
|
if (!empty($options)) { |
|
57
|
|
|
$text .= 'Options:<br>'; |
|
58
|
|
|
foreach ($options as $name) { |
|
59
|
|
|
$description = $this->parent()->describe($name); |
|
60
|
|
|
$text .= "<tab><warn>{$name}</warn> {$description}<br>"; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $text; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected function subCommandsHelp() : string |
|
68
|
|
|
{ |
|
69
|
|
|
$subCommands = $this->parent->commands(); |
|
70
|
|
|
$text = ''; |
|
71
|
|
|
if (!empty($subCommands)) { |
|
72
|
|
|
$text .= 'SubCommands:<br>'; |
|
73
|
|
|
foreach ($subCommands as $name => $cmd) { |
|
74
|
|
|
$text .= "<tab><warn>{$name}</warn> {$cmd->description()}<br>"; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $text; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function fieldHelp( |
|
82
|
|
|
string $name, Syntax $s, string $prefix = '', int $level = 1 |
|
83
|
|
|
) : string |
|
84
|
|
|
{ |
|
85
|
|
|
$tabs = str_repeat('<tab>', $level); |
|
86
|
|
|
$optional = ($s instanceof OptionalSyntax); |
|
87
|
|
|
if ($optional) |
|
88
|
|
|
$default = 'default: ' . json_encode($s->getDefault()); |
|
89
|
|
|
else |
|
90
|
|
|
$default = 'required'; |
|
91
|
|
|
$description = $this->parent()->describe($prefix.$name); |
|
92
|
|
|
$syntax = $this->helper->asString($s); |
|
93
|
|
|
$text = "{$tabs}<warn>{$name}</warn> <success>{$syntax}</success>" |
|
94
|
|
|
. " {$description} <info>({$default})</info><br>"; |
|
95
|
|
|
$level ++; |
|
96
|
|
|
$prefix .= $name . '.'; |
|
97
|
|
|
foreach ($this->helper->fields($s) as $field => $syntax) { |
|
98
|
|
|
$text .= $this->fieldHelp($field, $syntax, $prefix, $level); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $text; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|