Building elegant console applications with MiniCli

Nelson Isioma
4 min readMay 23, 2023

--

Minicli logo
Credits: minicli/minicli github

When you are building console applications, languages that come to mind include go, c# or python. Certainly not php, I am here to make a case for using php to build your next CLI using Minicli. It is a dependency-free framework(which makes it lightweight😀) and very fun to use.

In this tutorial, I will be a building a simple CLI called ‘greet’, that returns greetings according to our computer’s local time

Creating a New Application

To create a new minicli app, the only requirement is PHP 8.1+, to check your php version run:

php --version

// your output should look like
PHP 8.1.10 (cli) (built: Aug 30 2022 18:05:49) (ZTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.1.10, Copyright (c) Zend Technologies

Anything from PHP 8.1.0+ is perfect. Next, we install minicli using composer:

composer create-project --prefer-dist minicli/application greet-cli

Don’t forget to replace ‘greet-cli’ with the name of your CLI. Now cd into your new minicli app and you might be bamboozled with all the files and folders. But don’t worry I will explain the important stuff next.

Directory Structure

If you have been following the tutorial so far, your app directory will look like this:

.
├── app
│ └── Command
│ └── Demo
│ ├── DefaultController.php
│ ├── TableController.php
│ └── TestController.php
├── tests
│ ├── Feature
│ │ └── Command
│ │ └── DemoCommandTest.php
│ ├── Helpers.php
│ └── Pest.php
├── vendor/
├── composer.json
├── composer.lock
├── LICENSE
├── minicli
├── phpunit.xml
└── README.md

The first thing we will talk about is the minicli file, that’s the script that starts up our console application. to try it out run:

./minicli help

// This lists out all the available commands in your app:
Available Commands

demo
└──table
└──test

help

but what I do is rename this file from ‘minicli’ to the name of my app, which in this case is greet-cli. So in your terminal run:

mv minicli greet-cli

// so now we can run:
./greet-cli help

// and get all the available commands for our greet cli

Creating our first command

All commands are in the command folder. Let’s create a new command called ‘greet’. When called it will greet us depending on the local time. First we create a new greet folder under the Command directory by running:

mkdir app/Command/greet

Next we create a DefaultController class under our new greet folder like:

touch app/Command/greet/DefaultController.php

The DefaultController is what minicli looks for anytime the greet command is called. Now let’s add the following to our DefaultController:

<?php

namespace App\Command\greet;

class DefaultController extends \Minicli\Command\CommandController
{
public function handle(): void
{
$this->display("greet command called!");
}
}

The handle method contains the main function run by the DefaultController. Now let’s run this command

./greet-cli greet

// response should be
greet command called!

If that was not your response, check the previous steps to see what might have gone wrong. Otherwise, Well done! now let’s add functionality to our command.

So our goal is that greet-cli checks the current local time on our machine and replies with either “Good morning” or “Good afternoon” or “Good evening”. Let’s add the following code to our handle method

 public function handle(): void
{
$currentHour = date('H');

if ($currentHour < 12) {
$this->display("Good morning!");
return;
}
if ($currentHour < 16) {
$this->display("Good Afternoon!");
return;
}
$this->display("Good evening");
}

Now when we run the greet command it will return a fancy greeting😉

Adding Flags and Parameters

Minicli makes it very easy to add flags to your commands. Let’s add a flag “ — lang” that changes the greetings from English to Spanish. Our handle method will now look like this:

public function handle(): void
{
$currentHour = date('H');

if ($this->hasFlag('lang')) {
if ($currentHour < 12) {
$this->display("buen día!");
return;
}
if ($currentHour < 16) {
$this->display("buenas tardes!");
return;
}
$this->display("buenas noches");
} else {
if ($currentHour < 12) {
$this->display("Good morning!");
return;
}
if ($currentHour < 16) {
$this->display("Good Afternoon!");
return;
}
$this->display("Good evening");
}
}

Now run:

./greet-cli greet --lang

// this will return a greeting in spanish

Now let’s add a name parameter so it greets us with our name. Our handle method now looks like this:

 public function handle(): void
{
$currentHour = date('H');
$name = "";

if ($this->hasParam("name")) {
$name = $this->getParam("name");
}

if ($this->hasFlag('lang')) {
if ($currentHour < 12) {
$this->display("buen día $name!");
return;
}
if ($currentHour < 16) {
$this->display("buenas tardes $name!");
return;
}
$this->display("buenas noches $name!");
} else {
if ($currentHour < 12) {
$this->display("Good morning $name!");
return;
}
if ($currentHour < 16) {
$this->display("Good Afternoon $name!");
return;
}
$this->display("Good evening $name!");
}
}

The ‘hasParam’ method checks for the parameter and ‘getParam’ retrieves the parameter. So now we can run:

./greet-cli greet name=Nelson

// and it will greet you with your name

Summary

This is a simple starting point of getting started with minicli. There is still so much you can add to your application. The docs has all you need.

I can’t wait to see what you will build with minicli. Thank you for reading

--

--

Nelson Isioma

Web developer specialized in building backend systems, then I play with Vue from time to time