Symfony 2: check console console arguments

I create a command to create accounts from a file. In the team, I passed a few arguments.

$this ->setName('batch:create') ->setDescription('xyz') ->setHelp('xyz') ->addArgument('account-id', InputArgument::REQUIRED, "Set the account id.") ->addArgument('name', InputArgument::REQUIRED, "Set the account name."); 

I was just thinking if there is a way to check the type of argument passed. While I check it out,

  if (is_numeric($input->getArgument('account-id'))) { // .... } 

In any case, I can create a validator that checks the type, and I just need to call the validation function.

  if ($input->validate() === false) { // show error message and return. } 
+6
source share
1 answer

Unfortunately, there is currently no way to implement command argument validation in Symfony. The best way to implement these checks is to override the Symfony\Component\Console\Command::initialize method in your command, and then apply the validation rules there, throwing exceptions if the arguments passed are invalid.

Update: Matthias Novak implemented symfony-console-form ( https://github.com/matthiasnoback/symfony-console-form ) and looks like this: the interface Matthias\SymfonyConsoleForm\Console\Command\FormBasedCommand will give you basic validation options through the form component ( you need to check it with a check).

+9
source

Source: https://habr.com/ru/post/978271/


All Articles