How to Set Name to a custom CLI command

On a previous post we had the chance to see how we can create custom Magento CLI command. There are several ways to set a name to a CLI command. In that post we will go through these ways.

As you can see on our previous post about using profiler in a custom CLI command, we had set a name to our custom CLI command passing it as argument in our di.xml as you can see below.

This is one way to set a name. Another way is to add it directly in the constructor of our command class when calling the parent constructor. The parent constructor is the constructor of the class Symfony\Component\Console\Command\Command. This constructor has a string name as parameter. In case passing null means it must be set in the configure() function which is our 3rd way to set a name. We can call the function setName() inside the configure() function to set the command name. Below you can see both ways, using the command class of our older post as base.

Please do not forget that a CLI command should have an unique name.

Feel free to share this post and ask your questions in the comments below.

Till next time!

DigitalOcean Referral Badge