ez-php / console
Lightweight console infrastructure for PHP — command dispatcher, argument parser, and colored output helpers
1.8.0
2026-04-19 10:18 UTC
Requires
- php: ^8.5
Requires (Dev)
- ez-php/docker: ^1.0
- ez-php/testing-application: ^1.0
- friendsofphp/php-cs-fixer: ^3.94
- phpstan/phpstan: ^2.1
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^13.0
README
Lightweight console infrastructure for PHP — command dispatcher, argument parser, and colored output helpers.
Installation
composer require ez-php/console
Usage
Defining a command
use EzPhp\Console\CommandInterface; use EzPhp\Console\Input; use EzPhp\Console\Output; class GreetCommand implements CommandInterface { public function getName(): string { return 'greet'; } public function getDescription(): string { return 'Greet someone'; } public function getHelp(): string { return 'Usage: ez greet <name>'; } public function handle(array $args): int { $input = new Input($args); $name = $input->argument(0) ?? 'World'; Output::success("Hello, $name!"); return 0; } }
Running the console
use EzPhp\Console\Console; $console = new Console([new GreetCommand()]); exit($console->run($argv));
Output helpers
Output::line('plain text'); Output::info('informational message'); // blue Output::success('it worked!'); // green Output::warning('be careful'); // yellow Output::error('something failed'); // red (stderr) Output::colorize('custom', 35); // magenta
Parsing arguments
$input = new Input(['foo', '--name=Alice', '--verbose']); $input->argument(0); // 'foo' $input->option('name'); // 'Alice' $input->option('missing', 'default'); // 'default' $input->hasFlag('verbose'); // true
Progress bars
$bar = Output::progressBar(100); foreach ($items as $item) { process($item); $bar->advance(); } $bar->finish();
Interactive prompts
use EzPhp\Console\Prompt; $prompt = new Prompt(); $name = $prompt->ask('What is your name?'); $ok = $prompt->confirm('Continue?'); $color = $prompt->choice('Pick a color', ['red', 'green', 'blue']);
Structured command definitions
Commands implementing HasDefinition expose typed argument and option metadata for --help rendering:
use EzPhp\Console\HasDefinition; use EzPhp\Console\CommandDefinition; class MyCommand implements CommandInterface, HasDefinition { public function getDefinition(): CommandDefinition { return (new CommandDefinition()) ->argument('name', 'The user name') ->option('force', 'f', 'Skip confirmation'); } // ... }
Command aliases
use EzPhp\Console\AliasedCommand; $commands = [ new AliasedCommand($app->make(MigrateCommand::class), 'db:migrate'), ];
Classes
| Class | Description |
|---|---|
Console |
Command registry and dispatcher |
CommandInterface |
Contract: getName, getDescription, getHelp, handle |
HasDefinition |
Optional interface: exposes CommandDefinition for structured --help |
AliasedCommand |
Wraps a command under a different name |
Input |
Parses argv tokens into positional arguments and named options/flags |
Output |
Static ANSI output helpers; table(); progressBar() factory |
ProgressBar |
In-place terminal progress bar |
Prompt |
Interactive prompts: ask(), confirm(), choice() |
InputStreamInterface |
Abstraction over a readable line stream (injectable for testing) |
StdinInputStream |
InputStreamInterface implementation reading from STDIN |
CommandDefinition |
Fluent builder for argument + option declarations |
ArgumentDefinition |
Value object: positional argument (name, description, required) |
OptionDefinition |
Value object: named option (name, short alias, description) |
License
MIT