Download the PHP package b-hayes/cli without Composer
On this page you can find all versions of the php package b-hayes/cli. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package cli
Short Description Runs any PHP Class as a command line application
License MIT
Informations about the package cli
CLI
Quickly build interactive Command-line applications in PHP with 0 effort.
Installation
composer require b-hayes/cli
Usage.
Simply define a PHP class and inject it into the CLI wrapper 😎.
Now you can just build your class methods, without managing inputs, exit codes or printing usage hints! 😲
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 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.
If you implement __invoke() method or you pass in an anonymous function instead of a class, then your app immediately executes without the need for the user input.
Options/Flags
Based on the POSIX
standard, short options -o
and long options --longOption
are automatically created from public class properties.
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.
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).
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
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.
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.
Next, make the file executable:
Now you can run it as a terminal application!
CLI will guide the terminal user on how to run the available methods of your class.
Windows Users
For those who want to use powershell && cmd. Make a batch file containing:
💡 Start a collection.
I recommend keeping your cli app in a personal project,
and add its /bin
folder to your system path
so all your CLI apps are globally accessible.
I do this and use git to synchronize my personal devtools across computers. 😉
Error handling.
CLI catches all errors by default and presents a generic program crashed message.
You can use --debug
to see the stack trace if needed.
Response Exceptions
An error caused by input or an external factor, and you need to return a user response while also returning/remembering to use a non-zero exit code so the parent shell process knows the command has failed.
Just throw a UserResponseException from anywhere in your stack and CLI takes care of it.
To make life easy I have provided several, including a success response, so you don't have to return two values all the way up the stack.
You can also specify colours, emojis and more specific error codes.
The separate icon string is for globally disabling emojis output on terminals with no UTF-8 support (in the future).
Error code is last since its usually only important to have 0 or non-zero to indicate success/failure. (besides php 8 allows us to bypass order of arguments now)
Custom Response Exceptions.
You might want to specify your own exceptions to be treated as CLI responses instead of the provided ones. Simply pass in a list of class names in the constructor:
WARNING: Remember that by default all PHP and Third party exceptions will have 0 as their code and report a success response as nobody every thinks of them being used for CLI exit codes. You will have to be vigilant and account for this manually.
Forced debug mode.
During development, you may wish to always run in debug mode without typing --debug all the time. Simple pass true for the 3rd param in the constructor.
Or you may wish to remove the debug option entirely by passing in false
instead.
Side effect: If the user types --debug now the application won't run because it's now an invalid option.
To avoid this you can simulate someone typing it:
You can also prevent the debug option entirely while still passing it to your app as a property.
(supporting extreme edge case uses here and this will probably change.)
CLI as a Global tool.
CLI comes with a vendor bin to run itself exposing its prompt and colour print methods in the terminal:
You can install the package globally:
and then run it from anywhere without specifying the path:
Run any class by name.
This is great for adhoc testing on random class objects in a project your working on. It can also automatically add the projects top level namespace for you.
It does this by reading the composer.json
file in the current directory.
It will fail if the Class has a constructor with argument (I may add a way for it load dependencies in the future).
Running other shell commands.
A wrapper for passthru but, throws an Exception on failure.
This provides an eay way to stop checking exit codes. Catch the exception if you want to handle it.
Run a batch of shell commands.
Run several passthru commands and return true. Throws an exception if any of them fail.
Support.
Mainly just supporting my own use at the moment updating this project in my spare time.
At some point higher PHP versions will be required, but I do intend to try and support php7.2 and 7.4 separately for a while even after moving to php8.1 Eg. If I add a new feature that will also work in 7.2 I'll add it as a minor version update to the old version still allowing php7.2. (no guarantees tho).
Feedback && Contributions welcome.
I am using the MIT licence so feel free to do what you want, however I do ask that you submit a PR if you make any improvements or fix any bugs.
If this project gives you a 1up🍄 and you just want to show some appreciation then,
All versions of cli with dependencies
ext-json Version *