Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e4d4a92

Browse filesBrowse files
author
Robin Chalas
committed
[Console] Prevent ArgvInput::getFirstArgument() from returning an option value
1 parent 4912044 commit e4d4a92
Copy full SHA for e4d4a92

File tree

5 files changed

+45
-2
lines changed
Filter options

5 files changed

+45
-2
lines changed

‎src/Symfony/Component/Console/Application.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Application.php
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ public function doRun(InputInterface $input, OutputInterface $output)
202202
return 0;
203203
}
204204

205+
try {
206+
// Makes ArgvInput::getFirstArgument() able to distinguish an option from an argument.
207+
$input->bind($this->getDefinition());
208+
} catch (ExceptionInterface $e) {
209+
// Errors must be ignored, full binding/validation happens later when the command is known.
210+
}
211+
205212
$name = $this->getCommandName($input);
206213
if (true === $input->hasParameterOption(['--help', '-h'], true)) {
207214
if (!$name) {

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Input/ArgvInput.php
+18-1Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,25 @@ private function addLongOption($name, $value)
262262
*/
263263
public function getFirstArgument()
264264
{
265-
foreach ($this->tokens as $token) {
265+
$isOption = false;
266+
foreach ($this->tokens as $i => $token) {
266267
if ($token && '-' === $token[0]) {
268+
if (false !== strpos($token, '=') || !isset($this->tokens[$i + 1])) {
269+
continue;
270+
}
271+
272+
$name = '-' === $token[1] ? substr($token, 2) : substr($token, 1);
273+
if (!isset($this->options[$name]) && !$this->definition->hasShortcut($name)) {
274+
// noop
275+
} elseif ((isset($this->options[$name]) || isset($this->options[$name = $this->definition->shortcutToName($name)])) && $this->tokens[$i + 1] === $this->options[$name]) {
276+
$isOption = true;
277+
}
278+
279+
continue;
280+
}
281+
282+
if ($isOption) {
283+
$isOption = false;
267284
continue;
268285
}
269286

‎src/Symfony/Component/Console/Input/InputDefinition.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Input/InputDefinition.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,10 @@ public function getOptionDefaults()
338338
* @return string The InputOption name
339339
*
340340
* @throws InvalidArgumentException When option given does not exist
341+
*
342+
* @internal
341343
*/
342-
private function shortcutToName($shortcut)
344+
public function shortcutToName($shortcut)
343345
{
344346
if (!isset($this->shortcuts[$shortcut])) {
345347
throw new InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));

‎src/Symfony/Component/Console/Tests/ApplicationTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/ApplicationTest.php
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,19 @@ public function testRun()
884884
$this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if -n is passed');
885885
}
886886

887+
public function testRunWithGlobalOptionAndNoCommand()
888+
{
889+
$application = new Application();
890+
$application->setAutoExit(false);
891+
$application->setCatchExceptions(false);
892+
$application->getDefinition()->addOption(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL));
893+
894+
$output = new StreamOutput(fopen('php://memory', 'w', false));
895+
$input = new ArgvInput(['cli.php', '--foo', 'bar']);
896+
897+
$this->assertSame(0, $application->run($input, $output));
898+
}
899+
887900
/**
888901
* Issue #9285.
889902
*

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ public function testGetFirstArgument()
312312

313313
$input = new ArgvInput(['cli.php', '-fbbar', 'foo']);
314314
$this->assertEquals('foo', $input->getFirstArgument(), '->getFirstArgument() returns the first argument from the raw input');
315+
316+
$input = new ArgvInput(['cli.php', '--foo', 'fooval', 'bar']);
317+
$input->bind(new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('arg')]));
318+
$this->assertSame('bar', $input->getFirstArgument());
315319
}
316320

317321
public function testHasParameterOption()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.