ez-php/console

Lightweight console infrastructure for PHP — command dispatcher, argument parser, and colored output helpers

Maintainers

Package info

github.com/ez-php/console

pkg:composer/ez-php/console

Statistics

Installs: 3 233

Dependents: 4

Suggesters: 0

Stars: 0

Open Issues: 0

1.8.0 2026-04-19 10:18 UTC

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