PHPackages                             phpixie/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. phpixie/console

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

phpixie/console
===============

Console command library for PHPixie

3.4.3(8y ago)117.4k15BSD-3-ClausePHP

Since Jun 13Pushed 8y ago1 watchersCompare

[ Source](https://github.com/PHPixie/Console)[ Packagist](https://packagist.org/packages/phpixie/console)[ Docs](http://phpixie.com)[ RSS](/packages/phpixie-console/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (3)Versions (13)Used By (5)

Console
=======

[](#console)

PHPixie console command component

[![Author](https://camo.githubusercontent.com/24a0a94bb83eb81ba03c32074967dc730174cfd2849e984169db461d955cdbb9/687474703a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d40647261636f6e792d626c75652e7376673f7374796c653d666c61742d737175617265)](https://twitter.com/dracony)[![Source Code](https://camo.githubusercontent.com/e262163a11102703beda41a8df9e276d68747bddaa7b01cea01a16247e0ea4e9/687474703a2f2f696d672e736869656c64732e696f2f62616467652f736f757263652d706870697869652f636f6e736f6c652d626c75652e7376673f7374796c653d666c61742d737175617265)](https://github.com/phpixie/console)[![Software License](https://camo.githubusercontent.com/b60331a2084501dc07cf6d6964c0da58dd005d89c45cf3b28b4b22b60f5ec00f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4253442d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/phpixie/di/blob/master/LICENSE)

PHPixie Console allows you to define and run commands in the command line. The main benefit as opposed to just keeping a folder with PHP scripts are the ability to define options and arguments, validate them, generate command usage etc.

Migrating an Existing PHPixie Project
-------------------------------------

[](#migrating-an-existing-phpixie-project)

If you create a fresh PHPixie project you are already ready to go, otherwise you need to do a few small modifications to the project, by updating some files from the new project skeleton:

1.
2.
3. Add `"phpixie/framework-bundle": "~3.0"` to `composer.json`

Optionally also copy the Console factory and the example Greet Command:

4.
5.
6. Register the Console class in your Builder, like here:

Running commands
----------------

[](#running-commands)

Now try running the console script:

```
cd your_project_directory/
./console

```

This will give you the list of available commands and their descriptions, e.g.:

```
Available commands:

app:greet                     Greet the user
framework:installWebAssets    Symlink or copy bundle web files to the projects web folder
framework:generateBundle      Generate a new bundle
help                          Print command list and usage

```

You can get extended information about a command by using the `help` command:

```
./console help framework:installWebAssets

framework:installWebAssets [ --copy ]
Symlink or copy bundle web files to the projects web folder

Options:
copy    Whether to copy web directories instead of symlinking them

```

Default commands
----------------

[](#default-commands)

**framework:installWebAssets**Its purpose is creating symlinks from bundle directories to the /web/bundles folder, e.g. /web/bundles/app -&gt; /bundles/app/web. The idea behind it is that we can have bundles that are installable and updatable via composer that provide their own web assets. The `--copy` flag will copy the directories instead of symlinking them. This is usefull if you want to deploy the files to some CDN network afterwards.

**framework:generateBundle**This command generates and registers a new bundle within your project.

Adding your own commands
------------------------

[](#adding-your-own-commands)

There is a sample `app:greet` command provided in the skeleton project. They are added in the same was as HTTP Processors, using the `\Project\App\Console` class. To add a new command you have to add it's name to the array returned by the `commandNames` method, and create a `buildCommand` method.

You can configure your command to add a description and define options and arguments. Let's look at the default `Greet` command:

```
namespace Project\App\Console;

class Greet extends \PHPixie\Console\Command\Implementation
{
    public function __construct($config)
    {
        // Specify command description
        $config->description('Greet the user');

        //Define a 'message' argument
        $config->argument('message')
            ->description("Message to display");

        parent::__construct($config);
    }

    /**
     * Gets called when the command is executed.
     * $argumentData and $optionData work in the same
     * way as HTTP $request->query() and $request->data()
     */
    public function run($argumentData, $optionData)
    {
        $message = $argumentData->get('message', "Have fun coding!");
        $this->writeLine($message);
    }
}
```

### Arguments and options

[](#arguments-and-options)

Let's say we want to define a command that dumps some tables from the database, a typical call might look like this:

```
sqldump --user=root --skip-missing -f myDatabase users items

```

Here `myDatabase` is the name of the database, followed by the names of the tables we want to dump. These are the arguments of our command. The `user`, `skip-missing` and `f` are options. Note that for arguments the order in which they are specified matters, but for the options it does not, also short one letter options can be referenced with a single `-` instead of two.

Let's look at defining options:

```
$config->option('user')

    //Mark option as required
    ->required()

    //Describe what the option does.
    //this is displayed by the 'help' command
    ->description("User to connect to the database with");

$config->option('skip-missing')
    ->description("Don't throw an error if the tables are missing")

    //mark option as flag,
    //flag options don't accept a value,
    //but are set to 'true' if they are present.
    ->flag();

$config->option('f')
    ->flag()
    ->description("Force database dump");
```

When defining arguments you have to keep in mind that they should be defined in the same order in which they should be specified. In out case this means we have to define a `database` argument before the `tables` one:

```
$config->argument('database')
    ->required()
    ->description("Which database to dump the tables from");

$config->argument('tables')
    ->description("Tables to dump")

    // Can accept more than one value.
    // There can be only one argument marked with `arrayOf`
    // and it has to be the last one.
    ->arrayOf();
```

If we were to run the `help` command now, we would see the following:

```
./console help app:sqldump

app:sqldump --user=VALUE [ -f ] [ --skip-missing ] DATABASE [ TABLES... ]

Options:
user            User to connect to the database with
f               Force database dump
skip-missing    Don't throw an error if the tables are missing

Arguments:
DATABASE  Which database to dump the tables from
TABLES    Tables to dump

```

When the command is executed the `run` method of the command receives the passed options and arguments, which can be accessed in the same way as when working with HTTP requests:

```
public function run($argumentData, $optionData)
{
    $database = $argumentData->get('database');

    // specifying default value
    $user = $optionData->get('user', 'phpixie');
}
```

### Input and Output

[](#input-and-output)

The easiest way to return output from the command is by `return`ing a string. But some commands take a while to process and you may want to provide users with intermediate status. There are some additional methods you can use:

```
public function run($argumentData, $optionData)
{
    // Write text without line break
    $this->write("Hello ");

    // Write text with new line
    $this->writeLine("World");

    // Read a line of user input
    $str = $this->readLine();

    // Throwing a CommandException will output the error message
    // and ensure that the command exits with a non-zero exit code
    throw new \PHPixie\Console\Exception\CommandException("Something bad happened");
}
```

To further control input and out you can use the CLI context, in fact the above methods are just shortcuts to CLI context calls:

```
public function run($argumentData, $optionData)
{
    $context = $this->cliContext();

    $inputStream = $cliContext->inputStream();
    $outputStream = $cliContext->outputStream();
    $errorStream = $cliContext->errorStream();

    $outputStream->write("Hello");
    $errorStream->writeLine("Something bad happened");
    $context->setExitCode(1); // set the exit code
}
```

The exit code matters for if you want to check externally if the command was successful or not, e.g. in Bash if you do something like:

```
if ./console app:somecommand ; then
    echo "Command succeeded"
else
    echo "Command failed"
fi

```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity70

Established project with proven stability

 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 ~55 days

Recently: every ~115 days

Total

12

Last Release

3014d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2fd2ee997c779cf774247d3d4cde7d90491484f817032aab6dbc314857973b36?d=identicon)[dracony](/maintainers/dracony)

---

Top Contributors

[![dracony](https://avatars.githubusercontent.com/u/3127080?v=4)](https://github.com/dracony "dracony (19 commits)")

---

Tags

cliconsole

### Embed Badge

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

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

###  Alternatives

[symfony/console

Eases the creation of beautiful and testable command line interfaces

9.8k1.1B11.3k](/packages/symfony-console)[nunomaduro/collision

Cli error handling for console/command-line PHP applications.

4.6k331.8M8.5k](/packages/nunomaduro-collision)[nunomaduro/termwind

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

2.5k239.8M286](/packages/nunomaduro-termwind)[wp-cli/php-cli-tools

Console utilities for PHP

68325.0M367](/packages/wp-cli-php-cli-tools)[php-school/cli-menu

A command line menu helper in PHP

2.0k1.1M27](/packages/php-school-cli-menu)[seld/cli-prompt

Allows you to prompt for user input on the command line, and optionally hide the characters they type

24725.8M17](/packages/seld-cli-prompt)

PHPackages © 2026

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