From 002f9d5c2afbf7ced94482b3413e016c7e7044c6 Mon Sep 17 00:00:00 2001 From: alexander <“sanyaerin@mail.ru”> Date: Fri, 23 Jun 2023 13:17:39 +0300 Subject: [PATCH] chore --- src/Console/CliCommand.php | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Console/CliCommand.php b/src/Console/CliCommand.php index 7fbe900..d86bb1e 100644 --- a/src/Console/CliCommand.php +++ b/src/Console/CliCommand.php @@ -4,6 +4,7 @@ namespace IWD\Symfony\PresentationBundle\Console; +use IWD\Symfony\PresentationBundle\Exception\ValidatorException; use IWD\Symfony\PresentationBundle\Interfaces\InputContractInterface; use IWD\Symfony\PresentationBundle\Service\CliContractResolver; use ReflectionClass; @@ -12,6 +13,7 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Throwable; abstract class CliCommand extends Command { @@ -89,8 +91,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); - /** @var InputContractInterface $inputContract */ - $inputContract = $this->cliContractResolver->resolve($input, static::getInputContractClass()); + try { + /** @var InputContractInterface $inputContract */ + $inputContract = $this->cliContractResolver->resolve($input, static::getInputContractClass()); + } catch (ValidatorException $exception) { + $violations = json_decode($exception->getMessage(), true, 512, JSON_THROW_ON_ERROR); + $message = 'Command options has violations:' . PHP_EOL; + + $i = 0; + foreach ($violations as $property => $violation) { + ++$i; + $message .= sprintf( + '%s. %s: %s', + $i, $property, $violation + ) . PHP_EOL; + } + $io->error($message); + + return self::FAILURE; + } return $this->handle( io: $io,