[Console] fix handling positional arguments
This commit is contained in:
parent
ed09dc138e
commit
fad17a95ea
@ -110,6 +110,8 @@ abstract class Input implements InputInterface, StreamableInputInterface
|
|||||||
throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
|
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();
|
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));
|
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;
|
$this->arguments[$name] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,6 +75,11 @@ class InputTest extends TestCase
|
|||||||
$input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')]));
|
$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('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');
|
$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()
|
public function testSetInvalidArgument()
|
||||||
|
Reference in New Issue
Block a user