PHPackages                             popphp/pop-console - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. popphp/pop-console

ActiveLibrary[CLI &amp; Console](/categories/cli)

popphp/pop-console
==================

Pop Console Component for Pop PHP Framework

4.2.6(8mo ago)59.5k↑46.8%5BSD-3-ClausePHPPHP &gt;=8.3.0CI passing

Since Jul 28Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/popphp/pop-console)[ Packagist](https://packagist.org/packages/popphp/pop-console)[ Docs](https://github.com/popphp/pop-console)[ RSS](/packages/popphp-pop-console/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (2)Versions (31)Used By (5)

pop-console
===========

[](#pop-console)

[![Build Status](https://github.com/popphp/pop-console/workflows/phpunit/badge.svg)](https://github.com/popphp/pop-console/actions)[![Coverage Status](https://camo.githubusercontent.com/23ffc9a44c6f7d6c7154740c6af951cc92aa0b6ca60ed57f5de19838c5ef2df4/687474703a2f2f63632e706f707068702e6f72672f636f7665726167652e7068703f636f6d703d706f702d636f6e736f6c65)](http://cc.popphp.org/pop-console/)

[![Join the chat at https://discord.gg/TZjgT74U7E](https://camo.githubusercontent.com/acad7b0eeb78b78d08ffd2b85681ab243436388b5f86f8bcb956a69246e53739/68747470733a2f2f6d656469612e706f707068702e6f72672f696d672f646973636f72642e737667)](https://discord.gg/TZjgT74U7E)

- [Overview](#overview)
- [Install](#install)
- [Quickstart](#quickstart)
- [Response Buffer](#response-buffer)
- [Colors](#colors)
- [Lines](#lines)
- [Headers](#headers)
- [Alerts](#alerts)
- [Prompt](#prompt)
- [Commands](#commands)
- [Help Screen](#help-screen)

Overview
--------

[](#overview)

`pop-console` provides a layer to run an application from the console terminal and produce formatted output to the terminal window. It has support for commands and their parameters, as well ANSI-based console colors. It can be easily be used with an application built with Pop to route requests from the CLI to the application.

`pop-console` is a component of the [Pop PHP Framework](https://www.popphp.org/).

**Note**

The code below represents basic examples. Ideally, you could wire an application to use the console for outputting content to the terminal screen, but not for setting routes, controllers and actions. Refer to the [Pop PHP Tutorial](https://github.com/popphp/popphp-tutorial) example application to see how to wire up a CLI-based application complete with routes using Pop PHP.

[Top](#pop-console)

Install
-------

[](#install)

Install `pop-console` using Composer.

```
composer require popphp/pop-console

```

Or, require it in your composer.json file

```
"require": {
    "popphp/pop-console" : "^4.2.6"
}

```

[Top](#pop-console)

Quickstart
----------

[](#quickstart)

### Outputting to the console

[](#outputting-to-the-console)

You can use a console object to manage and deploy output to the console, including a prepended header and appended footer.

```
use Pop\Console\Console;

$console = new Console();
$console->setHeader('My Application'); // Set a global header at the start of the script
$console->setFooter('The End');        // Set a global footer at the end of the script

$console->append('Here is some console information.');
$console->append('Hope you enjoyed it!');
$console->send();
```

The above will output:

```
    My Application

    Here is some console information.
    Hope you enjoyed it!

    The End

```

### Console wrap and margin

[](#console-wrap-and-margin)

By default, the console object enforces a wrap width at 80 characters and provides a margin of 4 spaces for readability. These values can be changed to whatever is needed for the application.

```
use Pop\Console\Console;

$console = new Console(40, 2); // wrap width of 40, margin of 2 spaces
$console->append(
    'Here is some console information. This is a really long string. It will have to wrap.'
);
$console->send();
```

```
  Here is some console information. This
  is a really long string. It will have to
  wrap.

```

[Top](#pop-console)

Response Buffer
---------------

[](#response-buffer)

### Append vs Write

[](#append-vs-write)

In the above examples, the method `append()` was used in conjunction with `send()`. The method `append()`appends the content to the response buffer, which will only get produced to the terminal screen when the method `send()` is called. This is useful if you have to take a number of steps to create the response buffer before sending it.

Using the method `write()` allows you to produce content to the terminal screen in real time, without having to call the `send()` method. This is useful if you need to push content out to the terminal screen of the application as you go.

```
use Pop\Console\Console;

$console = new Console(40);
$console->write(
    'Here is some console information. This is a really long string. It will have to wrap.'
);
```

### Newline and Margin

[](#newline-and-margin)

By default, calling the `append()` or `write()` methods will produce the margin value at the beginning of the content and a newline at the end of the content. If this is not the desired behavior, boolean flags can be passed to control this:

```
use Pop\Console\Console;

$console = new Console(40);
$console->write('Here ', false);          // No new line, but use margin
$console->write('is ', false, false);     // No new line, no margin
$console->write('some ', false, false);   // No new line, no margin
$console->write('content.', true, false); // Use new line, but no margin
```

[Top](#pop-console)

Colors
------

[](#colors)

On a console terminal that supports it, you can colorize text outputted to the console with the `colorize()` method:

```
use Pop\Console\Console;
use Pop\Console\Color;

$console = new Console();
$console->write(
    'Here is some ' .
    $console->colorize('IMPORTANT', Color::BOLD_RED) .
    ' console information.'
);
```

The `colorize()` method is also available as a static method on the `Pop\Console\Color` class:

```
use Pop\Console\Console;
use Pop\Console\Color;

$console = new Console();
$console->write(
    'Here is some ' .
    Color::colorize('IMPORTANT', Color::BOLD_RED) .
    ' console information.'
);
```

Available color constants include:

- NORMAL
- BLACK
- RED
- GREEN
- YELLOW
- BLUE
- MAGENTA
- CYAN
- WHITE
- BRIGHT\_BLACK
- BRIGHT\_RED
- BRIGHT\_GREEN
- BRIGHT\_YELLOW
- BRIGHT\_BLUE
- BRIGHT\_MAGENTA
- BRIGHT\_CYAN
- BRIGHT\_WHITE
- BOLD\_BLACK
- BOLD\_RED
- BOLD\_GREEN
- BOLD\_YELLOW
- BOLD\_BLUE
- BOLD\_MAGENTA
- BOLD\_CYAN
- BOLD\_WHITE
- BRIGHT\_BOLD\_BLACK
- BRIGHT\_BOLD\_RED
- BRIGHT\_BOLD\_GREEN
- BRIGHT\_BOLD\_YELLOW
- BRIGHT\_BOLD\_BLUE
- BRIGHT\_BOLD\_MAGENTA
- BRIGHT\_BOLD\_CYAN
- BRIGHT\_BOLD\_WHITE

[Top](#pop-console)

Lines
-----

[](#lines)

The `line()` method provides a way to print a horizontal line rule out to the terminal. The default character for the line is a dash `-`, but any character can be passed into the method.

```
use Pop\Console\Console;

$console = new Console();
$console->line();
```

```
    ----------------------------------------

```

It will default to the wrap width of the console object. If no wrap width is available, it will take on the width of the terminal, unless a custom width is specified:

```
use Pop\Console\Console;

$console = new Console();
$console->line('=', 20);
```

```
    ====================

```

[Top](#pop-console)

Headers
-------

[](#headers)

The `header()` method provides a way to output a separate block of text with an underline emphasis:

```
use Pop\Console\Console;

$console = new Console(80);
$console->header('Hello World');
```

```
    Hello World
    -----------

```

The character, size and alignment can be controlled as well:

```
use Pop\Console\Console;

$console = new Console();
$console->header('Hello World', '=', 40, 'center');
```

```
                   Hello World
    ========================================

```

[Top](#pop-console)

Alerts
------

[](#alerts)

Alerts are specially formatted boxes that provide style and enhancement to the user's experience in regard to important information and notifications.

```
use Pop\Console\Console;

$console = new Console(40);
$console->alertDanger('Hello World', 'auto');
$console->alertWarning('Hello World', 'auto');
$console->alertSuccess('Hello World', 'auto');
$console->alertInfo('Hello World', 'auto');
$console->alertPrimary('Hello World', 'auto');
$console->alertSecondary('Hello World', 'auto');
$console->alertDark('Hello World', 'auto');
$console->alertLight('Hello World', 'auto');
$console->alertBox('Hello World', '-', '|', 'auto');
```

The `alertBox()` method produces a colorless alert box with a border made of character strings. The above code will produce the following output to the console terminal:

[![Alerts](tests/tmp/alerts.png)](tests/tmp/alerts.png)

[Top](#pop-console)

Prompt
------

[](#prompt)

You can trigger a prompt to get information from the user:

```
use Pop\Console\Console;

$console = new Console();
$name    = $console->prompt('Please provide your name: ');
$console->write('Hello ' . $name . '!');
```

```
$ ./app
    Please provide your name:  Nick
    Hello Nick!
```

You can also enforce a certain set of options as well as case-sensitivity. The prompt will not accept a value outside of the provided range of option values. If the case-sensitive flag is set to `true`, the prompt will not accept values that are not an exact case-match.

```
use Pop\Console\Console;

$console = new Console();
$letter  = $console->prompt(
    'Which is your favorite letter: A, B, C, or D? ',
    ['A', 'B', 'C', 'D'],
    true
);
$console->write('Your favorite letter is ' . $letter . '.');
```

```
$ ./app
    Which is your favorite letter: A, B, C, or D? B
    Your favorite letter is B.
```

### Confirm

[](#confirm)

The `confirm()` method is a shorthand version of a prompt to ask if the user is sure they want to proceed, else the application will exit:

```
use Pop\Console\Console;

$console = new Console();
$console->confirm();
$console->write('The user said yes.');
```

```
    Are you sure? [Y/N] y
    The user said yes.

```

[Top](#pop-console)

Commands
--------

[](#commands)

A command object allows you to define the name, parameters and help string values of a command and add the command to the console object:

```
use Pop\Console\Console;
use Pop\Console\Command;

$command1 = new Command('users');
$command1->setParams('--list []');
$command1->setHelp('This is the users help screen');

$command2 = new Command('roles');
$command2->setParams('--list []');
$command2->setHelp('This is the roles help screen');

$console = new Console();
$console->addCommand($command1);
$console->addCommand($command2);
```

[Top](#pop-console)

Help Screen
-----------

[](#help-screen)

Registering the commands with the console object like in the above example allows you to call the `help()` method to view the auto-generated help screen:

```
$console->help();
```

```
    users --list []    This is the users help screen
    roles --list []    This is the roles help screen

```

However, the console object has the method `addCommandsFromRoutes()` which works in conjunction with a `Pop\Router\Cli\Match` object to automatically generate the command, along with their parameters and help strings.

```
use Pop\Console\Console;

$this->console->addCommandsFromRoutes($cliRouteMatch, './myapp');
```

This console will use the CLI route match object and parse out all of the commands and make them available for the console object to leverage for the help screen.

### Help colors

[](#help-colors)

An extra layer of presentation control is available by way of setting the help screen colors. You can choose up to 4 colors that will be used in breaking apart the command strings by name and parameters and colorizing them to make the different segments standout in an organized fashion.

Let's take a look at the abstract constructor of the `pop-kettle` component.

```
    public function __construct(Application $application, Console $console)
    {
        $this->application = $application;
        $this->console     = $console;

        $this->console->setHelpColors(
            Color::BOLD_CYAN, Color::BOLD_GREEN, Color::BOLD_MAGENTA
            );
        $this->console->addCommandsFromRoutes(
            $application->router()->getRouteMatch(), './kettle'
        );
    }

    public function help()
    {
        $this->console->help();
    }
```

In the above constructor method, the help colors are set and then the application object pushes the CLI route match object into the console method `addCommandsFromRoutes()`. The second parameter `./kettle` is a script prefix to prepend to each line of help. Those two lines are all that is needed to produce the colorful and well organized help screen for `pop-kettle`, which is called within the controller's `help()` method.

The output looks like this:

[![Console Help](tests/tmp/console-help.png)](tests/tmp/console-help.png)

[Top](#pop-console)

###  Health Score

53

—

FairBetter than 96% of packages

Maintenance62

Regular maintenance activity

Popularity29

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity89

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~150 days

Total

26

Last Release

241d ago

Major Versions

2.1.0 → 3.0.02017-02-22

v2.x-dev → 3.1.02019-02-18

3.2.1 → 4.0.02023-11-17

PHP version history (8 changes)2.0.0PHP &gt;=5.4.0

3.0.0PHP &gt;=5.6.0

3.1.0PHP &gt;=7.1.0

3.2.0PHP &gt;=7.3.0

3.2.1PHP &gt;=7.4.0

4.0.0PHP &gt;=8.1.0

4.2.3PHP &gt;=8.2.0

4.2.6PHP &gt;=8.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/c19dee900e9e20039c723cc403f76b5c22ac2dddb3f86773ae64fb082d4949e2?d=identicon)[nicksagona](/maintainers/nicksagona)

---

Top Contributors

[![nicksagona](https://avatars.githubusercontent.com/u/898670?v=4)](https://github.com/nicksagona "nicksagona (102 commits)")

---

Tags

phpcliconsoleconsole-applicationpoppop phpcli application

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/popphp-pop-console/health.svg)

```
[![Health](https://phpackages.com/badges/popphp-pop-console/health.svg)](https://phpackages.com/packages/popphp-pop-console)
```

###  Alternatives

[nunomaduro/termwind

It's like Tailwind CSS, but for the console.

2.5k271.5M385](/packages/nunomaduro-termwind)[nunomaduro/laravel-console-task

Laravel Console Task is a output method for your Laravel/Laravel Zero commands.

2592.3M13](/packages/nunomaduro-laravel-console-task)[alecrabbit/php-console-spinner

Extremely flexible spinner for \[async\] php cli applications

24038.0k2](/packages/alecrabbit-php-console-spinner)[mehrancodes/laravel-harbor

A CLI tool to Quickly create On-Demand preview environment for your apps.

10097.5k](/packages/mehrancodes-laravel-harbor)[php-school/learn-you-php

An introduction to PHP's core features: i/o, http, arrays, exceptions and so on.

3202.0k](/packages/php-school-learn-you-php)[alecrabbit/php-cli-snake

Lightweight cli spinner with zero dependencies

29216.7k5](/packages/alecrabbit-php-cli-snake)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
