Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a76c4f

Browse files
committed
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
2 parents fb0a0a7 + bcbbb28 commit 9a76c4f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Symfony/Component/Console/Input/ArgvInput.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private function parseShortOptionSet($name)
134134

135135
break;
136136
} else {
137-
$this->addLongOption($option->getName(), true);
137+
$this->addLongOption($option->getName(), null);
138138
}
139139
}
140140
}
@@ -220,6 +220,10 @@ private function addLongOption($name, $value)
220220
$value = null;
221221
}
222222

223+
if (null !== $value && !$option->acceptValue()) {
224+
throw new \RuntimeException(sprintf('The "--%s" option does not accept a value.', $name, $value));
225+
}
226+
223227
if (null === $value && $option->acceptValue() && count($this->parsed)) {
224228
// if option accepts an optional or mandatory argument
225229
// let's see if there is one provided

src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ public function testParser()
105105
$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');
106106
}
107107

108+
try {
109+
$input = new ArgvInput(array('cli.php', '--foo=bar'));
110+
$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))));
111+
$this->fail('->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
112+
} catch (\Exception $e) {
113+
$this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
114+
$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');
115+
}
116+
108117
try {
109118
$input = new ArgvInput(array('cli.php', 'foo', 'bar'));
110119
$input->bind(new InputDefinition());

0 commit comments

Comments
 (0)