Shopping cart

How to Add Command line in to Console CLI in Magento 2

In this article, we will find how to add a command line into magento 2 console CLI. Magento 2 add command line use a interface to quick change some features like:

Before we start, please take some minutes to know about the naming in Magento 2 CLI.

We will use an example module Mageplaza_HelloWorld to demo for this lesson. To add an option to Magento 2 CLI, we will follow by some steps:

Step 1: Define command in di.xml

In di.xml file, you can use a type with name Magento\Framework\Console\CommandList to define the command option.

File: app/code/Mageplaza/HelloWorld/etc/di.xml

<?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="exampleSayHello" xsi:type="object">Mageplaza\HelloWorld\Console\Sayhello</item>
           </argument>
       </arguments>
   </type>
</config>

This config will declare a command class Sayhello. This class will define the command name and execute() method for this command.

Step 2: Create command class

As define in di.xml, we will create a command class:

File: app/code/Mageplaza/HelloWorld/Console/Sayhello.php

<?php
namespace Mageplaza\HelloWorld\Console;

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

class Sayhello extends Command
{
   protected function configure()
   {
       $this->setName('example:sayhello');
       $this->setDescription('Demo command line');
       
       parent::configure();
   }
   protected function execute(InputInterface $input, OutputInterface $output)
   {
       $output->writeln("Hello World");
   }
}

In this function, we will define 2 methods:

  • 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.

After declare this class, please flush Magento cache and type this command:

php magento --list

You will see the list of all commands. Our command will be show here

magento 2 add command line

Now you can run bin/magento example:sayhello from the command to see the result

magento 2 add command line

Now, we will add the arguments for the command.

Content would be:

<?php

namespace Mageplaza\HelloWorld\Console;

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

class Sayhello extends Command
{

	const NAME = 'name';

	protected function configure()
	{

		$options = [
			new InputOption(
				self::NAME,
				null,
				InputOption::VALUE_REQUIRED,
				'Name'
			)
		];

		$this->setName('example:sayhello')
			->setDescription('Demo command line')
			->setDefinition($options);

		parent::configure();
	}

	protected function execute(InputInterface $input, OutputInterface $output)
	{
		if ($name = $input->getOption(self::NAME)) {

			$output->writeln("Hello " . $name);


		} else {

			$output->writeln("Hello World");

		}

		return $this;

	}
}

We defined name argument for command line in configure() function and get it in execute() function. Please clear cache and run php bin/magento example:sayhello --name="Join" from the command line to check the result. It will show “Hello Join”





Enjoyed the tutorial? Spread it to your friends!

magento-2-module-development
hello-world
command
line
cli
console
featured

Jacker Ngo
Jacker Ngo
Magento Developer who is passionate about development and love technology. Jacker's story

Comments for How to Add Command line in to Console CLI in Magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

We recommend:

People also searched for

  • magento 2 add command line
  • magento 2 console
  • magento 2 command line
  • console in magento 2
  • cli in magento 2
  • command line in magneto 2
  • magento 2 create command line
  • magento 2 create module command line
  • magento 2 command line backup
  • magento 2 command line clear cache
  • magento 2 command line cron
  • magento 2 command line disable compilation
  • magento 2 command line export
  • magento 2 command line flush cache
  • magento 2 command line install
  • magento 2 command line indexer
  • magento 2 command line import
  • magento 2 command line interface
  • magento 2 command line reindex
  • magento 2 command line reindex all
  • magento 2 command line refresh cache
  • magento 2 command line script
  • magento 2 command line tool
  • magento 2 command line upgrade
  • 2.1.x, 2.2.x, 2.3.x