This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpCommand.php
148 lines (123 loc) · 3.51 KB
/
HelpCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Command\Command;
use Ulrack\Command\Common\Command\InputInterface;
use Ulrack\Command\Common\Command\OutputInterface;
use Ulrack\Command\Common\Command\CommandInterface;
use Ulrack\Command\Common\Dao\CommandConfigurationInterface;
class HelpCommand implements CommandInterface
{
/**
* Contains the current command configuration.
*
* @var CommandConfigurationInterface
*/
private $commandConfiguration;
/**
* Constructor.
*
* @param CommandConfigurationInterface $commandConfiguration
*/
public function __construct(
CommandConfigurationInterface $commandConfiguration
) {
$this->commandConfiguration = $commandConfiguration;
}
/**
* Executes the command.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return void
*/
public function __invoke(
InputInterface $input,
OutputInterface $output
): void {
$output->outputText(
'Command: ' . implode(' ', $input->getCommand()),
true,
'title'
);
$description = $this->commandConfiguration->getDescription();
if (!empty($description)) {
$output->outputText('Description: ' . $description);
}
if (count($this->commandConfiguration->getParameters()) > 0) {
$output->writeLine('');
$output->outputText(
'Parameters: ',
true,
'title'
);
$output->outputExplainedList(
$this->constructParametersList()
);
}
if (count($this->commandConfiguration->getFlags()) > 0) {
$output->writeLine('');
$output->outputText(
'Flags: ',
true,
'title'
);
$output->outputExplainedList(
$this->constructFlagsList()
);
}
}
/**
* Constructs the explained flags list.
*
* @return array
*/
private function constructFlagsList(): array
{
$list = [];
foreach ($this->commandConfiguration->getFlags() as $flag) {
$options = [];
if (isset($flag['long'])) {
$options[] = $flag['long'];
}
if (isset($flag['short'])) {
$options[] = $flag['short'];
}
$list[sprintf(
'[%s]',
implode('|', $options)
)] = $flag['description'] ?? '';
}
return $list;
}
/**
* Constructs the explained parameters list.
*
* @return array
*/
private function constructParametersList(): array
{
$list = [];
foreach ($this->commandConfiguration->getParameters() as $parameter) {
$options = [];
if (isset($parameter['long'])) {
$options[] = $parameter['long'];
}
if (isset($parameter['short'])) {
$options[] = $parameter['short'];
}
$list[sprintf(
'[%s](%s)%s',
implode('|', $options),
$parameter['type'],
isset($parameter['required']) && $parameter['required']
? '*'
: ''
)] = $parameter['description'] ?? '';
}
return $list;
}
}