Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix Zend\Console\Getopt::getUsageMessage()

Getopt::getUsageMessage() no longer throws a warning, when freeform flags
are available and the parameters are already parsed via Getopt::parse().
Freeform flags will not show up in Getopt::getUsageMessage().

Added two tests to ensure this.
  • Loading branch information...
commit 11504b3cc89b81af5ae555b2b9bcf87daff971a9 1 parent 72d29c0
@TimWolla authored
View
8 library/Zend/Console/Getopt.php
@@ -565,6 +565,9 @@ public function getUsageMessage()
$maxLen = 20;
$lines = array();
foreach ($this->rules as $rule) {
+ if (isset($rule['isFreeformFlag'])) {
+ continue;
+ }
$flags = array();
if (is_array($rule['alias'])) {
foreach ($rule['alias'] as $flag) {
@@ -763,7 +766,10 @@ protected function _parseSingleOption($flag, &$argv)
// Magic methods in future will use this mark as real flag value
$this->ruleMap[$flag] = $flag;
$realFlag = $flag;
- $this->rules[$realFlag] = array('param' => 'optional');
+ $this->rules[$realFlag] = array(
+ 'param' => 'optional',
+ 'isFreeformFlag' => true
+ );
} else {
$realFlag = $this->ruleMap[$flag];
}
View
29 tests/ZendTest/Console/GetoptTest.php
@@ -589,6 +589,35 @@ public function testGetoptWithFreeformFlagOptionRecognizeFlagsWithValue()
$this->assertEquals('test', $opts->freeform);
}
+ public function testGetoptWithFreeformFlagOptionShowHelpAfterParseDoesNotThrowNotices()
+ {
+ // this formerly failed, because the index 'alias' is not set for freeform flags.
+ $opts = new Getopt(
+ array('colors' => 'Colors-option'),
+ array('color', '--freeform', 'test', 'zend'),
+ array(Getopt::CONFIG_FREEFORM_FLAGS => true)
+ );
+ $opts->parse();
+
+ $opts->getUsageMessage();
+ }
+
+ public function testGetoptWithFreeformFlagOptionShowHelpAfterParseDoesNotShowFreeformFlags()
+ {
+ $opts = new Getopt(
+ array('colors' => 'Colors-option'),
+ array('color', '--freeform', 'test', 'zend'),
+ array(Getopt::CONFIG_FREEFORM_FLAGS => true)
+ );
+ $opts->parse();
+
+ $message = preg_replace('/Usage: .* \[ options \]/',
+ 'Usage: <progname> [ options ]',
+ $opts->getUsageMessage());
+ $message = preg_replace('/ /', '_', $message);
+ $this->assertEquals($message, "Usage:_<progname>_[_options_]\n--colors_____________Colors-option\n");
+ }
+
public function testGetoptRaiseExceptionForNumericOptionsByDefault()
{
$opts = new Getopt(
Please sign in to comment.
Something went wrong with that request. Please try again.