Issues

ZF-2064: Zend_Console_Getopt => method addRules() fails

Description

Using addRules() result in some warnings.

reproduce code (example from the manual):

require_once 'Zend/Console/Getopt.php';

$opts = new Zend_Console_Getopt('abp:');
$opts->addRules(
  array(
    'verbose|v' => 'Print verbose output'
  )
);

Output:

PHP Warning:  preg_match_all() expects parameter 2 to be string, array given in /Users/carsten/Sites/Zend/Console/Getopt.php on line 814

Warning: preg_match_all() expects parameter 2 to be string, array given in /Users/carsten/Sites/Zend/Console/Getopt.php on line 814
PHP Notice:  Undefined offset:  1 in /Users/carsten/Sites/Zend/Console/Getopt.php on line 815

Notice: Undefined offset:  1 in /Users/carsten/Sites/Zend/Console/Getopt.php on line 815
PHP Warning:  Invalid argument supplied for foreach() in /Users/carsten/Sites/Zend/Console/Getopt.php on line 815

Warning: Invalid argument supplied for foreach() in /Users/carsten/Sites/Zend/Console/Getopt.php on line 815

Comments

Assigned to Bill

This issue steems from a badly engineered switching between ZEND and GNU Modes.

Zend_Console_GetOpt works with two modes on options MODE_ZEND and MODE_GNU. What happens in the above example is the following:

In the constructor arguments are created in MODE_GNU. Console_GetOpt recognizes that and reverts its setting from MODE_ZEND (default) to MODE_GNU. Now when the addRules() is called with v|verbose Console_GetOpt is still in MODE_GNU and tries to add the array accordingly which fails.

The fix for this issue is very simple and just consists of not switching to MODE GNU when first adding a string then an array.

I suggest a refactoring to strategy pattern to fix the strange switch statement. I will add an issue for this.

Changing issues in preparation for the 1.7.0 release.