Description
When using a console command, if you name an optional parameter and give it an empty value, that value is ignored which can cause unexpected and unhelpful errors.
This can be used to see what I'm talking about:
(requires symfony/console)
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
require 'vendor/autoload.php';
$application = new Application();
$application
->register('echo')
->addOption('prefix', null, InputOption::VALUE_OPTIONAL)
->addArgument('value', InputArgument::REQUIRED)
->setCode(function ($input, $output) {
var_dump($input->getOption('prefix'));
var_dump($input->getArgument('value'));
});
$application->run();
Using this command you can observe some weird things.
These show the first value as prefix and the second value as value:
php command.php echo --prefix="123" "456"
php command.php echo --prefix= "123" "456"
and even
php command.php echo --prefix= "" ""
will output prefix and value each as empty strings
Note the space after the = in the second two instances. This means that if you've got a function somewhere plugging in values to a command and running it, but --prefix doesn't have a value or is an empty string at that point, you could end up with:
php command.php echo --prefix= 'value'
or
php command.php echo --prefix='' 'value'
which return:
[Symfony\Component\Console\Exception\RuntimeException]
Not enough arguments (missing: "value").
either way you will get an error because it doesn't think you've provided a value for the required parameter 'value' when really you have, because it didn't recognize your lack of value or empty string value for 'prefix'. Since the actual error is for a provided parameter, whereas the parsing issue is actually with another parameter these errors can be particularly tricky to identify and track down.