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

Issue Type: Bug Created: 2008-12-23T09:49:47.000+0000 Last Updated: 2009-01-14T13:34:14.000+0000 Status: Resolved Fix version(s): - 1.7.3 (19/Jan/09)

Reporter: Remy Damour (remy215) Assignee: Benjamin Eberlei (beberlei) Tags: - Zend_Console_Getopt

Related issues: Attachments:


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


Posted by Remy Damour (remy215) on 2008-12-23T09:54:33.000+0000

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

<pre class="highlight">protected function _parseSingleOption($flag, &$argv)
    if ($flag === '') return;
    return parent::_parseSingleOption($flag, $argv);

Posted by Remy Damour (remy215) on 2008-12-23T10:09:09.000+0000

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:

<pre class="highlight">
    public function parse()
        if ($this->_parsed === true) {
        $argv = $this->_argv;
        $this->_options = array();
        $this->_remainingArgs = array();
        while (count($argv) > 0) {
            if ($argv[0] == '--') {
                if ($this->_getoptConfig[self::CONFIG_DASHDASH]) {
                    $this->_remainingArgs = array_merge($this->_remainingArgs, $argv);
            if (substr($argv[0], 0, 2) == '--') {
            } else if ('-' != $argv[0] && substr($argv[0], 0, 1) == '-') {
            } 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) == '-') {

Posted by Benjamin Eberlei (beberlei) on 2009-01-09T06:38:59.000+0000

Fixed in next version.

Posted by Benjamin Eberlei (beberlei) on 2009-01-14T13:14:15.000+0000

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

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.