Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Route does not match double-dash flag #6

Open

Description

@hschletz

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.

Activity

  1. weierophinney commented on Nov 14, 2015

    @weierophinney
    Member

    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
    #6.

  2. hschletz commented on Nov 15, 2015

    @hschletz
    Author

    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.

  3. weierophinney commented on Dec 31, 2019

    @weierophinney
    Member

    This repository has been closed and moved to laminas/laminas-console; a new issue has been opened at laminas/laminas-console#9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions