merged branch tiagojsag/console_input_options_fix (PR #8199)
This PR was submitted for the master branch but it was merged into the 2.2 branch instead (closes #8199). Discussion ---------- [Console] Throw exception if value is passed to VALUE_NONE input optin, long syntax | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | #8135 | License | MIT Input options with InputOption::VALUE_NONE accept values in both short and long syntaxes: - When using the long syntax, no exception is thrown; - When using short, a "The %s option does not exist" exception is thrown. This PR only addresses the long syntax case. The short syntax case would require considerable refactoring of the parse code, which I believe should be discussed. I included a test that illustrates the above mentioned problem for the long syntax scenario. Commits ------- 32ea77f Throw exception if value is passed to VALUE_NONE input, long syntax
This commit is contained in:
commit
9a76c4f017
|
@ -134,7 +134,7 @@ class ArgvInput extends Input
|
|||
|
||||
break;
|
||||
} else {
|
||||
$this->addLongOption($option->getName(), true);
|
||||
$this->addLongOption($option->getName(), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -220,6 +220,10 @@ class ArgvInput extends Input
|
|||
$value = null;
|
||||
}
|
||||
|
||||
if (null !== $value && !$option->acceptValue()) {
|
||||
throw new \RuntimeException(sprintf('The "--%s" option does not accept a value.', $name, $value));
|
||||
}
|
||||
|
||||
if (null === $value && $option->acceptValue() && count($this->parsed)) {
|
||||
// if option accepts an optional or mandatory argument
|
||||
// let's see if there is one provided
|
||||
|
|
|
@ -105,6 +105,15 @@ class ArgvInputTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertEquals('The "-o" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
|
||||
}
|
||||
|
||||
try {
|
||||
$input = new ArgvInput(array('cli.php', '--foo=bar'));
|
||||
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))));
|
||||
$this->fail('->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
|
||||
$this->assertEquals('The "--foo" option does not accept a value.', $e->getMessage(), '->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
|
||||
}
|
||||
|
||||
try {
|
||||
$input = new ArgvInput(array('cli.php', 'foo', 'bar'));
|
||||
$input->bind(new InputDefinition());
|
||||
|
|
Reference in New Issue