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