Download the PHP package initphp/console without Composer
On this page you can find all versions of the php package initphp/console. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download initphp/console
More information about initphp/console
Files in initphp/console
Package console
Short Description A simple helper library for writing console/CLI applications in PHP, including a styleable ANSI table renderer (formerly initphp/cli-table).
License MIT
Homepage https://github.com/InitPHP/Console
Informations about the package console
InitPHP Console
A small, dependency-free helper library for writing console / CLI applications in PHP — command routing, typed arguments, coloured output, interactive questions and a styleable ANSI table renderer.
Starting with 2.1 this package also ships the ANSI table renderer that used to be distributed as the separate
initphp/cli-tablepackage (now deprecated). See Migrating frominitphp/cli-table.
Features
- Command routing — register commands as closures or as classes extending
Command. - Grouped help — automatic
help/listoverview and per-command--helpusage. - Typed arguments — declare
--namearguments with a type (INT,FLOAT,BOOL, …), a default and an optional/required flag; values are validated automatically. - Input parsing — long arguments (
--name=value), short options (-v,-abc,-k=value) and bare positional segments, with automatic scalar type casting. - Coloured output — 16/256-colour SGR helpers, message styles (
error,success,warning,info), key/value lists and a progress bar. - Interactive prompts — free-form
ask()and option-constrainedquestion(). - Table rendering — a styleable, multibyte-aware ASCII/ANSI table.
- Testable I/O — output and input streams are injectable, so commands can be unit tested without touching
STDOUT/STDIN.
Requirements
- PHP 7.2 or higher
ext-mbstring(optional) — improves table alignment for multibyte (UTF-8) values
Installation
Quick start
Create an entry script (e.g. console.php):
Run it:
Terminology
This library distinguishes three kinds of tokens that follow the command name:
| Token | Shape | Accessor |
|---|---|---|
| Argument | --name, --name=value |
getArgument() |
| Option | -v, -abc, -key=value |
getOption() |
| Segment | bare value, e.g. migrate |
getSegment() |
Note: here
--longtokens are called arguments and-shorttokens are called options. This is the opposite of some other frameworks — keep it in mind when porting code.
All scalar values are cast automatically: "true"/"false"/"yes"/"no" → bool, "null" → null, integer/decimal strings → int/float.
Class-based commands
For anything beyond a one-liner, extend Command:
Declared arguments() are validated before execute() runs: missing required arguments or values that do not match the declared type abort the command with an error.
Output
Interactive prompts
Typing exit or quit at any prompt terminates the application.
Rendering tables
Non-string cell values are stringified ([NULL], [TRUE], [FALSE], [ARRAY], [CALLABLE], [RESOURCE], or the class name for objects), columns are auto-sized, and mb_strlen() is used when available so multibyte values align correctly.
Documentation
In-depth, example-driven guides live in docs/:
- Getting started
- Commands
- Input: arguments, options & segments
- Typed input arguments
- Output & formatting
- Interactive questions
- Tables
- Migrating from
initphp/cli-table
Migrating from initphp/cli-table
The standalone initphp/cli-table package has been merged into this one as of 2.1 and is now deprecated.
If your code uses \InitPHP\CLITable\Table, no source changes are required — this package ships a class_alias keeping the old fully-qualified name working. Just switch the dependency:
(initphp/console declares a Composer replace for initphp/cli-table, so the two will never be installed side by side.) When you next touch the code, prefer the canonical namespace:
The alias is a transition aid and may be removed in a future major release. See the migration guide for details.
Testing & quality
Contributing
Contributions are welcome. Please run composer qa before opening a pull request. See the organisation contributing guidelines.
Credits
License
Released under the MIT License. Copyright © 2022 InitPHP.