Symfony2 控制台,按位选项,使用可选的 --no 前缀?
Symfony2 Console, bitwise options, using optional --no prefix?
有人通过 Symfony2 控制台使用过按位运算符吗?或者类似于 --[no-]flag
?
风格的东西
我已经 运行 进入了一个可以使用 --[no-]hardware
、--[no-software]
触发 include/exclude 硬件、include/exclude 软件的实例。我一共有3个不同的选项可以触发。
我可以使用 symfony/console 2.8.x?
参考有关此功能的任何在线示例
尝试以下操作:
namespace AppBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class FlagTestCommand extends Command
{
private static $flags = [
'hardware' => 1,
'software' => 2,
'item3' => 4,
'item4' => 8,
];
protected function configure()
{
$this->setName('flags:test');
foreach (self::$flags as $flagName => $bit) {
$this
->addOption('no-' . $flagName, null, InputOption::VALUE_NONE)
->addOption($flagName, null, InputOption::VALUE_NONE)
;
}
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// you can configure your default bits here
$bitsSet = 0;
foreach (self::$flags as $flagName => $bit) {
if ($input->getOption($flagName)) {
// bit should be set
$bitsSet |= $bit;
}
if ($input->getOption('no-' . $flagName)) {
// bit should not be set
$bitsSet &= ~$bit;
}
}
// check if flags are set
$hardwareEnabled = ($bitsSet & self::$flags['hardware']) === self::$flags['hardware'];
$softwareEnabled = ($bitsSet & self::$flags['software']) === self::$flags['software'];
var_dump(
$hardwareEnabled,
$softwareEnabled
);
}
}
输出:
$ php bin/console flags:test -h
Usage:
flags:test [options]
Options:
--no-hardware
--hardware
--no-software
--software
--no-item3
--item3
--no-item4
--item4
$ php bin/console flags:test
bool(false)
bool(false)
$ php bin/console flags:test --hardware
bool(true)
bool(false)
$ php bin/console flags:test --hardware --no-hardware
bool(false)
bool(false)
$ php bin/console flags:test --hardware --no-hardware --software
bool(false)
bool(true)
PHP位运算符参考:http://php.net/manual/en/language.operators.bitwise.php
有人通过 Symfony2 控制台使用过按位运算符吗?或者类似于 --[no-]flag
?
我已经 运行 进入了一个可以使用 --[no-]hardware
、--[no-software]
触发 include/exclude 硬件、include/exclude 软件的实例。我一共有3个不同的选项可以触发。
我可以使用 symfony/console 2.8.x?
参考有关此功能的任何在线示例尝试以下操作:
namespace AppBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class FlagTestCommand extends Command
{
private static $flags = [
'hardware' => 1,
'software' => 2,
'item3' => 4,
'item4' => 8,
];
protected function configure()
{
$this->setName('flags:test');
foreach (self::$flags as $flagName => $bit) {
$this
->addOption('no-' . $flagName, null, InputOption::VALUE_NONE)
->addOption($flagName, null, InputOption::VALUE_NONE)
;
}
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// you can configure your default bits here
$bitsSet = 0;
foreach (self::$flags as $flagName => $bit) {
if ($input->getOption($flagName)) {
// bit should be set
$bitsSet |= $bit;
}
if ($input->getOption('no-' . $flagName)) {
// bit should not be set
$bitsSet &= ~$bit;
}
}
// check if flags are set
$hardwareEnabled = ($bitsSet & self::$flags['hardware']) === self::$flags['hardware'];
$softwareEnabled = ($bitsSet & self::$flags['software']) === self::$flags['software'];
var_dump(
$hardwareEnabled,
$softwareEnabled
);
}
}
输出:
$ php bin/console flags:test -h
Usage:
flags:test [options]
Options:
--no-hardware
--hardware
--no-software
--software
--no-item3
--item3
--no-item4
--item4
$ php bin/console flags:test
bool(false)
bool(false)
$ php bin/console flags:test --hardware
bool(true)
bool(false)
$ php bin/console flags:test --hardware --no-hardware
bool(false)
bool(false)
$ php bin/console flags:test --hardware --no-hardware --software
bool(false)
bool(true)
PHP位运算符参考:http://php.net/manual/en/language.operators.bitwise.php