This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

Add Custom CLI Commands In Magento 2

Updated on  12th February

4 Min Read
Reading Time: 4 minutes

The new command line interface (CLI) enables Magento developers to use default command provided by Magento modules. Using this, developers could manage modules, carry our database updates, manage indexes and generate classes.

Magento 2 Custom CLI

The great thing is that Magento developers could now add custom CLI commands to their modules. While this is great, developers should always make sure that they observe the best code writing standards as defined by the Magento dev community.

Add a CLI Command

The process of adding a new CLI command is simple. In this tutorial, I will not go into the details of creating a Magento 2 module.

Just create a simple module Cloudways_Commandline with etc/module.xml and registration.php files included in the root of module’s directory.

Cloudways Magento Hosting For Developers

Now, create a di.xml file in the etc directory of your module. Insert the following code in the file:

As you can see, I have added an entry in di.xml file. The <type> tag is pointing to Magento\Framework\Console\CommandList class. Furthermore, it has the <argument> named commands, and the <item>  tag. It seems like Magento 2 is doing some magic by sending the argument to CommandList class. That is how dependency injection works – injecting the Command object (defined in the  <item> tag) into an array of all commands.

I have also declared a class Cloudways\Commandline\Console\Hicustomer in the <item> tag. This class will set the name, and description of command using the configure() method. The class will also define the execute() method for the command.

The next step is the creation of the class file Hicustomer.php (as defined in di.xml), in the Console directory of the module and add the following code to it:

The Hicustomer class has two methods. The configure() method is used to set initial configuration (such as name, description, and arguments) for the command. The execute() method will contain the logic that would be executed when the CLI command is called in the console.

Once the class has been declared, enable the module, run the setup upgrade command and flush Magento 2 cache. Now, type the following CLI command to see the list of all available Magento 2 commands.

You will notice that the custom CLI command is also on the list.

Now, run the php bin/magento cloudways:hicustomer command to see the desired result.

This was just a basic CLI command. To make things interesting, I will now demonstrate how to add an argument to the custom command. For this, replace the current contents of the class file Hicustomer.php with the following.

As you can see, I have created and added the Name argument for custom command in configure() method using  $this->setDefinition($commandoptions); function and get it in execute() method.

Once again, run setup upgrade command and flush Magento 2 cache. Invoke the custom CLI command by php bin/magento cloudways:hicustomer --Name="Fayyaz" and it will show “Hi Fayyaz” as an output.

Final Words!

In this tutorial, I have covered the basics of Magento 2 CLI commands and explored the best way of adding a new CLI Command in Magento 2. I hope you have found this tutorial useful. If you have any feedback, feel free to share it in the comments section below.

Share your opinion in the comment section. COMMENT NOW

Fayyaz Khattak

Fayyaz is a Magento Community Manager at Cloudways - A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. Fayyaz is a food lover and enjoys driving. You can email him at m.fayyaz@cloudways.com

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.