/** * Returns the option value for a given option name. * * @param string $name The option name * * @return mixed The option value * * @throws InvalidArgumentException When option given doesn't exist */ public function getOption($name) { if (!$this->definition->hasOption($name)) { throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); } return isset($this->options[$name]) ? $this->options[$name] : $this->definition->getOption($name)->getDefault(); }
private function setOptionDescription(InputDefinition $definition, $name, $description) { $argument = $definition->getOption($name); if ($argument instanceof InputOption) { $argument->setDescription($description); } }
/** * Returns an InputOption by name. * * @param string $name The InputOption name * * @return InputOption A InputOption object * * @api */ public function getOption($name) { if ('no-' === substr($name, 0, 3)) { $switch = parent::getOption('[no-]' . substr($name, 3)); $switch->setDefault(false); return $switch; } if (parent::hasOption('[no-]' . $name)) { $switch = parent::getOption('[no-]' . $name); $switch->setDefault(true); return $switch; } return parent::getOption($name); }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage The "--bar" option does not exist. */ public function testGetInvalidOption() { $this->initializeOptions(); $definition = new InputDefinition(array($this->foo)); $definition->getOption('bar'); }
public function testGetOption() { $this->initializeOptions(); $definition = new InputDefinition(array($this->foo)); $this->assertEquals($this->foo, $definition->getOption('foo'), '->getOption() returns a InputOption by its name'); try { $definition->getOption('bar'); $this->fail('->getOption() throws an exception if the option name does not exist'); } catch (\Exception $e) { $this->assertInstanceOf('\\Exception', $e, '->getOption() throws an exception if the option name does not exist'); $this->assertEquals('The "--bar" option does not exist.', $e->getMessage()); } }