1.3.7 2020-09-04 17:22 UTC

This package is auto-updated.

Last update: 2021-04-04 22:57:32 UTC


Qi Console provides PHP library classes for dealing with the console or terminal.

Build Status

Installation with Composer

Use composer to include the Qi_Console library in a project.

Add the following composer.json file to your project directory:

    "require": {
        "sumpygump/qi-console": "dev-master"

Then run composer install to fetch.

$ composer.phar install

If you don't have composer already installed, this is my recommendation for installing it. See installation instructions.

$ curl -sS | php
$ sudo mv composer.phar /usr/local/bin/composer

Once the files have been composed with the composer install command, you can use any of the Qi_Console_ classes after composer's autoloader is included.

require 'vendor/autoload.php';

$terminal = new Qi_Console_Terminal();
// ...

Manual Installation

You can also download the files and place them in a library folder. If you do this, be sure to update your autoloader to handle the Qi_Console_* classes or else manually include the files of the classes you'll need.



ArgV provides a way to assign and gather command line arguments

It will parse and assign option flags and arguments passed in as command line arguments

Examples of script arguments it can potentially parse:

  • short option (-f) : can be grouped (-fvz)
  • long option (--flag)
  • short parameter (-p value)
  • short parameter shunt (-pvalue)
  • long parameter (--param value)
  • long parameter shunt (--param=value)
  • standalone argument (filename)


The constructor takes two arguments: $argv and $rules

The argument $argv is an array of arguments from the command line that is parsed by PHP when the script is invoked. For example, when you invoke a PHP script with the following:

php myscript.php -v --flag --param=value okay

Then in your script PHP will provide a variable $argv representative of the elements of the arguments passed in as an array like this:


The argument $rules is a definition of options and help messages. The format is a key value array where the key is the option definition (possible parameters) and the value is the help message for that option.

Here are some examples illustrating the possible rules keys:

  • v - a single letter defines a single short option flag, so this corresponds to the option -v
  • flag - a single word defines a long option flag (--flag)
  • help|h - a word, then vertical bar, then a single letter will define a long and short option (--help and -h)
  • name|n: - a key that ends with a colon character (:) indicates a required parameter. This allows the following: --name=value, -n=value, --name value, -n value and -nvalue. If the arguments for this option don't contain a parameter this will throw a Qi_Console_ArgVException.
  • arg:filename - a key that begin with the string arg: defines a non-option argument (ones that don't begin with a -). The word after the colon is the name of the argument as it will appear in ArgV. The arg: parameters in $argv will be assigned in the order they appear in the rules array. So the first non-option argument in $argv will be assigned to the first arg: key name, etc.

Here is some example code that illustrates the above:

// Example rules with some help messages
$rules = array(
    'v' => 'Use verbose messaging',
    'o' => 'Another random option',
    'flag' => 'Flag something as special',
    'name|n:' => 'Provide a name to use',
    'arg:filename' => 'Filename to parse',

// Our example input
// php scriptname -v --flag --name "a name" filename.txt
$argv = array(
    'scriptname', // The first argument is always ignored by ArgV
    '"a name"',

$arguments = new Qi_Console_ArgV($argv, $rules);

// Now we can reference the following:
$arguments->v; // true
$arguments->o; // false
$arguments->flag; // true
$arguments->name; // equal to 'a name'
$arguments->filename; // equal to 'filename.txt'

Note that any additional options that were not defined in the rules array, but were passed in as input will result in sensible defaults, so options will default to true and named options (--anothername=value) will result in the values passed in ($arguments->anothername == 'value'). Additional non-option arguments will be available as $arguments->__arg2, $arguments->__arg3, etc.


The Qi_Console_Client class provides a base console client that can be used to create command line clients. It takes input as $argv and a Terminal object which can be used to output messages back to the terminal.



Basic Usage

class MyClient extends Qi_Console_Client
    public function init()
        // Initialization logic

    public function execute()
        // Execute the main logic of this client
        // Use $this->_args (ArgV object) to handle input and react
        // Use $this->_displayWarning() to output warning message to user
        // etc.

$arguments = new Qi_Console_ArgV($argv, $rules);
$terminal = new Qi_Console_Terminal();

$myClient = new MyClient($arguments, $terminal);


The exception handler provides a way to handle exceptions from your console application. It uses the Terminal object to output pretty messages.

$terminal = new Qi_Console_Terminal();
$exceptionHandler = new Qi_Console_ExceptionHandler($terminal);

// Now any time an exception is thrown, it will output a pretty message
// with colors and exit.


Qi_Console_Menu provides a way to prompt a user with a menu and receive input. Please see the code for documentation.


Qi_Console_ProgressBar provides the ability to display a progress bar in the terminal. Please see the code for documentation.


Qi_Console_Std is a wrapper for stdin, stdout and stderr.

This class provides methods for sending and receiving input from stdin and output to stdout and stderr.

// Receive input from STDIN
$input = Qi_Console_Std::in();

// Output to STDOUT
Qi_Console_Std::out('Some text');

// Output to STDERR
Qi_Console_Std::err('Error output');


Qi_Console_Tabular generates ascii tables for displaying tabular data.

// Define the table data
$tableData = array(
    array('John', '28', 'Green'),
    array('Hannah', '7', 'Violet'),
    array('Michael', '43', 'Red'),

// Define the headers for the columns
$headers = array(
    'Favorite Color',

// Define optional alignment for columns
$alignment = array(

$tabular = new Qi_Console_Tabular(
        'headers'   => $headers,
        'cellalign' => $alignment,


This will output the following table:

|  Name     |  Age  |  Favorite Color  |
|  John     |   28  |  Green           |
|  Hannah   |    7  |  Violet          |
|  Michael  |   43  |  Red             |

Terminal and Terminfo

Qi_Console_Terminal is a wrapper for Qi_Console_Terminfo which uses the UNIX terminfo mapping database to provide functionality for outputting escape sequences to the terminal. It provides a low-level robust way using terminal features such as outputting colors.

For more information about terminfo, check out these resources:

Basic Usage

$terminal = new Qi_Console_Terminal();

// Clear the screen

echo "Text is now red.\n";

echo "Text is now green.\n";

echo "Text is now bold.\n";

$terminal->center_text("This text is centered.");

// This is using the terminfo capability "Original Pair" to set the colors
// back to default.
echo "Now text is back to default color.\n";

// This is using the terminfo capability sgr0 which turns off all text
// attributes, meaning it is not bold anymore.
echo "Now text is not bold anymore.\n";