Magento 2 - Add Arguments to Command Line

I'm creating an article to add arguments, first of all you need to create custom command line if you want to follow this article.

Define Arguments

Once you already have custom command line, what you need to do is configuring option(s) onto configure method.

<?php

namespace Fiko\Magento\Console;

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

class Welcome extends Command
{
    const NAME = 'name';

    protected function configure()
    {
        $options = [
            new InputOption(
                self::NAME,
                '-a',
                InputOption::VALUE_OPTIONAL,
                'Your Name'
            ),
        ];

        $this->setName('fiko:welcome');
        $this->setDescription('Demo description of welcoming someone!');
        $this->setDefinition($options);

        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        if ($name = $input->getOption(self::NAME)) {
            $output->writeln("Welcome Home, {$name}!");
        } else {
            $output->writeln("Welcome Home!");
        }
    }
}

Then you can try to execute it!

bin/magento fiko:welcome -aBorizqy
OR
bin/magento fiko:welcome --name=Borizqy

of if you want to read the help or documentation, you can try

bin/magento fiko:welcome -h

Code Breakdown

Required Class

Symfony\Component\Console\Input\InputOption is required class to create the argument.

.....
use Symfony\Component\Console\Input\InputOption;
.....

Prepare Option(s)

    .....
    const NAME = 'name';
        .....
        $options = [
            new InputOption(
                self::NAME,
                '-a',
                InputOption::VALUE_OPTIONAL,
                'Your Name'
            ),
        ];
        .....
  • const NAME = 'name'; is optional, I recommend to use it so we only need to call self::NAME, and if we want to change it, just the the value of this content without we need to change multiple lines.

  • $options = [ it's array of the options, you can define more than one option, in order to do that just define multiple InputOption inside this array.

        .....
        $options = [
            new InputOption(
                self::NAME,
                '-a',
                InputOption::VALUE_OPTIONAL,
                'Your Name'
            ),
            new InputOption(
                self::SURNAME,
                '-s',
                InputOption::VALUE_OPTIONAL,
                'Your Surname'
            ),
            .....
        ];
        .....
  • self::NAME name of the option / argument

  • '-a', abbreviation of the argument, in case calling --name Borizqy is too long, we can use -aBorizqy or -a Borizqy

  • InputOption::VALUE_OPTIONAL is the type of the argument, at the moment there are

    • InputOption::VALUE_OPTIONAL The option may or may not have a value (e.g. --yell or --yell=loud), or we can ignore this type of argument.

    • InputOption::VALUE_REQUIRED A value must be passed when the option is used (e.g. --iterations=5 or -i5).

    • InputOption::VALUE_NONE Do not accept input for the option (e.g. --yell). This is the default behavior of options.

    • InputOption::VALUE_IS_ARRAY The option accepts multiple values (e.g. --dir=/foo --dir=/bar).

  • 'Your Name' is Description of the argument

Define Option(s)

Once options already prepared, then we need to define or register them to magento by adding setDefinition inside the configure method

    .....
    protected function configure()
    {
        .....
        $this->setDefinition($options);
        .....
    }
    .....

Fetching or Calling Option(s)

to fetch the argument we pass to the command line, use this line of code.

.....
$input->getOption(self::NAME)
.....

Thanks :)

Did you find this article valuable?

Support Fiko Borizqy by becoming a sponsor. Any amount is appreciated!