10000 Merge pull request #34 from mfn/mf-banner · kler/getopt-php@8f7a8ff · GitHub
[go: up one dir, main page]

Skip to content

Commit 8f7a8ff

Browse files
committed
Merge pull request getopt-php#34 from mfn/mf-banner
helptext: add support for custom banner message
2 parents cd0c634 + c8caecc commit 8f7a8ff

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/Ulrichsg/Getopt/Getopt.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Getopt implements \Countable, \ArrayAccess, \IteratorAggregate
2626
private $options = array();
2727
/** @var array */
2828
private $operands = array();
29+
/** @var string */
30+
private $banner = "Usage: %s [options] [operands]\n";
2931

3032
/**
3133
* Creates a new Getopt object.
@@ -184,14 +186,37 @@ public function getOperand($i)
184186
return ($i < count($this->operands)) ? $this->operands[$i] : null;
185187
}
186188

189+
/**
190+
* Returns the banner string
191+
*
192+
* @return string
193+
*/
194+
public function getBanner()
195+
{
196+
return $this->banner;
197+
}
198+
199+
/**
200+
* Set the banner string
201+
*
202+
* @param string $banner The banner string; will be passed to sprintf(), can include %s for current scripts name.
203+
* Be sure to include a trailing line feed.
204+
* @return Getopt
205+
*/
206+
public function setBanner($banner)
207+
{
208+
$this->banner = $banner;
209+
return $this;
210+
}
211+
187212
/**
188213
* Returns an usage information text generated from the given options.
189214
* @param int $padding Number of characters to pad output of options to
190215
* @return string
191216
*/
192217
public function getHelpText($padding = 25)
193218
{
194-
$helpText = sprintf("Usage: %s [options] [operands]\n", $this->scriptName);
219+
$helpText = sprintf($this->getBanner(), $this->scriptName);
195220
$helpText .= "Options:\n";
196221
foreach ($this->optionList as $option) {
197222
$mode = '';

test/Ulrichsg/Getopt/GetoptTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,23 @@ public function testHelpTextWithoutDescriptions()
177177

178178
$this->assertEquals($expected, $getopt->getHelpText());
179179
}
180+
181+
public function testHelpTextNoParse()
182+
{
183+
$getopt = new Getopt();
184+
$expected = "Usage: [options] [operands]\nOptions:\n";
185+
$this->assertSame($expected, $getopt->getHelpText());
186+
}
187+
188+
public function testHelpTextWithCustomBanner()
189+
{
190+
$script = $_SERVER['PHP_SELF'];
191+
192+
$getopt = new Getopt();
193+
$getopt->setBanner("My custom Banner %s\n");
194+
$this->assertSame("My custom Banner \nOptions:\n", $getopt->getHelpText());
195+
196+
$getopt->parse();
197+
$this->assertSame("My custom Banner $script\nOptions:\n", $getopt->getHelpText());
198+
}
180199
}

0 commit comments

Comments
 (0)
0