Search code examples
phpsymfonysymfony-2.8symfony-console

Symfony2 Console, bitwise options, using optional --no prefix?


Has anybody used bitwise operators via Symfony2 Console? Or something similar to style of --[no-]flag?

I've ran into an instance where I can use --[no-]hardware, --[no-software] for triggering include/exclude hardware, include/exclude software. I have a total of 3 different options that can be triggered.

Any examples online I can reference on this functionality using symfony/console 2.8.x?


Solution

  • Try the following:

    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
            );
        }
    }
    

    Output:

    $ 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 bitwise operators reference: http://php.net/manual/en/language.operators.bitwise.php