feature #38919 [Console] Make error message more verbose (Nyholm)

This PR was squashed before being merged into the 5.2-dev branch.

Discussion
----------

[Console] Make error message more verbose

| Q             | A
| ------------- | ---
| Branch?       | 5.x
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #38904
| License       | MIT
| Doc PR        | no

This will make the error message a bit more verbose when you are using too many or too few arguments.

Commits
-------

ef221e88aa [Console] Make error message more verbose
This commit is contained in:
Fabien Potencier 2020-11-01 18:40:12 +01:00
commit f5bd922e5b
2 changed files with 27 additions and 3 deletions

View File

@ -165,11 +165,25 @@ class ArgvInput extends Input
// unexpected argument
} else {
$all = $this->definition->getArguments();
if (\count($all)) {
throw new RuntimeException(sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all))));
$symfonyCommandName = null;
if (($inputArgument = $all[$key = array_key_first($all)] ?? null) && 'command' === $inputArgument->getName()) {
$symfonyCommandName = $this->arguments['command'] ?? null;
unset($all[$key]);
}
throw new RuntimeException(sprintf('No arguments expected, got "%s".', $token));
if (\count($all)) {
if ($symfonyCommandName) {
$message = sprintf('Too many arguments to "%s" command, expected arguments "%s".', $symfonyCommandName, implode('" "', array_keys($all)));
} else {
$message = sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all)));
}
} elseif ($symfonyCommandName) {
$message = sprintf('No arguments expected for "%s" command, got "%s".', $symfonyCommandName, $token);
} else {
$message = sprintf('No arguments expected, got "%s".', $token);
}
throw new RuntimeException($message);
}
}

View File

@ -247,6 +247,16 @@ class ArgvInputTest extends TestCase
new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_NONE)]),
'The "-Щ" option does not exist.',
],
[
['cli.php', 'acme:foo', 'bar'],
new InputDefinition([new InputArgument('command', InputArgument::REQUIRED)]),
'No arguments expected for "acme:foo" command, got "bar"',
],
[
['cli.php', 'acme:foo', 'bar'],
new InputDefinition([new InputArgument('name', InputArgument::REQUIRED)]),
'Too many arguments, expected arguments "name".',
],
];
}