zendframework / zend-console

Console component from Zend Framework
BSD 3-Clause "New" or "Revised" License
48 stars 31 forks source link

Route does not match double-dash flag #6

Open hschletz opened 8 years ago

hschletz commented 8 years ago

This is my simple route definition:

'[--flag|-f] <arg>'

Only "-f" and "-flag" match, "--flag" does not. According to the documentation, "--flag" should match, which would also follow common conventions.

weierophinney commented 8 years ago

What OS are you observing this on? We have used these flags successfully on zf-deploy (see its route configuration).

One note: if the glad should allow an argument, you need to use special notation:

[--flag|-f]:flag

I suggest looking at the tests, as those demonstrate the full range of options supported — which include the scenario you indicated. I cannot reproduce the issue you present on Linux, which leads me to believe it may be an environmental issue. On Nov 14, 2015 1:12 PM, "hschletz" notifications@github.com wrote:

This is my simple route definition:

'[--flag|-f] '

Only "-f" and "-flag" match, "--flag" does not. According to the documentation, "--flag" should match, which would also follow common conventions.

— Reply to this email directly or view it on GitHub https://github.com/zendframework/zend-console/issues/6.

hschletz commented 8 years ago

I forgot to test with real application. The problem only occurs during unit tests for my controller. It is caused by the command line parser regex in AbstractControllerTestCase::run():

preg_match_all('/(--\S+[= ]"[^\s"]*\s*[^\s"]*")|(--\S+=\S+|--\S+\s\S+|\S+)/', $url, $matches);

If I dispatch "arg --flag", the command line gets broken down correctly:

array(2) {
  [0]=>
  string(15) "arg"
  [1]=>
  string(10) "--flag"
}

but with "--flag arg" I get:

array(1) {
  [0]=>
  string(26) "--flag arg"
}

This would be correct in case of value parameters, but not with simple flags. But even with value parameters, this would not work as documented in practice, as demonstrated by this simple script:

<?php
var_dump($_SERVER['argv']);

Run it with given arguments:

php test.php --flag arg
array(3) {
  [0]=>
  string(8) "test.php"
  [1]=>
  string(6) "--flag"
  [2]=>
  string(3) "arg"
}

The arguments are passed according to my route specification, which is why the script works as expected when run from the command line, but the unit test fails.

Out of curiosity, I changed the route to "--flag=". It turns out that "--flag=arg" works as expected, but "--flag arg" is still treated as separate parameters due to the way the OS parses command lines (I ran it on Linux BTW).

So either the request params must be concatenated if argv contains a single "--flag" instead of "--flag=arg", or the "--flag arg" variant should be removed from the documentation.

The command line parser in AbstractControllerTestCase needs to be fixed too. I doubt that a single regex will be able to handle all edge cases the same way as the OS does, like escaped quotes. For complex command lines that get parsed incorrectly, it would be nice to pass parameters as an array in the third argument to dispatch(), but currently this gets unconditionally replaced with the parsed command line for console tests.

weierophinney commented 4 years ago

This repository has been closed and moved to laminas/laminas-console; a new issue has been opened at https://github.com/laminas/laminas-console/issues/9.