Skip to content

Commit

Permalink
[Console] Make error message more verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm authored and fabpot committed Nov 1, 2020
1 parent f372360 commit 5164210
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
18 changes: 16 additions & 2 deletions Input/ArgvInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,25 @@ private function parseArgument(string $token)
// unexpected argument
} else {
$all = $this->definition->getArguments();
$symfonyCommandName = null;
if (($inputArgument = $all[$key = array_key_first($all)] ?? null) && 'command' === $inputArgument->getName()) {
$symfonyCommandName = $this->arguments['command'] ?? null;
unset($all[$key]);
}

if (\count($all)) {
throw new RuntimeException(sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($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(sprintf('No arguments expected, got "%s".', $token));
throw new RuntimeException($message);
}
}

Expand Down
10 changes: 10 additions & 0 deletions Tests/Input/ArgvInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ public function provideInvalidInput()
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".',
],
];
}

Expand Down

0 comments on commit 5164210

Please sign in to comment.