Skip to content
Console application skeleton.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Console application

Build Status Code Quality Test Coverage Version Licence

Table of contents


composer require weew/console



This package provides a convenient skeleton for console application. Optically, heavily inspired by the symfony console it has it similarities and differences. Describing commands, parsing console arguments, styling and colorization of output, question helpers etc., all this is included inside this package. In related projects you'll find a list of used components that you might also use separately from this package.

Note: this package has not been tested on windows. Windows contributions are highly appreciated.


To start building your console app, you need to instantiated a new console first.

$console = new Console();


Commands are the pieces of logic that you might plug and play into your console application. A command can by anything. There is no interface contract that you must fulfill. This design choice was made because of the dependency injection support for the weew/console-container-aware package.

Your command must have the setup and run method. For further information about configuration of commands refer to the weew/console-arguments package.

class SampleCommand {
    public function setup(ICommand $command) {
        // describe command

            ->setDescription('Shows a list of colors');

        $command->argument(ArgumentType::SINGLE, 'favorite_color');
        $command->argument(ArgumentType::SINGLE_OPTIONAL, '--only-light');
        $command->argument(ArgumentType::SINGLE_OPTIONAL, '--only-dark');

    public function run(IInput $input, IOutput $output, IConsole $console) {
        // do your thang

        if ( ! $input->hasOption('--only-dark')) {

        if ( ! $input->hasOption('--only-light')) {

        if ($input->hasArgument('favorite_color')) {
            $favoriteColor = $input->getArgument('favorite_color');
            $output->writeLine("The best of all colors is <keyword>$favoriteColor</keyword>");

All the important functionality is available trough instances of IInput and IOutput. There are many more things you can do, just take a look at it. All you have to do now is to register your command on the console.


// or

$console->addCommand(new SampleCommand());

Running your command is as easy as pie.

$console->parseString('colors red --only-dark');

// or

$console->parseArgs(['colors', 'red', '--only-dark'];

// or

$console->parseArgv(['./file_name', 'colors', 'red', '--only-dark']);

You can prevent a command from executing in parallel setting the appropriate flag.



Input contains all the information about the received arguments and offers some apis for interaction with the user.

This is how you can retrieve the current command:


You can access arguments and options that were matched for the command:


You can prompt user for input using this methods:



Output is used to print information to the terminal. It uses the weew/console-formatter for styling and formatting of the text.

$output->writeLine('<keyword>key: </keyword> value');
$output->write('some text);


Widgets are small and reusable classes that serve as a kind of ui elements.


This widget allows you to easily print simple tables.

$table = new TableWidget($input, $output);
    ->setTitle('Table title')
    ->addRow('task1', 'x')
    ->addSection('Done tasks')
    ->addRow('task2', '');



There are several helpers that you might use for interaction with the user.


This helper allows you to prompt user for different kinds of input.

$prompt = new PromptHelper($input, $output);
$response = $prompt->ask('Are you ready?');
$response = $prompt->prompt('What is your name');

Related projects

You can’t perform that action at this time.