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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

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! 😲

https://i.imgur.com/uu8gQBr.gif

Behaviours.

Here is what happens when CLI runs your class object:

Arguments.

Arguments directly map to function definitions.

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.

https://i.imgur.com/mGr1PIj.gif

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:

https://i.imgur.com/EXuX9Jx.gif

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,

buy me a coffee. ☕😃 Donate


All versions of cli with dependencies

PHP Build Version
Package Version
Requires php Version ^7.2
ext-json Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package b-hayes/cli contains the following files

Loading the files please wait ....