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

Skip to content

Commit bcbbb28

Browse files
tiagojsagfabpot
authored andcommitted
Throw exception if value is passed to VALUE_NONE input, long syntax
1 parent fb0a0a7 commit bcbbb28

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)