1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace clearice\argparser; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class HelpMessageGenerator implements HelpGeneratorInterface |
7
|
|
|
{ |
8
|
3 |
|
public function generate($name, $command, $options, $description, $footer) |
9
|
|
|
{ |
10
|
3 |
|
if($command) { |
11
|
1 |
|
return wordwrap($description) . "\n\n" |
12
|
1 |
|
. $this->getUsageMessage($name, $options, $command) |
13
|
1 |
|
. $this->getOptionHelpMessages($options['options'], $command) |
14
|
1 |
|
. $this->getOptionHelpMessages($options['options']) |
15
|
1 |
|
. wordwrap($footer) . "\n"; |
16
|
|
|
} else { |
17
|
3 |
|
return wordwrap($description) . "\n\n" |
18
|
3 |
|
. $this->getUsageMessage($name, $options, $command) |
19
|
3 |
|
. $this->getCommandsMessage($options['commands']) |
20
|
3 |
|
. $this->getOptionHelpMessages($options['options']) |
21
|
3 |
|
. wordwrap($footer) . "\n"; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
3 |
|
private function getCommandsMessage($commands) |
26
|
|
|
{ |
27
|
3 |
|
if(count($commands)) { |
28
|
2 |
|
$commandsHelp = array('Commands:'); |
29
|
2 |
|
foreach ($commands as $command) |
30
|
|
|
{ |
31
|
2 |
|
$commandsHelp[] = implode("\n", $this->formatCommandHelp($command)); |
32
|
|
|
} |
33
|
2 |
|
$commandsHelp[] = ''; |
34
|
2 |
|
return implode("\n", $commandsHelp) . "\n"; |
35
|
|
|
} |
36
|
1 |
|
return ''; |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
private function formatCommandHelp($command) |
40
|
|
|
{ |
41
|
2 |
|
$commandHelp = array(); |
42
|
2 |
|
$help = explode("\n", wordwrap($command['help'], 59)); |
43
|
2 |
|
$commandHelp[] = $this->wrapHelp(sprintf("% -20s", $command['name']), $help, 20); |
44
|
2 |
|
foreach($help as $helpLine) |
45
|
|
|
{ |
46
|
2 |
|
$commandHelp[] = str_repeat(' ', 20) . $helpLine; |
47
|
|
|
} |
48
|
2 |
|
return $commandHelp; |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
private function getUsageMessage($name, $options, $command = '') |
52
|
|
|
{ |
53
|
3 |
|
return sprintf( |
54
|
3 |
|
"Usage:\n %s %s%s[OPTIONS] ...\n\n", basename($name), |
55
|
3 |
|
count($options['commands']) > 0 && $command == '' ? "[COMMAND] " : "", |
56
|
3 |
|
$command != "" ? "$command ": "" |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
private function getOptionHelpMessages($options, $command = '') |
61
|
|
|
{ |
62
|
3 |
|
$message = $command == '' ? "Options:\n" : "Options for $command command:\n"; |
63
|
3 |
|
foreach ($options as $option) { |
64
|
3 |
|
if($option['command'] !== $command) { |
65
|
2 |
|
continue; |
66
|
|
|
} |
67
|
3 |
|
$message .= $this->formatOptionHelp($option) . "\n"; |
68
|
|
|
} |
69
|
3 |
|
return "$message\n"; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Formats the help line of a value which is accepted by an option. If a |
74
|
|
|
* value type is provided in the option, it is used if not it uses a generic |
75
|
|
|
* "VALUE" to show that an option can accept a value. |
76
|
|
|
* |
77
|
|
|
* @param array $option |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
3 |
|
private function formatValue($option) |
81
|
|
|
{ |
82
|
3 |
|
if (isset($option['type'])) { |
83
|
2 |
|
return "=" . (isset($option['value']) ? $option['value'] : "VALUE"); |
84
|
|
|
} |
85
|
3 |
|
} |
86
|
|
|
|
87
|
3 |
|
private function formatOptionHelp($option) |
88
|
|
|
{ |
89
|
3 |
|
$optionHelp = array(); |
90
|
3 |
|
$help = explode("\n", wordwrap($option['help'], 50)); |
91
|
3 |
|
$argumentPart = $this->formatArgument($option); |
92
|
3 |
|
$optionHelp[] = $this->wrapHelp($argumentPart, $help); |
93
|
3 |
|
foreach ($help as $helpLine) { |
94
|
1 |
|
$optionHelp[] = str_repeat(' ', 29) . "$helpLine"; |
95
|
|
|
} |
96
|
3 |
|
return implode("\n", $optionHelp); |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
private function formatArgument($option) |
100
|
|
|
{ |
101
|
3 |
|
$valueHelp = $this->formatValue($option); |
102
|
3 |
|
$argumentHelp = ""; |
103
|
3 |
|
if (isset($option['name']) && isset($option['short_name'])) { |
104
|
3 |
|
$argumentHelp = sprintf( |
105
|
3 |
|
" %s, %-22s ", "-{$option['short_name']}", "--{$option['name']}$valueHelp" |
106
|
|
|
); |
107
|
1 |
|
} else if (isset($option['name'])) { |
108
|
1 |
|
$argumentHelp = sprintf( |
109
|
1 |
|
" %-27s", "--{$option['name']}$valueHelp" |
110
|
|
|
); |
111
|
1 |
|
} else if (isset($option['short_name'])) { |
112
|
1 |
|
$argumentHelp = sprintf( |
113
|
1 |
|
" %-27s", "-{$option['short_name']}" |
114
|
|
|
); |
115
|
|
|
} |
116
|
3 |
|
return $argumentHelp; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Wraps the help message arround the argument by producing two different |
121
|
|
|
* columns. The argument is placed in the first column and the help message |
122
|
|
|
* is placed in the second column. |
123
|
|
|
* |
124
|
|
|
* @param string $argumentPart |
125
|
|
|
* @param array $help |
126
|
|
|
* @param integer $minSize |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
3 |
|
private function wrapHelp($argumentPart, &$help, $minSize = 29) |
130
|
|
|
{ |
131
|
3 |
|
if (strlen($argumentPart) <= $minSize) { |
132
|
3 |
|
return $argumentPart . array_shift($help); |
133
|
|
|
} else { |
134
|
1 |
|
return $argumentPart; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |