8000 [Console] Fix hasParameterOption / getParameterOption when used with multiple flags by greg-1-anderson · Pull Request #25893 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Fix hasParameterOption / getParameterOption when used with multiple flags #25893

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

Merged
merged 1 commit into from
Feb 7, 2018
Merged
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
8000
19 changes: 13 additions & 6 deletions src/Symfony/Component/Console/Input/ArgvInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,11 @@ public function hasParameterOption($values)

foreach ($this->tokens as $token) {
foreach ($values as $value) {
if ($token === $value || 0 === strpos($token, $value.'=')) {
// Options with values:
// For long options, test for '--option=' at beginning
// For short options, test for '-o' at beginning
$leading = 0 === strpos($value, '--') ? $value.'=' : $value;
if ($token === $value || 0 === strpos($token, $leading)) {
return true;
}
}
Expand All @@ -300,13 +304,16 @@ public function getParameterOption($values, $default = false)
$token = array_shift($tokens);

foreach ($values as $value) {
if ($token === $value || 0 === strpos($token, $value.'=')) {
if (false !== $pos = strpos($token, '=')) {
return substr($token, $pos + 1);
}

if ($token === $value) {
return array_shift($tokens);
}
// Options with values:
// For long options, test for '--option=' at beginning
// For short options, test for '-o' at beginning
$leading = 0 === strpos($value, '--') ? $value.'=' : $value;
if (0 === strpos($token, $leading)) {
return substr($token, strlen($leading));
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Console/Input/InputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function getFirstArgument();
*
* This method is to be used to introspect the input parameters
* before they have been validated. It must be used carefully.
* Does not necessarily return the correct result for short options
* when multiple flags are combined in the same option.
*
* @param string|array $values The values to look for in the raw parameters (can be an array)
*
Expand All @@ -42,6 +44,8 @@ public function hasParameterOption($values);
*
* This method is to be used to introspect the input parameters
* before they have been validated. It must be used carefully.
* Does not necessarily return the correct result for short options
* when multiple flags are combined in the same option.
*
* @param string|array $values The value(s) to look for in the raw parameters (can be an array)
* @param mixed $default The default value to return if no result is found
Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ public function testHasParameterOption()
$input = new ArgvInput(array('cli.php', '-f', 'foo'));
$this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input');

$input = new ArgvInput(array('cli.php', '-etest'));
$this->assertTrue($input->hasParameterOption('-e'), '->hasParameterOption() returns true if the given short option is in the raw input');
$this->assertFalse($input->hasParameterOption('-s'), '->hasParameterOption() returns true if the given short option is in the raw input');

$input = new ArgvInput(array('cli.php', '--foo', 'foo'));
$this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given short option is in the raw input');

Expand All @@ -306,6 +310,33 @@ 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 testHasParameterOptionEdgeCasesAndLimitations()
{
$input = new ArgvInput(array('cli.php', '-fh'));
// hasParameterOption does not know if the previous short option, -f,
// takes a value or not. If -f takes a value, then -fh does NOT include
// -h; Otherwise it does. Since we do not know which short options take
// values, hasParameterOption does not support this use-case.
$this->assertFalse($input->hasParameterOption('-h'), '->hasParameterOption() returns true if the given short option is in the raw input');
// hasParameterOption does detect that `-fh` contains `-f`, since
// `-f` is the first short option in the set.
$this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input');
// The test below happens to pass, although it might make more sense
// to disallow it, and require the use of
// $input->hasParameterOption('-f') && $input->hasParameterOption('-h')
// instead.
$this->assertTrue($input->hasParameterOption('-fh'), '->hasParameterOption() returns true if the given short option is in the raw input');
// In theory, if -fh is supported, then -hf should also work.
// However, this is not supported.
$this->assertFalse($input->hasParameterOption('-hf'), '->hasParameterOption() returns true if the given short option is in the raw input');

$input = new ArgvInput(array('cli.php', '-f', '-h'));
// If hasParameterOption('-fh') is supported for 'cli.php -fh', then
// one might also expect that it should also be supported for
// 'cli.php -f -h'. However, this is not supported.
$this->assertFalse($input->hasParameterOption('-fh'), '->hasParameterOption() returns true if the given short option is in the raw input');
Copy link
Contributor

Choose a reason for hiding this comment

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

So you break the behaviour that was working with the patch IIUC?

Copy link
Member

Choose a reason for hiding this comment

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

If the patch breaks the existing and we can't manage to make both work, better revert it for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The patch does indeed have serious consequences for Drush, Robo, and potentially other clients as well. It is regrettable that get/hasParameterOptions do not have enough information available to handle multiple short options correctly.

}

public function testToString()
{
$input = new ArgvInput(array('cli.php', '-f', 'foo'));
Expand All @@ -327,6 +358,7 @@ public function testGetParameterOptionEqualSign($argv, $key, $expected)
public function provideGetParameterOptionValues()
{
return array(
array(array('app/console', 'foo:bar', '-edev'), '-e', 'dev'),
array(array('app/console', 'foo:bar', '-e', 'dev'), '-e', 'dev'),
array(array('app/console', 'foo:bar', '--env=dev'), '--env', 'dev'),
array(array('app/console', 'foo:bar', '-e', 'dev'), array('-e', '--env'), 'dev'),
Expand Down
0