diff --git a/src/Symfony/Component/Console/Input/Input.php b/src/Symfony/Component/Console/Input/Input.php index 474a020312908..f30da176734ff 100644 --- a/src/Symfony/Component/Console/Input/Input.php +++ b/src/Symfony/Component/Console/Input/Input.php @@ -141,6 +141,22 @@ public function hasArgument($name) return $this->definition->hasArgument($name); } + /** + * @param string $name + * + * @return bool True if the argument was provided for this input + * + * @throws InvalidArgumentException When no InputArgument exists in the InputDefinition with this name + */ + public function isArgumentProvided($name) + { + if (!$this->definition->hasArgument($name)) { + throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); + } + + return array_key_exists($name, $this->arguments); + } + /** * {@inheritdoc} */ @@ -181,6 +197,22 @@ public function hasOption($name) return $this->definition->hasOption($name); } + /** + * @param string $name + * + * @return bool True if the option was provided for this input + * + * @throws InvalidArgumentException When no InputOption exists in the InputDefinition with this name + */ + public function isOptionProvided($name) + { + if (!$this->definition->hasOption($name)) { + throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); + } + + return array_key_exists($name, $this->options); + } + /** * Escapes a token through escapeshellarg if it contains unsafe chars. * diff --git a/src/Symfony/Component/Console/Tests/Input/InputTest.php b/src/Symfony/Component/Console/Tests/Input/InputTest.php index ef7e5c4309a30..0d67ff5684361 100644 --- a/src/Symfony/Component/Console/Tests/Input/InputTest.php +++ b/src/Symfony/Component/Console/Tests/Input/InputTest.php @@ -137,4 +137,46 @@ public function testSetGetStream() $input->setStream($stream); $this->assertSame($stream, $input->getStream()); } + + public function testIsArgumentProvided() + { + $definition = new InputDefinition(array(new InputArgument('name'))); + + $input = new ArrayInput(array(), $definition); + $this->assertFalse($input->isArgumentProvided('name')); + + $input = new ArrayInput(array('name' => 'foo'), $definition); + $this->assertTrue($input->isArgumentProvided('name')); + } + + /** + * @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException + * @expectedExceptionMessage The "name" argument does not exist. + */ + public function testIsArgumentProvidedThrowsOnUndefinedArgument() + { + $input = new ArrayInput(array(), new InputDefinition()); + $input->isArgumentProvided('name'); + } + + public function testIsOptionProvided() + { + $definition = new InputDefinition(array(new InputOption('bar'))); + + $input = new ArrayInput(array(), $definition); + $this->assertFalse($input->isOptionProvided('bar')); + + $input = new ArrayInput(array('--bar' => 'foo'), $definition); + $this->assertTrue($input->isOptionProvided('bar')); + } + + /** + * @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException + * @expectedExceptionMessage The "bar" option does not exist. + */ + public function testIsOptionProvidedThrowsOnUndefinedOption() + { + $input = new ArrayInput(array(), new InputDefinition()); + $input->isOptionProvided('bar'); + } }