~blueyed/simpletest/simpletest-dev

« back to all changes in this revision

Viewing changes to arguments.php

  • Committer: lastcraft
  • Date: 2010-04-03 12:26:59 UTC
  • Revision ID: importd@pear-20100403122659-owyvsam2dhfqmfpu
Wrote extensible help formatter

Show diffs side-by-side

added added

removed removed

Lines of Context:
130
130
    }
131
131
}
132
132
 
 
133
/**
 
134
 *    Renders the help for the command line arguments.
 
135
 *    @package  SimpleTest
 
136
 *    @subpackage   UnitTester
 
137
 */
133
138
class SimpleHelp {
134
 
    private $banner;
 
139
    private $overview;
135
140
    private $flag_sets = array();
136
141
    private $explanations = array();
137
142
    
138
 
    function __construct($banner) {
139
 
        $this->banner = $banner;
 
143
    /**
 
144
     * Sets up the top level explanation for the program.
 
145
     * @param string $overview        Summary of program.
 
146
     */
 
147
    function __construct($overview = '') {
 
148
        $this->overview = $overview;
140
149
    }
141
150
    
 
151
    /**
 
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.
 
158
     */
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;
146
163
    }
147
164
    
 
165
    /**
 
166
     * Generates the help text.
 
167
     * @returns string      The complete formatted text.
 
168
     */
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);
153
174
        }
154
175
        return $this->noDuplicateNewLines($text);
155
176
    }
156
177
    
 
178
    /**
 
179
     * Works out the longest flag for formatting purposes.
 
180
     * @param array $flag_sets      The internal flag set list.
 
181
     */
157
182
    private function longestFlag($flag_sets) {
158
183
        $longest = 0;
159
184
        foreach ($flag_sets as $flags) {
164
189
        return $longest;
165
190
    }
166
191
    
 
192
    /**
 
193
     * Generates the text for a single flag and it's alternate flags.
 
194
     * @returns string           Help text for that flag group.
 
195
     */
 
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";
 
201
        }
 
202
        return $text;
 
203
    }
 
204
    
 
205
    /**
 
206
     * Generates the flag name including leading dashes.
 
207
     * @param string $flag          Just the name.
 
208
     * @returns                     Fag with apropriate dashes.
 
209
     */
167
210
    private function renderFlag($flag) {
168
211
        return (strlen($flag) == 1 ? '-' : '--') . $flag;
169
212
    }
170
213
    
171
 
    private function renderFlagSet($flags, $explanation, $tab_stop) {
172
 
        return str_pad($this->renderFlag($flags[0]), $tab_stop, ' ') . $explanation . "\n";
173
 
    }
174
 
    
 
214
    /**
 
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.
 
219
     */
175
220
    private function noDuplicateNewLines($text) {
176
221
        return preg_replace('/(\n+)/', "\n", $text);
177
222
    }