bug #41952 [Console] fix handling positional arguments (nicolas-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[Console] fix handling positional arguments

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Follow up of #41949

Commits
-------

fad17a95ea [Console] fix handling positional arguments
This commit is contained in:
Nicolas Grekas 2021-07-03 21:16:28 +02:00
commit e93f8c0ad3
2 changed files with 9 additions and 0 deletions

View File

@ -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;
}

View File

@ -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()