CLI

API Documentation

Build Coverage

Usage Example

To create a command, you must extend the abstract Command class. The only method mandatory is run, where the logic of the command will be run.

In a file called boot.php we create the Hello command, adding the following content:

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

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

class Hello extends Command
{
	protected string $name = 'hello';
	protected string $description = 'Say hello';

	public function run() : void
	{
		$name = $this->console->getArgument(0);
		if ($name) {
			$name = ', ' . $name;
		}
		CLI::write('Hello' . $name . '!');
	}
}

And, below, we instantiate the Console and add the Hello command:

$console = new Console();
$console->addCommand(new Hello($console));
$console->run();

After that, we can run the boot.php file:

php boot.php
 _____                                            _
|  ___| __ __ _ _ __ ___   _____      _____  _ __| | __
| |_ | '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
|  _|| | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
|_|  |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\

Saturday, July 25, 2020 - 16:32:57 - America/Sao_Paulo

Commands:
  hello  Say hello
  help   Shows command usage help.
  index  Shows commands list.

Note that the screen shows us a header and a list of all available commands. To call a command, just add its name as the first argument:

php boot.php hello

And the output:

Hello!

Adding an argument:

php boot.php hello Natan

And the output shows the name.

Hello, Natan!

Through this practical example you can learn a little about the CLI library.

More usage examples can be found at tests.