Skip to content

[Console] Add method to know parsed option #12773

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 2 commits 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
12 changes: 12 additions & 0 deletions src/Symfony/Component/Console/Input/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ public function setOption($name, $value)
$this->options[$name] = $value;
}

/**
* Returns true if an InputOption object exists by name in the command line arguments.
*
* @param string $name The InputOption name
*
* @return bool true if the InputOption object exists, false otherwise
*/
public function hasParsedOption($name)
{
return array_key_exists($name, $this->options);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe faster with isset ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samsonasik array_key_exists is different from isset. Here, a value can be null

}

/**
* Returns true if an InputOption object exists by name.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,24 @@ public function testHasParameterOption()
$this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given option with provided value is in the raw input');
}

public function testGetParameterOption()
{
$input = new ArgvInput(array('cli.php', '--foo'));
$input->bind(new InputDefinition(array(new InputOption('foo', null, InputOption::VALUE_OPTIONAL, 'foo option', 'value'))));
$this->assertEquals('value', $input->getOption('foo'));
$this->assertTrue($input->hasParsedOption('foo'));

$input = new ArgvInput(array('cli.php', '--foo'));
$input->bind(new InputDefinition(array(new InputOption('foo', null, InputOption::VALUE_OPTIONAL, 'foo option'))));
$this->assertEquals(null, $input->getOption('foo'));
$this->assertTrue($input->hasParsedOption('foo'));

$input = new ArgvInput(array('cli.php'));
$input->bind(new InputDefinition(array(new InputOption('foo', null, InputOption::VALUE_OPTIONAL, 'foo option'))));
$this->assertEquals(null, $input->getOption('foo'));
$this->assertFalse($input->hasParsedOption('foo'));
}

public function testToString()
{
$input = new ArgvInput(array('cli.php', '-f', 'foo'));
Expand Down