Попробуйте следующее:
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