Zend Framework

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

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.7.1
  • Fix Version/s: 1.7.3
  • Component/s: Zend_Console_Getopt
  • Labels:
    None

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:

myphpscript.php -

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

Activity

Hide
Remy Damour added a comment -

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);
}
Show
Remy Damour added a comment - 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);
}
Hide
Remy Damour added a comment -

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 ('-' != $argv[0] && substr($argv[0], 0, 1) == '-') {

Show
Remy Damour added a comment - 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 ('-' != $argv[0] && substr($argv[0], 0, 1) == '-') {
Hide
Benjamin Eberlei added a comment -

Fixed in next version.

Show
Benjamin Eberlei added a comment - Fixed in next version.
Hide
Benjamin Eberlei added a comment -

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

Show
Benjamin Eberlei added a comment - merged back into 1.7 release branch, due to possible 1.7.3 release

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: