|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Stefaminator\Cli; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class HelpRunner extends CmdRunner { |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
public function run(): void { |
|
11
|
|
|
$this->displayHeader(); |
|
12
|
|
|
$this->displayUsage(); |
|
13
|
|
|
$this->displayOptions(); |
|
14
|
|
|
$this->displaySubcommands(); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function displayHeader(): void { |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
$help = <<<EOT |
|
21
|
|
|
|
|
22
|
|
|
o |
|
23
|
|
|
` /_\ ' |
|
24
|
|
|
- (o o) - |
|
25
|
|
|
----------ooO--(_)--Ooo---------- |
|
26
|
|
|
Need help? |
|
27
|
|
|
--------------------------------- |
|
28
|
|
|
EOT; |
|
29
|
|
|
|
|
30
|
|
|
App::echo($help, Color::FOREGROUND_COLOR_YELLOW); |
|
31
|
|
|
|
|
32
|
|
|
App::eol(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function displayUsage(): void { |
|
36
|
|
|
|
|
37
|
|
|
$cmd = $this->cmd(); |
|
38
|
|
|
|
|
39
|
|
|
$oc = $cmd->getOptionCollection(); |
|
40
|
|
|
$has_options = !empty($oc->options); |
|
41
|
|
|
|
|
42
|
|
|
$has_subcommands = !empty($cmd->subcommands); |
|
43
|
|
|
|
|
44
|
|
|
App::eol(); |
|
45
|
|
|
App::echo('Usage: ', Color::FOREGROUND_COLOR_YELLOW); |
|
46
|
|
|
App::eol(); |
|
47
|
|
|
|
|
48
|
|
|
App::echo( |
|
49
|
|
|
' ' . |
|
50
|
|
|
($cmd->parent !== null ? $cmd->cmd : 'command') . |
|
51
|
|
|
($has_options ? ' [options]' : '') . |
|
52
|
|
|
($has_subcommands ? ' [command]' : '') |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
App::eol(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function displayOptions(): void { |
|
59
|
|
|
|
|
60
|
|
|
$cmd = $this->cmd(); |
|
61
|
|
|
|
|
62
|
|
|
$oc = $cmd->getOptionCollection(); |
|
63
|
|
|
$has_options = !empty($oc->options); |
|
64
|
|
|
|
|
65
|
|
|
if ($has_options) { |
|
66
|
|
|
|
|
67
|
|
|
App::eol(); |
|
68
|
|
|
App::echo('Options: ', Color::FOREGROUND_COLOR_YELLOW); |
|
69
|
|
|
App::eol(); |
|
70
|
|
|
|
|
71
|
|
|
foreach ($oc->options as $option) { |
|
72
|
|
|
|
|
73
|
|
|
$s = ' '; |
|
74
|
|
|
if(!empty($option->short)) { |
|
75
|
|
|
$s = '-' . $option->short . ', '; |
|
76
|
|
|
} |
|
77
|
|
|
$s .= '--' . $option->long; |
|
78
|
|
|
|
|
79
|
|
|
$s = ' ' . str_pad($s, 20, ' '); |
|
80
|
|
|
App::echo($s, Color::FOREGROUND_COLOR_GREEN); |
|
81
|
|
|
|
|
82
|
|
|
$s = ' ' . $option->desc; |
|
83
|
|
|
App::echo($s); |
|
84
|
|
|
|
|
85
|
|
|
if ($option->defaultValue) { |
|
86
|
|
|
$s = ' [default: ' . $option->defaultValue . ']'; |
|
87
|
|
|
App::echo($s, Color::FOREGROUND_COLOR_YELLOW); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
App::eol(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
App::eol(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function displaySubcommands(): void { |
|
99
|
|
|
|
|
100
|
|
|
$cmd = $this->cmd(); |
|
101
|
|
|
|
|
102
|
|
|
$has_subcommands = !empty($cmd->subcommands); |
|
103
|
|
|
|
|
104
|
|
|
if($has_subcommands) { |
|
105
|
|
|
|
|
106
|
|
|
App::eol(); |
|
107
|
|
|
App::echo('Available commands: ', Color::FOREGROUND_COLOR_YELLOW); |
|
108
|
|
|
App::eol(); |
|
109
|
|
|
|
|
110
|
|
|
foreach ($cmd->subcommands as $_cmd) { |
|
111
|
|
|
|
|
112
|
|
|
$s = ' ' . str_pad($_cmd->cmd, 20, ' '); |
|
113
|
|
|
App::echo($s, Color::FOREGROUND_COLOR_GREEN); |
|
114
|
|
|
|
|
115
|
|
|
$s = ' ' . $_cmd->descr; |
|
116
|
|
|
App::echo($s); |
|
117
|
|
|
|
|
118
|
|
|
App::eol(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
App::eol(); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
} |