PHPackages                             justim/consoler - 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. justim/consoler

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

justim/consoler
===============

Sinatra-like application builder for the console

226[1 issues](https://github.com/justim/consoler/issues)PHP

Since Mar 11Pushed 11y ago2 watchersCompare

[ Source](https://github.com/justim/consoler)[ Packagist](https://packagist.org/packages/justim/consoler)[ RSS](/packages/justim-consoler/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Consoler [![Build Status](https://camo.githubusercontent.com/34464964b745a6c49679939ea7a103a82e13fbfb0b56c51758d452868bac08cb/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f6a757374696d2f636f6e736f6c65722e706e67)](http://travis-ci.org/justim/consoler) [![Coverage Status](https://camo.githubusercontent.com/0d70b9babbfd6424cfdb6595e1b0f1c467c9a296051275f5d40f2c2fcb01dd5e/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6a757374696d2f636f6e736f6c65722e737667)](https://coveralls.io/r/justim/consoler?branch=master)
===============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#consoler--)

> Sinatra-like application builder for the console

Features
--------

[](#features)

- No fiddling with `$argv` and friends
- Arguments filled based on their name, for easy access
- Easily plugged in your current codebase (see [Last thingies](#last-thingies))
- Grouped optionals
- Option aliases

Requirements
------------

[](#requirements)

- `PHP >= 5.4`

Installation
------------

[](#installation)

- For Consoler to work you only need the `Consoler.php` file, download it and hack away
- Also available at [Packagist](https://packagist.org/packages/justim/consoler) (Composer)

Workflow
--------

[](#workflow)

Begin by creating an instance of `Consoler` — in these example we use `app.php` as filename for the script:

```
$app = new Consoler
```

Then you can add command to it in two ways:

```
$app('options', $callback); // available at: php app.php
$app->create('options', $callback); // available at: php app.php create
```

To these functions you can add two parameter, the options as a string, and a callback which will be called when a command is matched. The options are optional and can be skipped, the first argument will become the callback.

### Syntax of the options

[](#syntax-of-the-options)

- `-v`: short option with the name `v` needed (ex. `php app.php -v`)
- `--verbose`: long option with the name `verbose` needed (ex. `php app.php --verbose`)
- `filename`: argument (ex. `php app.php sherlock.php`)
- `-f=`: short option with value (ex. `php app.php -f sherlock`)
- `--filename=`: long option with value (ex. `php app.php --filename sherlock`)
- `[ .. ]`: optional options/arguments. Optional-tokens can occur anywhere in the options, as long as they are not nested and properly closed. You can group optional parts, meaning that both options/arguments should be available or both not. (ex. `php app.php` would match `[-f=]`, and so would `php app.php -f sherlock.mp4`)
- `--verbose|-v`: options can have aliases where the first one is leading (see [Return types in callback](#return-types-in-callback))
- `file:filename`: string before the colon is the name of a filter, currently only `file` and `dir` are available. (ex. [Filters example](#filters-example))

Options and/or arguments are mandatory unless specified otherwise.

### Return types in callback

[](#return-types-in-callback)

- Short options give a integer: `php app.php -v -v` -&gt; `$v === 2` (zero when optional and not provided)
- Long options give a boolean: `php app.php --verbose` -&gt; `$verbose === true` (`false` when not provided)
- Options with a value give a string: `php app.php --filename sherlock.mp4` =&gt; `$filename === 'sherlock'` (`null` when optional and not provided)
- With aliases the leading type is used: `php app.php --verbose` -&gt; `$verbose === 1` (with `-v|--verbose`)

Examples
--------

[](#examples)

### Basic example

[](#basic-example)

```
$app = new Consoler;
$app->create('filename', function($filename)
{
	touch($filename);
});
$app->run();
```

Call with: `php app.php create sherlock.mp4`

### Options example

[](#options-example)

```
$app = new Consoler;
$app->create('[-f|--force] filename', function($filename, $force)
{
	if ($force || !file_exists($filename))
	{
		touch($filename);
	}
});
$app->run();
```

Call with: `php app.php create -f sherlock.mp4`

### Filters example

[](#filters-example)

```
$app = new Consoler;
$app->remove('file:filename', function($filename)
{
	unlink($filename);
});
$app->run();
```

Call with: `php app.php remove sherlock.mp4` *(only matches when file exists)*

### Arguments example

[](#arguments-example)

```
$app = new Consoler;
$app->remove('[foo] [bar baz] filename', function($foo, $bar, $baz, $filename)
{
	// foo = null
	// bar = '1'
	// baz = '2'
	// filename = 'sherlock.mp4'
});
$app->run();
```

Call with: `php app.php remove 1 2 sherlock.mp4` *(foo is not matched, bar &amp; baz take precedence because they are grouped)*

### Interactive example

[](#interactive-example)

```
$app = new Consoler;
$app->remove('filename', function($filename, $confirm, $print)
{
	if ($confirm('Are you sure?', 'y' /* default */))
	{
		unlink($filename);
		$print('File removed');
	}
	else
	{
		$print('File not removed');
	}
});
$app->run();
```

Call with: `php app.php remove sherlock.mp4`

### Mooooaaaarrr

[](#mooooaaaarrr)

Check out the tests in [tests/ConsolerTest.php](https://github.com/justim/consoler/blob/master/tests/ConsolerTest.php).

Helpers
-------

[](#helpers)

> Helpers are available as parameter in the callback

- `$print` -&gt; prints to the standard out (`STDOUT`)
- `$error` -&gt; prints to the standard error (`STDERR`)
- `$ask` -&gt; return data from `STDIN` — typing :)
- `$confirm` -&gt; ask for `y` or `n` and return a `boolean`
- `$password` -&gt; ask for a password without showing the input (on `STDERR`)
- `$file` -&gt; ask for a valid file
- `$exit` -&gt; helper to exit the process (`exit;`) with optional error message or code

### Last thingies

[](#last-thingies)

You can use one of your existing classes as a valid callback by adding `__consolerInvoke`-method.

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance10

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/5addc442589060d2c83836f11b851560df560ac2da1156b20ba0e93788467c7a?d=identicon)[justim](/maintainers/justim)

---

Top Contributors

[![justim](https://avatars.githubusercontent.com/u/757292?v=4)](https://github.com/justim "justim (11 commits)")

---

Tags

consolephp

### Embed Badge

![Health badge](/badges/justim-consoler/health.svg)

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

###  Alternatives

[wp-cli/wp-cli

WP-CLI framework

5.0k17.2M320](/packages/wp-cli-wp-cli)[consolidation/annotated-command

Initialize Symfony Console commands from annotated command class methods.

22569.8M19](/packages/consolidation-annotated-command)[chi-teck/drupal-code-generator

Drupal code generator

26947.8M5](/packages/chi-teck-drupal-code-generator)[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)[illuminate/console

The Illuminate Console package.

12944.1M5.1k](/packages/illuminate-console)[php-tui/php-tui

Comprehensive TUI library heavily influenced by Ratatui

589747.0k6](/packages/php-tui-php-tui)

PHPackages © 2026

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