1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace clearice\argparser; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class HelpMessageGenerator |
7
|
|
|
{ |
8
|
1 |
|
public function generate($name, $command, $options, $description, $footer) |
9
|
|
|
{ |
10
|
1 |
|
if($command) { |
|
|
|
|
11
|
|
|
|
12
|
|
|
} else { |
13
|
1 |
|
return sprintf( |
14
|
1 |
|
"%s\n\n%s\n\nOptions:\n%s\n%s\n", |
15
|
1 |
|
wordwrap($description), |
16
|
1 |
|
$this->getUsageMessage($name), |
17
|
1 |
|
$this->getOptionHelpMessages($options), |
18
|
1 |
|
wordwrap($footer) |
19
|
|
|
); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
1 |
|
private function getUsageMessage($name) |
24
|
|
|
{ |
25
|
1 |
|
return sprintf("Usage:\n %s [OPTIONS] ...", basename($name)); |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
private function getOptionHelpMessages($options) |
29
|
|
|
{ |
30
|
1 |
|
$message = ""; |
31
|
1 |
|
foreach ($options as $option) { |
32
|
1 |
|
$message .= $this->formatOptionHelp($option) . "\n"; |
33
|
|
|
} |
34
|
1 |
|
return $message; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Formats the help line of a value which is accepted by an option. If a |
39
|
|
|
* value type is provided in the option, it is used if not it uses a generic |
40
|
|
|
* "VALUE" to show that an option can accept a value. |
41
|
|
|
* |
42
|
|
|
* @param array $option |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
1 |
|
private function formatValue($option) |
46
|
|
|
{ |
47
|
1 |
|
if (isset($option['type'])) { |
48
|
1 |
|
return "=" . (isset($option['value']) ? $option['value'] : "VALUE"); |
49
|
|
|
} |
50
|
1 |
|
} |
51
|
|
|
|
52
|
1 |
|
private function formatOptionHelp($option) |
53
|
|
|
{ |
54
|
1 |
|
$optionHelp = array(); |
55
|
1 |
|
$help = explode("\n", wordwrap($option['help'], 50)); |
56
|
1 |
|
$argumentPart = $this->formatArgument($option); |
57
|
1 |
|
$optionHelp[] = $this->wrapHelp($argumentPart, $help); |
58
|
1 |
|
foreach ($help as $helpLine) { |
59
|
1 |
|
$optionHelp[] = str_repeat(' ', 29) . "$helpLine"; |
60
|
|
|
} |
61
|
1 |
|
return implode("\n", $optionHelp); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
private function formatArgument($option) |
65
|
|
|
{ |
66
|
1 |
|
$valueHelp = $this->formatValue($option); |
67
|
1 |
|
$argumentHelp = ""; |
68
|
1 |
|
if (isset($option['name']) && isset($option['short_name'])) { |
69
|
1 |
|
$argumentHelp = sprintf( |
70
|
1 |
|
" %s, %-22s ", "-{$option['short_name']}", "--{$option['name']}$valueHelp" |
71
|
|
|
); |
72
|
1 |
|
} else if (isset($option['name'])) { |
73
|
1 |
|
$argumentHelp = sprintf( |
74
|
1 |
|
" %-27s", "--{$option['name']}$valueHelp" |
75
|
|
|
); |
76
|
1 |
|
} else if (isset($option['short_name'])) { |
77
|
1 |
|
$argumentHelp = sprintf( |
78
|
1 |
|
" %-27s", "-{$option['short_name']}" |
79
|
|
|
); |
80
|
|
|
} |
81
|
1 |
|
return $argumentHelp; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Wraps the help message arround the argument by producing two different |
86
|
|
|
* columns. The argument is placed in the first column and the help message |
87
|
|
|
* is placed in the second column. |
88
|
|
|
* |
89
|
|
|
* @param string $argumentPart |
90
|
|
|
* @param array $help |
91
|
|
|
* @param integer $minSize |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
1 |
|
private function wrapHelp($argumentPart, &$help, $minSize = 29) |
95
|
|
|
{ |
96
|
1 |
|
if (strlen($argumentPart) <= $minSize) { |
97
|
1 |
|
return $argumentPart . array_shift($help); |
98
|
|
|
} else { |
99
|
1 |
|
return $argumentPart; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.