Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Console] Option with optional argument always returning null whether specified or not #11572

Copy link
Copy link
Closed
@EdwardIII

Description

@EdwardIII
Issue body actions

From the docs (http://symfony.com/doc/current/components/console/introduction.html#using-command-options):

It is also possible to make an option optionally accept a value (so that --yell
or --yell=loud work). Options can also be configured to accept an array of values.

In practice it doesn't seem to work this way. When creating a Command like this:

    <?php
                ->addOption(
                    'createfile',
                    null,
                    InputOption::VALUE_OPTIONAL,
                    'Do not delete the file, specify argument to choose an alternative filename'
                )


    ...

                $createfile = $input->getOption('createfile');
                var_dump($createfile); // returns NULL

And calling it like this:

php app/console my:command --createfile

The value of $createfile is null, even though the switch is present.

I can't find any way to identify that the command is present unless it has a value.
i.e. php app/console my:command --createfile='/tmp/somefile.txt' does what you'd expect and returns the string "/tmp/somefile.txt".

I can see no other way of identifying whether this option is present?

There seems to be some code supporting the idea that you get a different return value if the option is provided but with no optional value present:

https://github.com/symfony/symfony/blob/2.5/src/Symfony/Component/Console/Input/ArgvInput.php#L234

This looks like it should provide you with an empty string if this parameter is provided but has no value, however in my tests count($this->parsed) was 0, which meant that this inner block could never get executed.

I checked out the unit tests for ArgvInput but couldn't find a test that seemed to be based on this particular use case:

https://github.com/symfony/symfony/blob/2.5/src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php

Symfony version 2.5.2 - app/dev/debug

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.