[Console] Prevent ArgvInput::getFirstArgument() from returning an option value
This commit is contained in:
parent
c6de2dc03e
commit
46461e91f9
@ -202,6 +202,13 @@ class Application
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Makes ArgvInput::getFirstArgument() able to distinguish an option from an argument.
|
||||||
|
$input->bind($this->getDefinition());
|
||||||
|
} catch (ExceptionInterface $e) {
|
||||||
|
// Errors must be ignored, full binding/validation happens later when the command is known.
|
||||||
|
}
|
||||||
|
|
||||||
$name = $this->getCommandName($input);
|
$name = $this->getCommandName($input);
|
||||||
if (true === $input->hasParameterOption(['--help', '-h'], true)) {
|
if (true === $input->hasParameterOption(['--help', '-h'], true)) {
|
||||||
if (!$name) {
|
if (!$name) {
|
||||||
|
@ -262,8 +262,27 @@ class ArgvInput extends Input
|
|||||||
*/
|
*/
|
||||||
public function getFirstArgument()
|
public function getFirstArgument()
|
||||||
{
|
{
|
||||||
foreach ($this->tokens as $token) {
|
$isOption = false;
|
||||||
|
foreach ($this->tokens as $i => $token) {
|
||||||
if ($token && '-' === $token[0]) {
|
if ($token && '-' === $token[0]) {
|
||||||
|
if (false !== strpos($token, '=') || !isset($this->tokens[$i + 1])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If it's a long option, consider that everything after "--" is the option name.
|
||||||
|
// Otherwise, use the last char (if it's a short option set, only the last one can take a value with space separator)
|
||||||
|
$name = '-' === $token[1] ? substr($token, 2) : substr($token, -1);
|
||||||
|
if (!isset($this->options[$name]) && !$this->definition->hasShortcut($name)) {
|
||||||
|
// noop
|
||||||
|
} elseif ((isset($this->options[$name]) || isset($this->options[$name = $this->definition->shortcutToName($name)])) && $this->tokens[$i + 1] === $this->options[$name]) {
|
||||||
|
$isOption = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($isOption) {
|
||||||
|
$isOption = false;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -338,8 +338,10 @@ class InputDefinition
|
|||||||
* @return string The InputOption name
|
* @return string The InputOption name
|
||||||
*
|
*
|
||||||
* @throws InvalidArgumentException When option given does not exist
|
* @throws InvalidArgumentException When option given does not exist
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
*/
|
*/
|
||||||
private function shortcutToName($shortcut)
|
public function shortcutToName($shortcut)
|
||||||
{
|
{
|
||||||
if (!isset($this->shortcuts[$shortcut])) {
|
if (!isset($this->shortcuts[$shortcut])) {
|
||||||
throw new InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
|
throw new InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
|
||||||
|
@ -884,6 +884,19 @@ class ApplicationTest extends TestCase
|
|||||||
$this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if -n is passed');
|
$this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if -n is passed');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testRunWithGlobalOptionAndNoCommand()
|
||||||
|
{
|
||||||
|
$application = new Application();
|
||||||
|
$application->setAutoExit(false);
|
||||||
|
$application->setCatchExceptions(false);
|
||||||
|
$application->getDefinition()->addOption(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL));
|
||||||
|
|
||||||
|
$output = new StreamOutput(fopen('php://memory', 'w', false));
|
||||||
|
$input = new ArgvInput(['cli.php', '--foo', 'bar']);
|
||||||
|
|
||||||
|
$this->assertSame(0, $application->run($input, $output));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Issue #9285.
|
* Issue #9285.
|
||||||
*
|
*
|
||||||
|
@ -312,6 +312,14 @@ class ArgvInputTest extends TestCase
|
|||||||
|
|
||||||
$input = new ArgvInput(['cli.php', '-fbbar', 'foo']);
|
$input = new ArgvInput(['cli.php', '-fbbar', 'foo']);
|
||||||
$this->assertEquals('foo', $input->getFirstArgument(), '->getFirstArgument() returns the first argument from the raw input');
|
$this->assertEquals('foo', $input->getFirstArgument(), '->getFirstArgument() returns the first argument from the raw input');
|
||||||
|
|
||||||
|
$input = new ArgvInput(['cli.php', '--foo', 'fooval', 'bar']);
|
||||||
|
$input->bind(new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('arg')]));
|
||||||
|
$this->assertSame('bar', $input->getFirstArgument());
|
||||||
|
|
||||||
|
$input = new ArgvInput(['cli.php', '-bf', 'fooval', 'argval']);
|
||||||
|
$input->bind(new InputDefinition([new InputOption('bar', 'b', InputOption::VALUE_NONE), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('arg')]));
|
||||||
|
$this->assertSame('argval', $input->getFirstArgument());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testHasParameterOption()
|
public function testHasParameterOption()
|
||||||
|
Reference in New Issue
Block a user