From fad17a95eab25460d5aea81a3dded01ad980ae32 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 2 Jul 2021 19:03:33 +0200 Subject: [PATCH] [Console] fix handling positional arguments --- src/Symfony/Component/Console/Input/Input.php | 4 ++++ src/Symfony/Component/Console/Tests/Input/InputTest.php | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/Symfony/Component/Console/Input/Input.php b/src/Symfony/Component/Console/Input/Input.php index 6e4c01e95f..6b093e7506 100644 --- a/src/Symfony/Component/Console/Input/Input.php +++ b/src/Symfony/Component/Console/Input/Input.php @@ -110,6 +110,8 @@ abstract class Input implements InputInterface, StreamableInputInterface throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); } + $name = \is_int($name) ? key(\array_slice($this->definition->getArguments(), $name, 1, true)) : $name; + return $this->arguments[$name] ?? $this->definition->getArgument($name)->getDefault(); } @@ -122,6 +124,8 @@ abstract class Input implements InputInterface, StreamableInputInterface throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); } + $name = \is_int($name) ? key(\array_slice($this->definition->getArguments(), $name, 1, true)) : $name; + $this->arguments[$name] = $value; } diff --git a/src/Symfony/Component/Console/Tests/Input/InputTest.php b/src/Symfony/Component/Console/Tests/Input/InputTest.php index 48c287cd9d..3441351b4b 100644 --- a/src/Symfony/Component/Console/Tests/Input/InputTest.php +++ b/src/Symfony/Component/Console/Tests/Input/InputTest.php @@ -75,6 +75,11 @@ class InputTest extends TestCase $input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')])); $this->assertEquals('default', $input->getArgument('bar'), '->getArgument() returns the default value for optional arguments'); $this->assertEquals(['name' => 'foo', 'bar' => 'default'], $input->getArguments(), '->getArguments() returns all argument values, even optional ones'); + + $input = new ArrayInput(['arg1' => 'foo'], new InputDefinition([new InputArgument('arg1'), new InputArgument('arg2')])); + $input->setArgument(1, 'bar'); + $this->assertEquals('bar', $input->getArgument(1)); + $this->assertEquals(['arg1' => 'foo', 'arg2' => 'bar'], $input->getArguments()); } public function testSetInvalidArgument()