|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Stefaminator\Cli; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class HelpRunner extends CmdRunner { |
|
8
|
|
|
|
|
9
|
|
|
public static $header = <<<EOT |
|
10
|
|
|
|
|
11
|
|
|
o |
|
12
|
|
|
` /_\ ' |
|
13
|
|
|
- (o o) - |
|
14
|
|
|
--------ooO--(_)--Ooo-------- |
|
15
|
|
|
Need help? |
|
16
|
|
|
----------------------------- |
|
17
|
|
|
EOT; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
4 |
|
public function run(): void { |
|
21
|
4 |
|
$this->displayHeader(); |
|
22
|
4 |
|
$this->displayUsage(); |
|
23
|
4 |
|
$this->displayArguments(); |
|
24
|
4 |
|
$this->displayOptions(); |
|
25
|
4 |
|
$this->displaySubcommands(); |
|
26
|
4 |
|
$this->displayHelp(); |
|
27
|
4 |
|
self::eol(); |
|
28
|
4 |
|
} |
|
29
|
|
|
|
|
30
|
4 |
|
public function displayHeader(): void { |
|
31
|
|
|
|
|
32
|
4 |
|
$help = self::$header; |
|
33
|
|
|
|
|
34
|
4 |
|
self::echo($help, Color::FOREGROUND_COLOR_YELLOW); |
|
35
|
|
|
|
|
36
|
4 |
|
self::eol(); |
|
37
|
4 |
|
} |
|
38
|
|
|
|
|
39
|
4 |
|
public function displayUsage(): void { |
|
40
|
|
|
|
|
41
|
4 |
|
$cmd = $this->getCmd(); |
|
42
|
|
|
|
|
43
|
4 |
|
$oc = $cmd->getOptionCollection(); |
|
44
|
4 |
|
$has_options = !empty($oc->options); |
|
45
|
|
|
|
|
46
|
4 |
|
$arg_usage = $this->getArgumentUsage(); |
|
47
|
|
|
|
|
48
|
4 |
|
$has_subcommands = !empty($cmd->subcommands); |
|
49
|
|
|
|
|
50
|
4 |
|
self::eol(); |
|
51
|
4 |
|
self::echo('Usage: ', Color::FOREGROUND_COLOR_YELLOW); |
|
52
|
4 |
|
self::eol(); |
|
53
|
|
|
|
|
54
|
4 |
|
self::echo( |
|
55
|
|
|
' ' . |
|
56
|
4 |
|
($cmd->parent !== null ? $cmd->cmd : 'command') . |
|
57
|
4 |
|
($has_options ? ' [options]' : '') . |
|
58
|
4 |
|
(!empty($arg_usage) ? ' ' . $arg_usage : '') . |
|
59
|
4 |
|
($has_subcommands ? ' [command]' : '') |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
4 |
|
self::eol(); |
|
63
|
4 |
|
} |
|
64
|
|
|
|
|
65
|
4 |
|
private function getArgumentUsage() { |
|
66
|
|
|
|
|
67
|
4 |
|
$cmd = $this->getCmd(); |
|
68
|
|
|
|
|
69
|
4 |
|
$arg_usage = []; |
|
70
|
4 |
|
foreach ($cmd->argumentSpecs as $k => $v) { |
|
71
|
1 |
|
$arg_usage[] = '[<' . $k . '>]' . (array_key_exists('multiple', $v) ? '...' : ''); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
4 |
|
return implode(' ', $arg_usage); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
4 |
|
public function displayArguments(): void { |
|
78
|
|
|
|
|
79
|
4 |
|
$cmd = $this->getCmd(); |
|
80
|
|
|
|
|
81
|
4 |
|
$has_arguments = !empty($cmd->argumentSpecs); |
|
82
|
|
|
|
|
83
|
4 |
|
if ($has_arguments) { |
|
84
|
|
|
|
|
85
|
1 |
|
self::eol(); |
|
86
|
1 |
|
self::echo('Arguments: ', Color::FOREGROUND_COLOR_YELLOW); |
|
87
|
1 |
|
self::eol(); |
|
88
|
|
|
|
|
89
|
1 |
|
foreach ($cmd->argumentSpecs as $argumentSpec => $config) { |
|
90
|
|
|
|
|
91
|
1 |
|
$s = $argumentSpec; |
|
92
|
1 |
|
$s = ' ' . str_pad($s, 20, ' '); |
|
93
|
1 |
|
self::echo($s, Color::FOREGROUND_COLOR_GREEN); |
|
94
|
|
|
|
|
95
|
1 |
|
$s = ' ' . (array_key_exists('description', $config) ? $config['description'] : ''); |
|
96
|
1 |
|
self::echo($s); |
|
97
|
|
|
|
|
98
|
1 |
|
self::eol(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
4 |
|
} |
|
104
|
|
|
|
|
105
|
4 |
|
public function displayOptions(): void { |
|
106
|
|
|
|
|
107
|
4 |
|
$cmd = $this->getCmd(); |
|
108
|
|
|
|
|
109
|
4 |
|
$oc = $cmd->getOptionCollection(); |
|
110
|
4 |
|
$has_options = !empty($oc->options); |
|
111
|
|
|
|
|
112
|
4 |
|
if ($has_options) { |
|
113
|
|
|
|
|
114
|
4 |
|
self::eol(); |
|
115
|
4 |
|
self::echo('Options: ', Color::FOREGROUND_COLOR_YELLOW); |
|
116
|
4 |
|
self::eol(); |
|
117
|
|
|
|
|
118
|
4 |
|
foreach ($oc->options as $option) { |
|
119
|
|
|
|
|
120
|
4 |
|
$s = ' '; |
|
121
|
4 |
|
if (!empty($option->short)) { |
|
122
|
4 |
|
$s = '-' . $option->short . ', '; |
|
123
|
|
|
} |
|
124
|
4 |
|
$s .= '--' . $option->long; |
|
125
|
|
|
|
|
126
|
4 |
|
$s = ' ' . str_pad($s, 20, ' '); |
|
127
|
4 |
|
self::echo($s, Color::FOREGROUND_COLOR_GREEN); |
|
128
|
|
|
|
|
129
|
4 |
|
$s = ' ' . $option->desc; |
|
130
|
4 |
|
self::echo($s); |
|
131
|
|
|
|
|
132
|
4 |
|
if ($option->defaultValue) { |
|
133
|
1 |
|
$s = ' [default: ' . $option->defaultValue . ']'; |
|
134
|
1 |
|
self::echo($s, Color::FOREGROUND_COLOR_YELLOW); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
4 |
|
self::eol(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
4 |
|
} |
|
142
|
|
|
|
|
143
|
4 |
|
public function displaySubcommands(): void { |
|
144
|
|
|
|
|
145
|
4 |
|
$cmd = $this->getCmd(); |
|
146
|
|
|
|
|
147
|
4 |
|
$has_subcommands = !empty($cmd->subcommands); |
|
148
|
|
|
|
|
149
|
4 |
|
if ($has_subcommands) { |
|
150
|
|
|
|
|
151
|
2 |
|
self::eol(); |
|
152
|
2 |
|
self::echo('Available commands: ', Color::FOREGROUND_COLOR_YELLOW); |
|
153
|
2 |
|
self::eol(); |
|
154
|
|
|
|
|
155
|
2 |
|
foreach ($cmd->subcommands as $_cmd) { |
|
156
|
|
|
|
|
157
|
2 |
|
$s = ' ' . str_pad($_cmd->cmd, 20, ' '); |
|
158
|
2 |
|
self::echo($s, Color::FOREGROUND_COLOR_GREEN); |
|
159
|
|
|
|
|
160
|
2 |
|
$s = ' ' . $_cmd->descr; |
|
161
|
2 |
|
self::echo($s); |
|
162
|
|
|
|
|
163
|
2 |
|
self::eol(); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
4 |
|
} |
|
167
|
|
|
|
|
168
|
4 |
|
public function displayHelp(): void { |
|
169
|
|
|
|
|
170
|
4 |
|
$cmd = $this->getCmd(); |
|
171
|
|
|
|
|
172
|
4 |
|
$runner = $cmd->getRunner(); |
|
173
|
|
|
|
|
174
|
4 |
|
if($runner === null) { |
|
175
|
|
|
return; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
4 |
|
ob_start(); |
|
179
|
4 |
|
$runner->help(); |
|
180
|
4 |
|
$help = ob_get_clean(); |
|
181
|
|
|
|
|
182
|
4 |
|
if(empty($help)) { |
|
183
|
3 |
|
return; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
1 |
|
self::eol(); |
|
187
|
1 |
|
self::echo('Help: ', Color::FOREGROUND_COLOR_YELLOW); |
|
188
|
1 |
|
self::eol(); |
|
189
|
|
|
|
|
190
|
1 |
|
echo $help; |
|
191
|
1 |
|
} |
|
192
|
|
|
|
|
193
|
|
|
} |