[Console] fix handling positional arguments

This commit is contained in:
Nicolas Grekas 2021-07-02 19:03:33 +02:00
parent ed09dc138e
commit fad17a95ea
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()