PHPackages                             b-hayes/cli - 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. b-hayes/cli

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

b-hayes/cli
===========

Runs any PHP Class as a command line application

v0.0.9(4y ago)489[4 issues](https://github.com/b-hayes/CLI/issues)[2 PRs](https://github.com/b-hayes/CLI/pulls)MITPHPPHP ^7.2CI failing

Since Apr 19Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/b-hayes/CLI)[ Packagist](https://packagist.org/packages/b-hayes/cli)[ RSS](/packages/b-hayes-cli/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (15)Used By (0)

CLI
===

[](#cli)

Quickly build interactive Command-line applications in PHP with 0 effort.

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

[](#installation)

`composer require b-hayes/cli`

Usage.
------

[](#usage)

Simply define a PHP class and inject it into the CLI wrapper 😎.

```
(new \BHayes\CLI\CLI( $yourClass ))->run();
```

Now you can just build your class methods, without managing inputs, exit codes or printing usage hints! 😲

[![https://i.imgur.com/uu8gQBr.gif](https://camo.githubusercontent.com/cc788a58128d845a228650b1c1b778a01d9bd6fb80170ae23735ee7a3b2da698/68747470733a2f2f692e696d6775722e636f6d2f757538675142722e676966)](https://camo.githubusercontent.com/cc788a58128d845a228650b1c1b778a01d9bd6fb80170ae23735ee7a3b2da698/68747470733a2f2f692e696d6775722e636f6d2f757538675142722e676966)

Behaviours.
-----------

[](#behaviours)

Here is what happens when CLI runs your class object:

- Public methods are exposed as executable commands. 👍
- Required inputs and data types are automatically enforced. 🙂
- Automatically provides usage hints to guide the user. 😊
- Print anything that is returned. (public properties only if an object). 😃
- Manages error messages with Shell exit codes! 😲
- Maps your public properties to options/flags. 🤯
- Easily print or return coloured strings and prompt for input/confirmations 😍

### Arguments.

[](#arguments)

Arguments directly map to function definitions.

- Parameter types are strongly enforced (eg, bool must be 'true' or 'false' and int can not have decimal places).
- Prevents the user from passing too many arguments, unless it is ...variadic. (php allows it but I don't)

### Single function scripts.

[](#single-function-scripts)

If you implement [\_\_invoke()](https://www.php.net/manual/en/language.oop5.magic.php#object.invoke) method or you pass in an [anonymous function](https://www.php.net/manual/en/language.types.callable.php) instead of a class, then your app immediately executes without the need for the user input.

### Options/Flags

[](#optionsflags)

Based on the [POSIX](https://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html)standard, short options `-o` and long options `--longOption`are automatically created from public class properties.

```
public $cats;
if($this->cats) { echo "Cat mode enabled!"; }
```

[![https://i.imgur.com/mGr1PIj.gif](https://camo.githubusercontent.com/4accefd626338baae5d19c43d65c7ac0f23dfc274ea567a4a0c22ebfd8cffd91/68747470733a2f2f692e696d6775722e636f6d2f6d47723150496a2e676966)](https://camo.githubusercontent.com/4accefd626338baae5d19c43d65c7ac0f23dfc274ea567a4a0c22ebfd8cffd91/68747470733a2f2f692e696d6775722e636f6d2f6d47723150496a2e676966)

*CLI currently does not support options with arguments, yet.*

I planned to support this for php7.4 and higher using typehints, but not until I flesh out as much as I can for 7.2 users before moving on.

### Reserved Options.

[](#reserved-options)

CLI has reserved some options:

- --help. Just prints related doc blocks and exits without running your class. (might become prettier in future).
- --debug. Debug mode, if enabled all exceptions/errors and their stack traces are printed.
- -i does nothing, yet. (I have ideas for an interactive mode).

[![https://i.imgur.com/EXuX9Jx.gif](https://camo.githubusercontent.com/98c1d298e70c11a49582c1875cfef7181deb43e0f9f33deaa527b0a656d87004/68747470733a2f2f692e696d6775722e636f6d2f45587558394a782e676966)](https://camo.githubusercontent.com/98c1d298e70c11a49582c1875cfef7181deb43e0f9f33deaa527b0a656d87004/68747470733a2f2f692e696d6775722e636f6d2f45587558394a782e676966)

### Exit codes and Errors.

[](#exit-codes-and-errors)

CLI will automatically return a non-zero exit code on failure. Error output is suppressed unless you use `--debug` mode or throw a \[Response Exception\](#Response Exceptions).

Dependencies
------------

[](#dependencies)

CLI has no dependencies and does not force you to dependent on it in-case your class is also used for other things.

Getting started example.
------------------------

[](#getting-started-example)

For those unfamiliar with shell scripts...

Make a file with a shebang line (#!) at the top that tells your shell to run this with PHP.

```
#!/usr/bin/env php
