8000 [Console] Add a way to check if an argument/option was provided by ogizanagi · Pull Request #21210 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Add a way to check if an argument/option was provided #21210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Symfony/Component/Console/Input/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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.
*
Expand Down
42 changes: 42 additions & 0 deletions src/Symfony/Component/Console/Tests/Input/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
0