Skip to content

Commit

Permalink
merged branch tiagojsag/console_input_options_fix (PR #8199)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Jun 13, 2013
2 parents fb0a0a7 + bcbbb28 commit 9a76c4f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Symfony/Component/Console/Input/ArgvInput.php
Expand Up @@ -134,7 +134,7 @@ private function parseShortOptionSet($name)

break;
} else {
$this->addLongOption($option->getName(), true);
$this->addLongOption($option->getName(), null);
}
}
}
Expand Down Expand Up @@ -220,6 +220,10 @@ private function addLongOption($name, $value)
$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
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php
Expand Up @@ -105,6 +105,15 @@ public function testParser()
$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());
Expand Down

0 comments on commit 9a76c4f

Please sign in to comment.