Description
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:
Symfony version 2.5.2 - app/dev/debug