---
title: "How to Create Custom Command in Console CLI in Magento 2"
url: "https://dolphinwebsolution.com/create-custom-command-in-console-cli-in-magento-2"
date: "2021-10-06T05:37:06+05:30"
modified: "2023-08-25T09:35:08+05:30"
author:
  name: "Jigar Patel"
categories:
  - "Magento"
word_count: 584
reading_time: "3 min read"
summary: "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..."
description: "In this article, Today I will instruct on How to Create custom Command in Console CLI in Magento 2. It is being provided default and you create your own cust..."
keywords: "Create Custom Command in Console,Create Custom Command in Console in magento 2,Create Command in Console,Create Custom Command in Console cli,Create Custom Command in Console cli in magento 2,Magento 2 Create Custom Command in Console, Magento"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Create Child Theme in Magento 2"
    url: "https://dolphinwebsolution.com/how-to-create-child-theme-in-magento-2"
  - title: "How to Display swatches instead of labeled product options on Checkout in Magento 2"
    url: "https://dolphinwebsolution.com/how-to-display-swatches-instead-of-labeled-product-options-on-checkout-in-magento-2"
  - title: "How to Create Sales Order Programmatically in Magento 2?"
    url: "https://dolphinwebsolution.com/create-sales-order-programmatically-in-magento-2"
---

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

_Published: October 6, 2021_  
_Author: Jigar Patel_  

![](https://dolphinwebsolution.com/wp-content/uploads/2023/02/How-to-Create-Custom-Command-in-Console-CLI-in-Magento-2-1024x536.png)

**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](https://dolphinwebsolution.com/magento-2-add-product-to-cart-with-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](https://dolphinwebsolution.com/how-to-create-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="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Console\CommandList">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="Hellocustom" xsi:type="object">Dolphin\CustomCli\Console\Command\Hellocustom</item>
            </argument>
        </arguments>
    </type>
</config>
```

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

```
<?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->setName("dolphin_customcli:hellocustom");
        $this->setDescription("custom cli command");
        $this->setDefinition([
            new InputArgument(self::NAME_ARGUMENT, InputArgument::OPTIONAL, "Name"),
            new InputOption(self::NAME_OPTION, "-a", InputOption::VALUE_NONE, "Option functionality")
        ]);
        parent::configure();
    }
}
```

- **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\]

![AI Integration in Magento: Benefits, Use Cases and Business Impact](https://dolphinwebsolution.com/wp-content/uploads/2021/10/cli-command-1024x302.png)

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:

![AI Integration in Magento: Benefits, Use Cases and Business Impact](https://dolphinwebsolution.com/wp-content/uploads/2021/10/cli-command2-1024x58.png)

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.

![AI Integration in Magento: Benefits, Use Cases and Business Impact](https://dolphinwebsolution.com/wp-content/uploads/2021/10/cli-command3-1024x49.png)

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!**


---

_View the original post at: [https://dolphinwebsolution.com/create-custom-command-in-console-cli-in-magento-2](https://dolphinwebsolution.com/create-custom-command-in-console-cli-in-magento-2)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-26 06:53:25 UTC_  
