0

I know how to create custom CLI commands with input parameters like: bin/magento customers:syncbyid --id=69, but I'd like to know how (if even possible) I can create a command with an option without a name like this: bin/magento customers:syncbyid 69. This would be very useful because this specific command would only ever require 1 parameter. Typing the parameter name with each use would be wasted time.

The documentation on commands is very scarce, so if the possibility even exists, I'd have no idea about that. I've tried setting the option name to an empty string, but this throws the error "An option name cannot be empty".

$this->addOption(
    'id',
    null,
    InputOption::VALUE_REQUIRED,
    'ID'
);

1 Answer 1

1

You can use Input Argument instead of Input Option

For example:

<?php
namespace Vendor\Module\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ImportStore extends Command
{
    const INPUT_KEY_TEST = 'test';

  
    protected function configure()
    {
        $this->setName('test:test');
        $this->setDescription('Your desc');
        $this->addArgument(
            self::INPUT_KEY_TEST,
            InputArgument::REQUIRED,
            'description'
        );

        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $testValue = $input->getArgument(self::INPUT_KEY_TEST);

    }

}
1
  • Thanks, this is exactly what I was looking for. I'm surprised at how badly CLI commands are documented. Apr 29, 2022 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.