Aplus Framework Docs

CLI

Aplus Framework CLI Library

Aplus Framework CLI (Command-Line Interface) Library.

Installation

The installation of this library can be done with Composer:

composer require aplus/cli

Running

Create a file (cli.php) with the following contents:

<?php
require __DIR__ . '/vendor/autoload.php';

use Framework\CLI\Console;

$console = new Console();
$console->run(); // void

Go to the terminal an run:

php cli.php

The output will be like the image below:

Aplus CLI - Index Command

Add a Custom Language

Edit the PHP file:

use Framework\CLI\Console;
use Framework\Language\Language;

$language = new Language('pt-br');

$console = new Console($language);
$console->run(); // void

Run the file in the terminal. The output will be like the following:

Aplus CLI - Index Command with Custom Language

If the CLI Library is not localized in your language, you can contribute by adding it with a Pull Request in the package repository.

It is also possible to add custom languages at runtime. See the Language Library to know more.

Add a Custom Command

Now, let's add your first Command.

Edit the PHP file:

use Framework\CLI\CLI;
use Framework\CLI\Command;
use Framework\CLI\Console;

class HelloCommand extends Command
{
    public function run() : void
    {
        CLI::write('Hello, Aplus!');
    }
}

$console = new Console();
$console->addCommand(HelloCommand::class); // static
$console->run(); // void

Go to the terminal and run:

php cli.php

Note that hello is listed as an available command:

Aplus CLI - Index Command with Custom Command

Run the hello command:

php cli.php hello

The output will be like this:

Aplus CLI - Run a Hello Command

Conclusion

Aplus CLI Library is an easy-to-use tool for PHP developers, beginners and experienced.
It is perfect for building simple and full-featured command-line interfaces.
The more you use it, the more you will learn.

Did you find something wrong?
Be sure to let us know about it with an issue.
Thank you!

Search results