/**
* {@inheritdoc}
*/
public function resolveCommand(RawArgs $args, Application $application)
{
$tokens = $args->getTokens();
$namedCommands = $application->getNamedCommands();
$argumentsToTest = $this->getArgumentsToTest($tokens);
$optionsToTest = $this->getOptionsToTest($tokens);
// Try to find a command for the passed arguments and options.
if ($result = $this->processArguments($args, $namedCommands, $argumentsToTest, $optionsToTest)) {
return $this->createResolvedCommand($result);
}
// If arguments were passed, we did not find the matching command.
if ($argumentsToTest) {
throw CannotResolveCommandException::nameNotFound(reset($argumentsToTest), $namedCommands);
}
// If no arguments were passed, run the application's default command.
if ($result = $this->processDefaultCommands($args, $application->getDefaultCommands())) {
return $this->createResolvedCommand($result);
}
// No default command is configured.
throw CannotResolveCommandException::noDefaultCommand();
}