如何使用 Symfony 控制台识别是否提供了没有值的选项?
How to identify if an option was supplied without a value with Symfony Console?
使用 Symfony3 控制台,我如何判断用户提供了一个选项,但没有提供值?与根本不提供选项相反?
作为示例,采用以下控制台配置。
<?php
class MyCommand extends \Symfony\Component\Console\Command\Command
{
// ...
protected function configure()
{
$this->setName('test')
->setDescription('update an existing operation.')
->addOption(
'option',
null,
InputOption::VALUE_OPTIONAL,
'The ID of the operation to update.'
);
}
}
命令帮助将选项说明为--option[=OPTION]
,因此我可以通过以下方式调用它。
bin/console test
bin/console test --option
bin/console test --option=foo
但是,在前两种情况下,$input->getOption()
会returnNULL
。我预计在第二种情况下它会 return TRUE
,或者表明提供了选项的东西。
所以我不知道如何区分根本没有提供的选项和提供了但没有值的选项。
如果无法区分,InputOption::VALUE_OPTIONAL
的用例是什么?
您正在将两件事结合在一起。没有值 InputOption::VALUE_NONE
的选项和具有可选值 InputOption::VALUE_OPTIONAL
的选项。
文档说:http://symfony.com/doc/current/console/input.html
There is nothing forbidding you to create a command with an option that optionally accepts a value. However, there is no way you can distinguish when the option was used without a value (command --language) or when it wasn't used at all (command). In both cases, the value retrieved for the option will be null.
这正是您的情况。
您无法区分什么时候参数根本没有传递或传递但没有值。这就是 InputOption::VALUE_NONE
的目的。
根据您的用例,您可以为将在 console test
和 console test --option
情况下使用的参数提供默认值。
另请注意,addOption
将快捷方式作为参数作为第二个参数。
public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null)
21 年 6 月 10 日编辑:这仅适用于 Symfony 3.3 及以下版本。现在正确答案是ScorpioT1000
提供的答案
在 Symfony\Component\Console\Input\InputInterface
中四处寻找后,我发现了 getParameterOption()
方法,它能够区分未使用的选项、未使用值的选项和使用值的选项.
在命令的 configure()
方法中:
$this->addOption('test', null, InputOption::VALUE_OPTIONAL);
在命令的 execute()
方法中:
$test = $input->getOption('test'); $rawTest = $input->getParameterOption('--test');
为给定的命令行生成以下值:
> bin/console some:cmd
$test
=> null
$rawTest
=> false
> bin/console some:cmd --test
$test
=> null
$rawTest
=> null
> bin/console some:cmd --test=something
$test
=> "something"
$rawTest
=> "something"
从 Symfony 3.4 开始,您只需将默认值设置为 false
并检查:
- 如果值为
false
,则该选项不存在
- 如果值为
null
选项存在但没有值
- 否则它有它的价值
例如
$this->addOption('force', null, InputOption::VALUE_OPTIONAL, 'Force something', false);
$force = $input->getOption('force') !== false;
使用 Symfony3 控制台,我如何判断用户提供了一个选项,但没有提供值?与根本不提供选项相反?
作为示例,采用以下控制台配置。
<?php
class MyCommand extends \Symfony\Component\Console\Command\Command
{
// ...
protected function configure()
{
$this->setName('test')
->setDescription('update an existing operation.')
->addOption(
'option',
null,
InputOption::VALUE_OPTIONAL,
'The ID of the operation to update.'
);
}
}
命令帮助将选项说明为--option[=OPTION]
,因此我可以通过以下方式调用它。
bin/console test
bin/console test --option
bin/console test --option=foo
但是,在前两种情况下,$input->getOption()
会returnNULL
。我预计在第二种情况下它会 return TRUE
,或者表明提供了选项的东西。
所以我不知道如何区分根本没有提供的选项和提供了但没有值的选项。
如果无法区分,InputOption::VALUE_OPTIONAL
的用例是什么?
您正在将两件事结合在一起。没有值 InputOption::VALUE_NONE
的选项和具有可选值 InputOption::VALUE_OPTIONAL
的选项。
文档说:http://symfony.com/doc/current/console/input.html
There is nothing forbidding you to create a command with an option that optionally accepts a value. However, there is no way you can distinguish when the option was used without a value (command --language) or when it wasn't used at all (command). In both cases, the value retrieved for the option will be null.
这正是您的情况。
您无法区分什么时候参数根本没有传递或传递但没有值。这就是 InputOption::VALUE_NONE
的目的。
根据您的用例,您可以为将在 console test
和 console test --option
情况下使用的参数提供默认值。
另请注意,addOption
将快捷方式作为参数作为第二个参数。
public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null)
21 年 6 月 10 日编辑:这仅适用于 Symfony 3.3 及以下版本。现在正确答案是ScorpioT1000
提供的答案在 Symfony\Component\Console\Input\InputInterface
中四处寻找后,我发现了 getParameterOption()
方法,它能够区分未使用的选项、未使用值的选项和使用值的选项.
在命令的 configure()
方法中:
$this->addOption('test', null, InputOption::VALUE_OPTIONAL);
在命令的 execute()
方法中:
$test = $input->getOption('test'); $rawTest = $input->getParameterOption('--test');
为给定的命令行生成以下值:
> bin/console some:cmd
$test
=> null
$rawTest
=> false
> bin/console some:cmd --test
$test
=> null
$rawTest
=> null
> bin/console some:cmd --test=something
$test
=> "something"
$rawTest
=> "something"
从 Symfony 3.4 开始,您只需将默认值设置为 false
并检查:
- 如果值为
false
,则该选项不存在 - 如果值为
null
选项存在但没有值 - 否则它有它的价值
例如
$this->addOption('force', null, InputOption::VALUE_OPTIONAL, 'Force something', false);
$force = $input->getOption('force') !== false;