beecubu / php-foundation-cli
Simple CLI Framework based on PHP Object Foundation.
v1.4.0
2024-07-14 15:49 UTC
Requires
- php: >=7.1.0
- beecubu/php-foundation-helpers: ^v1.10
Suggests
- readline: *
README
Simple CLI framework based on PHP Object Foundation. It provides:
Consoleutilities for colored output, input helpers, progress UI, and simple charts.- A tiny
Application+ViewControllerstack to build CLI screens/flows.
Requirements
- PHP 7.1+
beecubu/php-foundation-helpers(Composer dependency)- Optional:
ext-readlinefor nicer input handling
Installation
composer require beecubu/php-foundation-cli
Quick Start
<?php
use Beecubu\Foundation\Cli\Console;
Console::title('My CLI App');
Console::line('Hello <green>world</green>!');
Colors
Colors are expressed with simple tags:
<red>text</red>, <green>text</green>, <cyan>text</cyan>, <yellow>text</yellow>, <purple>text</purple>
Background tags:
<b_red>text</b_red>, <b_green>text</b_green>, <b_blue>text</b_blue>, ...
Console Helpers
Output
Console::line('A single line');
Console::lines(['Line 1', 'Line 2']);
Console::title("Main Title\nSubtitle");
Console::section("Section Title");
Console::center('Centered text');
Console::separator();
Console::error('Something went wrong', true);
Console::success('All good');
Console::warning('Be careful');
Console::clear();
Input
$name = Console::getString('Your name', false);
$age = Console::getIntValue('Age', 1, 120);
$price = Console::getValue('Price', 0, 9999, true, 9.99);
$ok = Console::getYesNo('Continue?');
$date = Console::getDate('Pick a date');
$dateTime = Console::getDateTime('Pick a date and time');
Options
// Returns array with exactly one true
$options = Console::getOption('Choose', 'a', 'b', 'c');
// Returns index (0-based)
$index = Console::getOptionIndex('Choose', 'a', 'b', 'c');
Progress UI
// Progress bar
Console::progressbar(100, true, 'Starting');
Console::progressbar(10, false, 'Working...');
Console::progressbar(90, false, 'Done');
// Spinner (call repeatedly in a loop)
Console::spinner('Loading...', 3);
Console::spinner(false); // clear
Simple Charts
$data = [
['value' => 0.25, 'color' => 'red'],
['value' => 0.35, 'color' => 'green'],
['value' => 0.40, 'color' => 'blue'],
];
Console::printChartPercentageBar($data);
Application + ViewController
This is a small view stack to build multi-screen CLI apps.
<?php
use Beecubu\Foundation\Cli\Application\Application;
use Beecubu\Foundation\Cli\Application\ViewController;
use Beecubu\Foundation\Cli\Console;
class MainView extends ViewController
{
private $done = false;
public function view(): void
{
Console::title('Main View');
$name = Console::getString('Your name', false);
Console::success("Hello $name");
$this->done = true;
}
public function finished(): bool
{
return $this->done;
}
}
new Application(new MainView());
Navigation
Inside any ViewController you can:
$this->application->pushView($anotherView)to open a new view.$this->application->back()to return to the previous view.$this->application->home()to return to the first view.
When a view reports finished() === true, the application will go back automatically (or exit if it was the only view).