134
* Renders the help for the command line arguments.
135
* @package SimpleTest
136
* @subpackage UnitTester
133
138
class SimpleHelp {
135
140
private $flag_sets = array();
136
141
private $explanations = array();
138
function __construct($banner) {
139
$this->banner = $banner;
144
* Sets up the top level explanation for the program.
145
* @param string $overview Summary of program.
147
function __construct($overview = '') {
148
$this->overview = $overview;
152
* Adds the explanation for a group of flags that all
153
* have the same function.
154
* @param string/array $flags Flag and alternates. Don't
155
* worry about leading dashes
156
* as these are inserted automatically.
157
* @param string explanation What that flag group does.
142
159
function explainFlag($flags, $explanation) {
143
160
$flags = is_array($flags) ? $flags : array($flags);
144
161
$this->flag_sets[] = $flags;
145
162
$this->explanations[] = $explanation;
166
* Generates the help text.
167
* @returns string The complete formatted text.
148
169
function render() {
149
170
$tab_stop = $this->longestFlag($this->flag_sets) + 4;
150
$text = $this->banner . "\n";
171
$text = $this->overview . "\n";
151
172
for ($i = 0; $i < count($this->flag_sets); $i++) {
152
173
$text .= $this->renderFlagSet($this->flag_sets[$i], $this->explanations[$i], $tab_stop);
154
175
return $this->noDuplicateNewLines($text);
179
* Works out the longest flag for formatting purposes.
180
* @param array $flag_sets The internal flag set list.
157
182
private function longestFlag($flag_sets) {
159
184
foreach ($flag_sets as $flags) {
193
* Generates the text for a single flag and it's alternate flags.
194
* @returns string Help text for that flag group.
196
private function renderFlagSet($flags, $explanation, $tab_stop) {
197
$flag = array_shift($flags);
198
$text = str_pad($this->renderFlag($flag), $tab_stop, ' ') . $explanation . "\n";
199
foreach ($flags as $flag) {
200
$text .= ' ' . $this->renderFlag($flag) . "\n";
206
* Generates the flag name including leading dashes.
207
* @param string $flag Just the name.
208
* @returns Fag with apropriate dashes.
167
210
private function renderFlag($flag) {
168
211
return (strlen($flag) == 1 ? '-' : '--') . $flag;
171
private function renderFlagSet($flags, $explanation, $tab_stop) {
172
return str_pad($this->renderFlag($flags[0]), $tab_stop, ' ') . $explanation . "\n";
215
* Converts multiple new lines into a single new line.
216
* Just there to trap accidental duplicate new lines.
217
* @param string $text Text to clean up.
218
* @returns string Text with no blank lines.
175
220
private function noDuplicateNewLines($text) {
176
221
return preg_replace('/(\n+)/', "\n", $text);