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
|
3 |
|
public function run(): void { |
21
|
3 |
|
$this->displayHeader(); |
22
|
3 |
|
$this->displayUsage(); |
23
|
3 |
|
$this->displayArguments(); |
24
|
3 |
|
$this->displayOptions(); |
25
|
3 |
|
$this->displaySubcommands(); |
26
|
3 |
|
} |
27
|
|
|
|
28
|
3 |
|
public function displayHeader(): void { |
29
|
|
|
|
30
|
3 |
|
$help = self::$header; |
31
|
|
|
|
32
|
3 |
|
App::echo($help, Color::FOREGROUND_COLOR_YELLOW); |
33
|
|
|
|
34
|
3 |
|
App::eol(); |
35
|
3 |
|
} |
36
|
|
|
|
37
|
3 |
|
public function displayUsage(): void { |
38
|
|
|
|
39
|
3 |
|
$cmd = $this->cmd(); |
40
|
|
|
|
41
|
3 |
|
$oc = $cmd->getOptionCollection(); |
42
|
3 |
|
$has_options = !empty($oc->options); |
43
|
|
|
|
44
|
3 |
|
$arg_usage = $this->getArgumentUsage(); |
45
|
|
|
|
46
|
3 |
|
$has_subcommands = !empty($cmd->subcommands); |
47
|
|
|
|
48
|
3 |
|
App::eol(); |
49
|
3 |
|
App::echo('Usage: ', Color::FOREGROUND_COLOR_YELLOW); |
50
|
3 |
|
App::eol(); |
51
|
|
|
|
52
|
3 |
|
App::echo( |
53
|
|
|
' ' . |
54
|
3 |
|
($cmd->parent !== null ? $cmd->cmd : 'command') . |
55
|
3 |
|
($has_options ? ' [options]' : '') . |
56
|
3 |
|
(!empty($arg_usage) ? ' ' . $arg_usage : '') . |
57
|
3 |
|
($has_subcommands ? ' [command]' : '') |
58
|
|
|
); |
59
|
|
|
|
60
|
3 |
|
App::eol(); |
61
|
3 |
|
} |
62
|
|
|
|
63
|
3 |
|
private function getArgumentUsage() { |
64
|
|
|
|
65
|
3 |
|
$cmd = $this->cmd(); |
66
|
|
|
|
67
|
3 |
|
$arg_usage = []; |
68
|
3 |
|
foreach ($cmd->argumentSpecs as $k => $v) { |
69
|
1 |
|
$arg_usage[] = '[<' . $k . '>]' . (array_key_exists('multiple', $v) ? '...' : ''); |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
return implode(' ', $arg_usage); |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
public function displayArguments(): void { |
76
|
|
|
|
77
|
3 |
|
$cmd = $this->cmd(); |
78
|
|
|
|
79
|
3 |
|
$has_arguments = !empty($cmd->argumentSpecs); |
80
|
|
|
|
81
|
3 |
|
if ($has_arguments) { |
82
|
|
|
|
83
|
1 |
|
App::eol(); |
84
|
1 |
|
App::echo('Arguments: ', Color::FOREGROUND_COLOR_YELLOW); |
85
|
1 |
|
App::eol(); |
86
|
|
|
|
87
|
1 |
|
foreach ($cmd->argumentSpecs as $argumentSpec => $config) { |
88
|
|
|
|
89
|
1 |
|
$s = $argumentSpec; |
90
|
1 |
|
$s = ' ' . str_pad($s, 20, ' '); |
91
|
1 |
|
App::echo($s, Color::FOREGROUND_COLOR_GREEN); |
92
|
|
|
|
93
|
1 |
|
$s = ' ' . (array_key_exists('description', $config) ? $config['description'] : ''); |
94
|
1 |
|
App::echo($s); |
95
|
|
|
|
96
|
1 |
|
App::eol(); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
App::eol(); |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
} |
104
|
|
|
|
105
|
3 |
|
public function displayOptions(): void { |
106
|
|
|
|
107
|
3 |
|
$cmd = $this->cmd(); |
108
|
|
|
|
109
|
3 |
|
$oc = $cmd->getOptionCollection(); |
110
|
3 |
|
$has_options = !empty($oc->options); |
111
|
|
|
|
112
|
3 |
|
if ($has_options) { |
113
|
|
|
|
114
|
3 |
|
App::eol(); |
115
|
3 |
|
App::echo('Options: ', Color::FOREGROUND_COLOR_YELLOW); |
116
|
3 |
|
App::eol(); |
117
|
|
|
|
118
|
3 |
|
foreach ($oc->options as $option) { |
119
|
|
|
|
120
|
3 |
|
$s = ' '; |
121
|
3 |
|
if (!empty($option->short)) { |
122
|
3 |
|
$s = '-' . $option->short . ', '; |
123
|
|
|
} |
124
|
3 |
|
$s .= '--' . $option->long; |
125
|
|
|
|
126
|
3 |
|
$s = ' ' . str_pad($s, 20, ' '); |
127
|
3 |
|
App::echo($s, Color::FOREGROUND_COLOR_GREEN); |
128
|
|
|
|
129
|
3 |
|
$s = ' ' . $option->desc; |
130
|
3 |
|
App::echo($s); |
131
|
|
|
|
132
|
3 |
|
if ($option->defaultValue) { |
133
|
|
|
$s = ' [default: ' . $option->defaultValue . ']'; |
134
|
|
|
App::echo($s, Color::FOREGROUND_COLOR_YELLOW); |
135
|
|
|
} |
136
|
|
|
|
137
|
3 |
|
App::eol(); |
138
|
|
|
} |
139
|
|
|
|
140
|
3 |
|
App::eol(); |
141
|
|
|
} |
142
|
|
|
|
143
|
3 |
|
} |
144
|
|
|
|
145
|
3 |
|
public function displaySubcommands(): void { |
146
|
|
|
|
147
|
3 |
|
$cmd = $this->cmd(); |
148
|
|
|
|
149
|
3 |
|
$has_subcommands = !empty($cmd->subcommands); |
150
|
|
|
|
151
|
3 |
|
if ($has_subcommands) { |
152
|
|
|
|
153
|
2 |
|
App::eol(); |
154
|
2 |
|
App::echo('Available commands: ', Color::FOREGROUND_COLOR_YELLOW); |
155
|
2 |
|
App::eol(); |
156
|
|
|
|
157
|
2 |
|
foreach ($cmd->subcommands as $_cmd) { |
158
|
|
|
|
159
|
2 |
|
$s = ' ' . str_pad($_cmd->cmd, 20, ' '); |
160
|
2 |
|
App::echo($s, Color::FOREGROUND_COLOR_GREEN); |
161
|
|
|
|
162
|
2 |
|
$s = ' ' . $_cmd->descr; |
163
|
2 |
|
App::echo($s); |
164
|
|
|
|
165
|
2 |
|
App::eol(); |
166
|
|
|
} |
167
|
|
|
|
168
|
2 |
|
App::eol(); |
169
|
|
|
} |
170
|
3 |
|
} |
171
|
|
|
|
172
|
|
|
} |