All posts
Published in PHP

Say Goodbye to Boring Command Lines: Create Powerful and Fun Tools with PHP

Profile image of Atakan Demircioğlu
By Atakan Demircioğlu
Fullstack Developer

This is the guide to create powerful CLI tools with PHP from skratch.

Say Goodbye to Boring Command Lines: Create Powerful and Fun Tools with PHP

Say Goodbye to Boring Command Lines: Create Powerful and Fun Tools with PHP image 1

Command Line Interface (CLI) tools have been gaining popularity lately due to their speed and efficiency. CLI tools that can automate tasks, simplify workflows, enhance your productivity and etc.

If you’re a beginner or an experienced developer, this guide will provide you with the tools and knowledge to create CLI tools that will revolutionize your workflow and help you achieve your coding goals.

So, let’s get started and unleash the full potential of PHP on the command line.

Table Of Contents:

· What are the project requirements and packages?
· Start Codding
 ∘ Creating Folder Structure
 ∘ Creating Commands
 ∘ Giving permissions to file
 ∘ Testing commands manually

What are the project requirements and packages?

Honestly, writing everything from scratch is not worth it. I researched some packages and decided to use Symfony’s Console package.

I loved the usage, and some features like autocomplete, help part, and colors.

So let’s start coding.

Start Codding

Firstly create a new directory under your repositories. If you are not using Git or you don’t have a workspace folder, I really recommend you create a new one :)

Also for great terminal features, I recommend you to try Warp Terminal. I have a short article about Warp. Here you can read the article.

mkdir php-cli-app-example cd php-cli-app-example composer init

After answering the questions you will have an initial setup of a PHP package.

Now let’s install the Symfony Console package

composer require symfony/console

After that under the /src folder run this command

Creating Folder Structure

mkdir Commands && mkdir Exceptions && mkdir Helpers

This will be our folder structure, actually, you are free to create your own folder structure.

After that under the /src folder, create the ExampleCLI.php file.

touch ExampleCLI.php

Then open the file in your code editor or with Vim and etc.

Be careful about the namespace, it must match your composer.json file.

Creating Commands

After that open a new file under Commands named ExampleCommand.php.

In this class, we are inheriting the Command class from the Symfony Console package and we are modifying it.

$defaultName is for your command name in the CLI application. You can modify the description of your command and you can add arguments or options easily.

Also with some powerful features of this package, you can draw tables in CLI.

Say Goodbye to Boring Command Lines: Create Powerful and Fun Tools with PHP image 2

After that for testing the application, go to the main directory of you application and create a file named as cli with no extensions.

Giving permissions to file

Give permissions to run it;

chmod +x cli

Then run;

./cli

You will see a screen like that. And you can see your new command example in the last part of this screenshot.

Say Goodbye to Boring Command Lines: Create Powerful and Fun Tools with PHP image 3

Then type something wrong to just test the features of Symfony Console.

Testing commands manually

Say Goodbye to Boring Command Lines: Create Powerful and Fun Tools with PHP image 4

./cli helloos

It will respond to you with hellooss is not defined.

Also if you type a similar command to match any command, the tool is warning you.

Say Goodbye to Boring Command Lines: Create Powerful and Fun Tools with PHP image 5

Ok, lets run the correct command and see what is happening.

./cli example:songs first

 

Say Goodbye to Boring Command Lines: Create Powerful and Fun Tools with PHP image 6

Yay, this was the example of creating powerful CLI tools with the Symfony Console package.

Here is the source code of this article.

Twitter: https://twitter.com/atakde

Github: https://github.com/atakde

If you like to get more updates from me, 
please follow me on Medium and subscribe to email alert.