sebastianfeldmann/cli
Lightweight PHP library for building CLI tools: defines commands and options, parses argv, validates input, and renders help/usage output. Clean API for composing console applications with consistent argument handling and exit codes.
Begin by installing the package via Composer:
composer require sebastianfeldmann/cli
The core classes are Cli (entry point), Command, and Argument. Start with a minimal CLI app:
use SebastianFeldmann\Cli\Cli;
use SebastianFeldmann\Cli\Command;
require_once 'vendor/autoload.php';
$cli = new Cli('my-tool', '1.0.0');
$cli->addCommand(new Command(
name: 'greet',
description: 'Say hello',
arguments: [
new \SebastianFeldmann\Cli\Argument\Required('name', 'Who to greet'),
],
options: [
new \SebastianFeldmann\Cli\Option\Flag('uppercase', 'Uppercase output'),
],
callback: static function (Command $cmd): int {
$name = $cmd->getArgument('name')->getValue();
$msg = "Hello, {$name}!";
if ($cmd->getOption('uppercase')->isSet()) {
$msg = strtoupper($msg);
}
$cmd->getOutput()->writeln($msg);
return Command::SUCCESS;
}
));
exit($cli->run());
First use case: scaffold a CLI tool for project-specific automation (e.g., bin/my-tool generate-report).
CommandInterface or extend AbstractCommand) for better testability and organization.Argument and Option validators (e.g., ->withValidator(Validator::regex('/^[a-z]+$/'))) for early feedback.Output helper methods like writeln(), writeBlock() (for styled boxes), and progressBar() (for long tasks).Cli::addCommands([...]) or group by feature (e.g., db:*, cache:*).Input, Output, and Command — the API is designed for unit testing without CLI dependencies.require 'phar://app.phar/vendor/autoload.php').Cli throws UnknownCommandException; handle gracefully with $cli->onUnknownCommand(fn($cmdName) => ...) or setCatchExceptions(true).--snake-case by default; use Option::withName('camelCase') to override (not recommended for user-facing CLIs).Command::SUCCESS (0) or Command::FAILURE (1) explicitly — but other codes like Command::INVALID (65) are also supported.stream_isatty(STDOUT) manually — the library doesn’t auto-toggle verbosity.OutputInterface to customize formatting (e.g., JSON output for machine consumption).How can I help you explore Laravel packages today?