Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to the official documentation for initphp/console — a small,
dependency-free toolkit for writing command-line applications in PHP: command
routing, typed arguments, coloured output, interactive prompts, and a
styleable ANSI table renderer.
composer require initphp/console#!/usr/bin/env php<?phprequire__DIR__ . '/vendor/autoload.php';
useInitPHP\Console\{Application, Input, Output};
$console = newApplication('Acme CLI', '1.0.0');
$console->register('greet', function (Input$input, Output$output) {
$output->writeln('Hello {name}!', ['name' => $input->getArgument('name', 'World')]);
}, 'Greets someone.');
$console->run();php console.php greet --name=Ada # Hello Ada!
php console.php list # Overview of all commands| Type | Purpose |
|---|---|
Application | The command registry and dispatcher. |
Command | Base class for class-based commands. |
Input / InputInterface | Parsed access to arguments, options and segments. |
InputArgument | A typed, validated --name argument declaration. |
Output / OutputInterface | Coloured writing, lists, progress bar and prompts. |
Question | A value object describing an interactive question. |
Helpers | Scalar value casting shared across the components. |
Utils\Table | A styleable, multibyte-aware ASCII/ANSI table renderer. |
- New to the package? Read Installation, then Quick Start.
- Building commands? See Commands, then Input and Input Arguments.
- Formatting output? Read Output; for prompts see Questions.
- Rendering tables? See Tables.
- Coming from
initphp/cli-table? Read the Migration Guide. - Looking for a specific method? The full API Reference lists every class member.
This library splits the tokens after the command name into three buckets:
| Token | Shape | Accessor |
|---|---|---|
| Argument | --name, --name=value | getArgument() |
| Option | -v, -abc, -key=value | getOption() |
| Segment | bare value, e.g. migrate | getSegment() |
Here
--longtokens are called arguments and-shorttokens are called options — the opposite of some other CLI frameworks. Keep it in mind when porting code. See Input for the full grammar.
- License: MIT
- Minimum PHP: 7.2 (CI matrix runs 7.2 – 8.4)
- Runtime dependencies: none
- Optional extension:
ext-mbstring(improves table alignment for UTF-8 values) - Packagist:
initphp/console - Source: github.com/InitPHP/Console
- Issues: github.com/InitPHP/Console/issues
- Discussions: github.com/orgs/InitPHP/discussions
- Security:
SECURITY.md
Since 2.1 this package also ships the table renderer formerly distributed as
initphp/cli-table(now deprecated). The old class name keeps working via aclass_alias— see Migration Guide.
If something in this wiki is unclear, ambiguous, or wrong, please open an issue — documentation fixes are reviewed eagerly.
initphp/console · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Building Commands
Output & Interaction
Reference
Practical Guides
Migration & Help