How to Create Custom Command in Console CLI in Magento 2

Written by Jigar Patel

Oct 06

How to Create Custom Command in Console CLI in Magento 2

How to Create Custom Command in Console CLI in Magento 2.

Magento 2, it is being provided default and you create your own custom console CLI command to manage actions, indexes, modules, etc. The new interface performs multiple tasks, including:

  • Installing Commerce (and related tasks such as creating or updating the database schema, creating the deployment configuration, and so on).
  • Clearing the cache.
  • Managing indexes, including reindexing.
  • Creating translation dictionaries and translation packages.
  • Generating non-existent classes such as factories and interceptors for plug-ins, generating the dependency injection configuration for the object manager.
  • Deploying static view files.
  • Creating CSS from Less.

Also Like Read:  Magento 2 Add Product to cart with a custom price

Custom console command in cli .Taken Dolphin as Package name and CustomCli as Modulename for simplicity. code snippets are under a module named Dolphin_CustomCli.

Before creating the customer tab, we need to create a new module We already have a blog on “how to create a basic module in Magento 2“. We need to create module.xml and registration.php file. You can create module.xml and registration.php  from this tutorial.

Step:1 Define Command in di.xml

File: app/code/Dolphin/CustomCli/etc/di.xml
you need to use Magento\Framework\Console\CommandList to define your command option.

<?xml version="1.0" ?>
<config xmlns:xsi=""
    <type name="Magento\Framework\Console\CommandList">
            <argument name="commands" xsi:type="array">
                <item name="Hellocustom" xsi:type="object">Dolphin\CustomCli\Console\Command\Hellocustom</item>

This di.xml declare a command class Hellocustom (Dolphin\CustomCli\Console\Command\Hellocustom). This class defines the command name and execute() method.

Step:2 Command Class Hellocustom.php

File: app/code/Dolphin/CustomCli/Console/Command/Hellocustom.php

namespace Dolphin\CustomCli\Console\Command;

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

class Hellocustom extends Command

    const NAME_ARGUMENT = "name";
    const NAME_OPTION = "option";

     * {@inheritdoc}
    protected function execute(
        InputInterface  $input,
        OutputInterface $output
        $name = $input->getArgument(self::NAME_ARGUMENT);
        $option = $input->getOption(self::NAME_OPTION);
        $output->writeln("Hello " . $name);

     * {@inheritdoc}
    protected function configure()
        $this->setDescription("custom cli command");
            new InputArgument(self::NAME_ARGUMENT, InputArgument::OPTIONAL, "Name"),
            new InputOption(self::NAME_OPTION, "-a", InputOption::VALUE_NONE, "Option functionality")
  • configure() method is used to set the name, description, command line arguments of the magento 2 add command line
  • execute() method will run when we call this command line via console.

Next, we need to check your new command is show in command list.

Magento 2 Below CLI command to see the list of all available Magento 2 commands.

[dt_highlight color=”” text_color=”” bg_color=””]php bin/magento list[/dt_highlight]

cli command

Now you can run [dt_highlight color=”” text_color=”” bg_color=””]php bin/magento dolphin_customcli:hellocustom[/dt_highlight] from the command to see the result:

cli command2

The CLI command by [dt_highlight color=”” text_color=”” bg_color=””]php bin/magento dolphin_customcli:hellocustom -a ‘jigar’[/dt_highlight] and it will show “Hello jigar” as an output.

cli command3

I hope this instruction will be helpful for you.

If you have any difficulties regarding this blog, do consider them posting in the Comments section below!

Thank you!

Written by Jigar Patel

Our Magento Store

Do not miss a chance to grab exciting offers on Magento Development at Dolphin Web Solution. We are providing discounts on various Magento services this season. Go and grab yours today at our Magento Store.

Multiple Wishlist for Magento 2

Multiple Wishlist for Magento 2

₹ 3106

Wallet and Reward Points for Magento 2

Wallet and Reward Points for Magento 2

₹ 9476

RMA for Magento 2

RMA for Magento 2

₹ 11865

₹ 14254
Abandoned Cart Email for Magento 2

Abandoned Cart Email for Magento 2

₹ 6291

Simple Details on Configurable Product for Magento 2

Simple Details on Configurable Product for Magento 2

₹ 7883

₹ 9476
Frequently Bought Together for Magento 2

Frequently Bought Together for Magento 2

₹ 5494

₹ 7087

Let's Start Your Project

Get free consultation for your digital product idea to turn it into reality!

Copyright © 2023 DOLPHIN WEB SOLUTION. All rights reserved.