ZF-5345: using '-' as argument on cli call raise Zend_Console_Getopt_Exception("Option \"\" is not recognized.")

Description

If we make a cli call to a php script without using options and using '-' as argument, Zend_Console_Getopt raise a Zend_Console_Getopt_Exception("Option \"\" is not recognized.") exception.

ex: ```

This is very annoying because it's a standard to use '-' as argument to tell to the script to look at STDIN to get its input.

Regards, Remy Damour

Comments

as a quick fix, I extended this class, overloading _parseSingleOption method as described below:

protected function _parseSingleOption($flag, &$argv)
{
    if ($flag === '') return;
    return parent::_parseSingleOption($flag, $argv);
}

Fix on my quick fix :-) (I should have tested first): my quick fix does not generate any error, but ignores "-" as a remainingArgument, considering it as an option flag: $this->getRemainingArgs() returns an empty array instead of array("-");

The fix below solves all problems (i've highlighted my change). I no longer overload _parseSingleOption() method, but parse() method instead:


    public function parse()
    {
        if ($this->_parsed === true) {
            return;
        }
        $argv = $this->_argv;
        $this->_options = array();
        $this->_remainingArgs = array();
        while (count($argv) > 0) {
            if ($argv[0] == '--') {
                array_shift($argv);
                if ($this->_getoptConfig[self::CONFIG_DASHDASH]) {
                    $this->_remainingArgs = array_merge($this->_remainingArgs, $argv);
                    break;
                }
            }
            if (substr($argv[0], 0, 2) == '--') {
                $this->_parseLongOption($argv);
            } else if ('-' != $argv[0] && substr($argv[0], 0, 1) == '-') {
                $this->_parseShortOptionCluster($argv);
            } else {
                $this->_remainingArgs[] = array_shift($argv);
            }
        }
        $this->_parsed = true;
        return $this;
    }

change made is on last "else if" statement: } else if ({color:green}'-' != $argv[0] && {color}substr($argv[0], 0, 1) == '-') {

Fixed in next version.

merged back into 1.7 release branch, due to possible 1.7.3 release