|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\CLImate\Argument; |
|
4
|
|
|
|
|
5
|
|
|
use League\CLImate\CLImate; |
|
6
|
|
|
|
|
7
|
|
|
class Summary |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var \League\CLImate\CLImate $climate |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $climate; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string $description |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $description; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string $command |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $command; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Filter $filter |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $filter; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param \League\CLImate\CLImate $climate |
|
31
|
|
|
* |
|
32
|
|
|
* @return \League\CLImate\Argument\Summary |
|
33
|
|
|
*/ |
|
34
|
4 |
|
public function setClimate(CLImate $climate) |
|
35
|
|
|
{ |
|
36
|
4 |
|
$this->climate = $climate; |
|
37
|
|
|
|
|
38
|
4 |
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $description |
|
43
|
|
|
* |
|
44
|
|
|
* @return \League\CLImate\Argument\Summary |
|
45
|
|
|
*/ |
|
46
|
4 |
|
public function setDescription($description) |
|
47
|
|
|
{ |
|
48
|
4 |
|
$this->description = $description; |
|
49
|
|
|
|
|
50
|
4 |
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $command |
|
55
|
|
|
* |
|
56
|
|
|
* @return \League\CLImate\Argument\Summary |
|
57
|
|
|
*/ |
|
58
|
4 |
|
public function setCommand($command) |
|
59
|
|
|
{ |
|
60
|
4 |
|
$this->command = $command; |
|
61
|
|
|
|
|
62
|
4 |
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param Filter $filter |
|
67
|
|
|
* @param Argument[] $arguments |
|
68
|
|
|
* |
|
69
|
|
|
* @return \League\CLImate\Argument\Summary |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public function setFilter($filter, $arguments) |
|
72
|
|
|
{ |
|
73
|
4 |
|
$this->filter = $filter; |
|
74
|
4 |
|
$this->filter->setArguments($arguments); |
|
75
|
|
|
|
|
76
|
4 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Output the full summary for the program |
|
81
|
|
|
*/ |
|
82
|
4 |
|
public function output() |
|
83
|
|
|
{ |
|
84
|
|
|
// Print the description if it's defined. |
|
85
|
4 |
|
if ($this->description) { |
|
86
|
4 |
|
$this->climate->out($this->description)->br(); |
|
87
|
2 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
// Print the usage statement with the arguments without a prefix at the end. |
|
90
|
4 |
|
$this->climate->out("Usage: {$this->command} " |
|
91
|
4 |
|
. $this->short($this->getOrderedArguments())); |
|
92
|
|
|
|
|
93
|
|
|
// Print argument details. |
|
94
|
4 |
|
foreach (['required', 'optional'] as $type) { |
|
95
|
4 |
|
$this->outputArguments($this->filter->{$type}(), $type); |
|
96
|
2 |
|
} |
|
97
|
4 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Build a short summary of a list of arguments. |
|
101
|
|
|
* |
|
102
|
|
|
* @param Argument[] $arguments |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
8 |
|
public function short($arguments) |
|
107
|
|
|
{ |
|
108
|
8 |
|
return implode(' ', array_map([$this, 'argumentBracketed'], $arguments)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Build an argument's summary for use in a usage statement. |
|
113
|
|
|
* |
|
114
|
|
|
* For example, "-u username, --user username", "--force", or |
|
115
|
|
|
* "-c count (default: 7)". |
|
116
|
|
|
* |
|
117
|
|
|
* @param Argument $argument |
|
118
|
|
|
* |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
8 |
|
public function argument(Argument $argument) |
|
122
|
|
|
{ |
|
123
|
8 |
|
$summary = $this->prefixedArguments($argument); |
|
124
|
8 |
|
$printedName = mb_strstr($summary, ' ' . $argument->name()); |
|
125
|
|
|
|
|
126
|
|
|
// Print the argument name if it's not printed yet. |
|
127
|
8 |
|
if (!$printedName && !$argument->noValue()) { |
|
128
|
4 |
|
$summary .= $argument->name(); |
|
129
|
2 |
|
} |
|
130
|
|
|
|
|
131
|
8 |
|
if ($defaults = $argument->defaultValue()) { |
|
132
|
4 |
|
if (count($defaults) == 1) { |
|
133
|
4 |
|
$summary .= " (default: {$defaults[0]})"; |
|
134
|
2 |
|
} else { |
|
135
|
4 |
|
$summary .= ' (defaults: ' . implode(', ', $defaults) . ')'; |
|
136
|
|
|
} |
|
137
|
2 |
|
} |
|
138
|
|
|
|
|
139
|
8 |
|
return $summary; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Build argument summary surrounded by brackets |
|
144
|
|
|
* |
|
145
|
|
|
* @param Argument $argument |
|
146
|
|
|
* |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
8 |
|
protected function argumentBracketed(Argument $argument) |
|
150
|
|
|
{ |
|
151
|
8 |
|
return '[' . $this->argument($argument) . ']'; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Get the arguments ordered by whether or not they have a prefix |
|
156
|
|
|
* |
|
157
|
|
|
* @return Argument[] |
|
158
|
|
|
*/ |
|
159
|
4 |
|
protected function getOrderedArguments() |
|
160
|
|
|
{ |
|
161
|
4 |
|
return array_merge($this->filter->withPrefix(), $this->filter->withoutPrefix()); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Print out the argument list |
|
166
|
|
|
* |
|
167
|
|
|
* @param array $arguments |
|
168
|
|
|
* @param string $type |
|
169
|
|
|
*/ |
|
170
|
4 |
|
protected function outputArguments($arguments, $type) |
|
171
|
|
|
{ |
|
172
|
4 |
|
if (count($arguments) == 0) { |
|
173
|
|
|
return; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
4 |
|
$this->climate->br()->out(mb_convert_case($type, MB_CASE_TITLE) . ' Arguments:'); |
|
177
|
|
|
|
|
178
|
4 |
|
foreach ($arguments as $argument) { |
|
179
|
4 |
|
$this->climate->tab()->out($this->argument($argument)); |
|
180
|
|
|
|
|
181
|
4 |
|
if ($argument->description()) { |
|
182
|
4 |
|
$this->climate->tab(2)->out($argument->description()); |
|
183
|
2 |
|
} |
|
184
|
2 |
|
} |
|
185
|
4 |
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Builds the summary for any prefixed arguments |
|
189
|
|
|
* |
|
190
|
|
|
* @param Argument $argument |
|
191
|
|
|
* |
|
192
|
|
|
* @return string |
|
193
|
|
|
*/ |
|
194
|
8 |
|
protected function prefixedArguments(Argument $argument) |
|
195
|
|
|
{ |
|
196
|
8 |
|
$prefixes = [$argument->prefix(), $argument->longPrefix()]; |
|
197
|
8 |
|
$summary = []; |
|
198
|
|
|
|
|
199
|
8 |
|
foreach ($prefixes as $key => $prefix) { |
|
200
|
8 |
|
if (!$prefix) { |
|
201
|
8 |
|
continue; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
8 |
|
$sub = str_repeat('-', $key + 1) . $prefix; |
|
205
|
|
|
|
|
206
|
8 |
|
if (!$argument->noValue()) { |
|
207
|
8 |
|
$sub .= " {$argument->name()}"; |
|
208
|
4 |
|
} |
|
209
|
|
|
|
|
210
|
8 |
|
$summary[] = $sub; |
|
211
|
4 |
|
} |
|
212
|
|
|
|
|
213
|
8 |
|
return implode(', ', $summary); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|