Permalink
Browse files

Wrote extensible help formatter

  • Loading branch information...
1 parent c684bf5 commit 0e91f6701f7cb7500cffada485433e6496d57dc9 lastcraft committed Apr 3, 2010
Showing with 59 additions and 8 deletions.
  1. +53 −8 arguments.php
  2. +6 −0 test/arguments_test.php
View
@@ -130,30 +130,55 @@ function all() {
}
}
+/**
+ * Renders the help for the command line arguments.
+ * @package SimpleTest
+ * @subpackage UnitTester
+ */
class SimpleHelp {
- private $banner;
+ private $overview;
private $flag_sets = array();
private $explanations = array();
- function __construct($banner) {
- $this->banner = $banner;
+ /**
+ * Sets up the top level explanation for the program.
+ * @param string $overview Summary of program.
+ */
+ function __construct($overview = '') {
+ $this->overview = $overview;
}
+ /**
+ * Adds the explanation for a group of flags that all
+ * have the same function.
+ * @param string/array $flags Flag and alternates. Don't
+ * worry about leading dashes
+ * as these are inserted automatically.
+ * @param string explanation What that flag group does.
+ */
function explainFlag($flags, $explanation) {
$flags = is_array($flags) ? $flags : array($flags);
$this->flag_sets[] = $flags;
$this->explanations[] = $explanation;
}
+ /**
+ * Generates the help text.
+ * @returns string The complete formatted text.
+ */
function render() {
$tab_stop = $this->longestFlag($this->flag_sets) + 4;
- $text = $this->banner . "\n";
+ $text = $this->overview . "\n";
for ($i = 0; $i < count($this->flag_sets); $i++) {
$text .= $this->renderFlagSet($this->flag_sets[$i], $this->explanations[$i], $tab_stop);
}
return $this->noDuplicateNewLines($text);
}
+ /**
+ * Works out the longest flag for formatting purposes.
+ * @param array $flag_sets The internal flag set list.
+ */
private function longestFlag($flag_sets) {
$longest = 0;
foreach ($flag_sets as $flags) {
@@ -164,14 +189,34 @@ private function longestFlag($flag_sets) {
return $longest;
}
- private function renderFlag($flag) {
- return (strlen($flag) == 1 ? '-' : '--') . $flag;
+ /**
+ * Generates the text for a single flag and it's alternate flags.
+ * @returns string Help text for that flag group.
+ */
+ private function renderFlagSet($flags, $explanation, $tab_stop) {
+ $flag = array_shift($flags);
+ $text = str_pad($this->renderFlag($flag), $tab_stop, ' ') . $explanation . "\n";
+ foreach ($flags as $flag) {
+ $text .= ' ' . $this->renderFlag($flag) . "\n";
+ }
+ return $text;
}
- private function renderFlagSet($flags, $explanation, $tab_stop) {
- return str_pad($this->renderFlag($flags[0]), $tab_stop, ' ') . $explanation . "\n";
+ /**
+ * Generates the flag name including leading dashes.
+ * @param string $flag Just the name.
+ * @returns Fag with apropriate dashes.
+ */
+ private function renderFlag($flag) {
+ return (strlen($flag) == 1 ? '-' : '--') . $flag;
}
+ /**
+ * Converts multiple new lines into a single new line.
+ * Just there to trap accidental duplicate new lines.
+ * @param string $text Text to clean up.
+ * @returns string Text with no blank lines.
+ */
private function noDuplicateNewLines($text) {
return preg_replace('/(\n+)/', "\n", $text);
}
@@ -72,5 +72,11 @@ function testHasAtleastFourSpacesAfterLongestFlag() {
$this->assertEqual($help->render(),
"Cool program\n-a Enables A\n--long Enables Long\n");
}
+
+ function testCanDisplaysMultipleFlagsForEachOption() {
+ $help = new SimpleHelp('Cool program');
+ $help->explainFlag(array('a', 'aa'), 'Enables A');
+ $this->assertEqual($help->render(), "Cool program\n-a Enables A\n --aa\n");
+ }
}
?>

0 comments on commit 0e91f67

Please sign in to comment.