What you should use to write a CLI app
Published 4 years and 2 months ago, Categorized under: Code, Linux, PHP

I was going to write a post about this, but then I found out that I don't have a big plan for what am going to write, so instead, I prefer that you go and read this article:  Re-Introducing Symfony Console – CLI PHP for the Uninitiated!  its not exactly what I wanted to write, but why to re-write things when someone already did that.


As a web developer, we are always fascinated with what we can do for the web, we have the best technologies ( not including old IE version ), we have some limitation, but now we can also use web technologies to develop Desktop apps. So talking about CLI apps may not be the best topic here, but sometimes its way better to use CLI(https://en.wikipedia.org/wiki/Command-line_interface) to develop an application than using the normal route with nginx/apache, why you may need to do that? the reasons are unlimited, the main one is your judgement.

First of all developing CLI(https://en.wikipedia.org/wiki/Command-line_interface) application with PHP does not differ that much from developing Web app, you will use the same code, but your user will interact with the app via Terminal not Visual UI, this can be helpful when you are doing something like processing big files,  CSV files, or maybe doing some maintenance. You will need to care about the structure of your app, and how you code, at the end you don't want to spend a lot of time trying to debug it and even losing control and make it way complicated than it should. so lets start to show you how I do it, then you may use the same or even better technique to develop your own.

Required Components:

There are a lot of libraries which we can utilize to develop the code, as with web you are not limited to specific framework or library, you can create your CLI(https://en.wikipedia.org/wiki/Command-line_interface) with nodejs or even better with PHP ;) , for me I depend on PHP so I'll list all the libraries that I use here:

  1. Symfony Console{style="text-decoration: underline;"} : http://symfony.com/doc/current/components/console.html
  2. Symfony Process{style="text-decoration: underline;"} : http://symfony.com/doc/current/components/process.html

Symfony Console is the main component here, which will help us to build a Command Line Application (also known as CLI).

Share It via: