8000 merged branch jmikola/double-dash (PR #3624) · symfony/symfony@645d09c · GitHub
[go: up one dir, main page]

Skip to content

Commit 645d09c

Browse files
committed
merged branch jmikola/double-dash (PR #3624)
Commits ------- 4d4ef24 [Console] Stop parsing options after encountering "--" token Discussion ---------- [Console] Stop parsing options after encountering "--" token This enables support for arguments with leading dashes (e.g. "-1"), as supported by getopt in other languages. [![Build Status](https://secure.travis-ci.org/jmikola/symfony.png?branch=double-dash)](http://travis-ci.org/jmikola/symfony) The test suite currently fails due to 7a54fe4. ArgvInputTest passes, and these changes don't appear to break anything else. ![](http://media.giantbomb.com/uploads/2/27528/1061704-mario_kart_double_dash___title_screen_super.jpg) Aside: This got me thinking about how one would pass an option value of "-1". I suppose for input options with `VALUE_OPTIONAL`, it would be ambiguous if "-1" followed; however, `VALUE_REQUIRED` should probably require that the next token is captured as the option value. In my tests, a required option value with a leading dash was interpreted as another option. The workaround for al 10000 l of this is to use the space-less syntax (e.g. `-f=-1`). --------------------------------------------------------------------------- by fabpot at 2012-03-17T08:43:15Z AFAIK, the `--` should disable both option and argument parsing, no? --------------------------------------------------------------------------- by jmikola at 2012-03-18T02:13:51Z If that were the case, what would be the point of using `--` at all? :) * http://wiki.bash-hackers.org/dict/terms/end_of_options * http://perldoc.perl.org/Getopt/Long.html#Mixing-command-line-option-with-other-arguments
2 parents 3d84153 + 4d4ef24 commit 645d09c

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

CHANGELOG-2.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
168168
* made the defaults (helper set, commands, input definition) in Application more easily customizable
169169
* added support for the shell even if readline is not available
170170
* added support for process isolation in Symfony shell via `--process-isolation` switch
171+
* added support for `--`, which disables options parsing after that point (tokens will be parsed as arguments)
171172

172173
### ClassLoader
173174

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,14 @@ protected function setTokens(array $tokens)
7575
*/
7676
protected function parse()
7777
{
78+
$parseOptions = true;
7879
$this->parsed = $this->tokens;
7980
while (null !== $token = array_shift($this->parsed)) {
80-
if (0 === strpos($token, '--')) {
81+
if ($parseOptions && '--' == $token) {
82+
$parseOptions = false;
83+
} elseif ($parseOptions && 0 === strpos($token, '--')) {
8184
$this->parseLongOption($token);
82-
} elseif ('-' === $token[0]) {
85+
} elseif ($parseOptions && '-' === $token[0]) {
8386
$this->parseShortOption($token);
8487
} else {
8588
$this->parseArgument($token);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,24 @@ public function testParser()
151151
$input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name=baz'));
152152
$input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
153153
$this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions());
154+
155+
try {
156+
$input = new ArgvInput(array('cli.php', '-1'));
157+
$input->bind(new InputDefinition(array(new InputArgument('number'))));
158+
$this->fail('->parse() throws a \RuntimeException if an unknown option is passed');
159+
} catch (\Exception $e) {
160+
$this->assertInstanceOf('\RuntimeException', $e, '->parse() parses arguments with leading dashes as options without having encountered a double-dash sequence');
161+
$this->assertEquals('The "-1" option does not exist.', $e->getMessage(), '->parse() parses arguments with leading dashes as options without having encountered a double-dash sequence');
162+
}
163+
164+
$input = new ArgvInput(array('cli.php', '--', '-1'));
165+
$input->bind(new InputDefinition(array(new InputArgument('number'))));
166+
$this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
167+
168+
$input = new ArgvInput(array('cli.php', '-f', 'bar', '--', '-1'));
169+
$input->bind(new InputDefinition(array(new InputArgument('number'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
170+
$this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses arguments with leading dashes as options before having encountered a double-dash sequence');
171+
$this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
154172
}
155173

156174
public function testGetFirstArgument()

0 commit comments

Comments
 (0)
0