1*ab8e5256SAndreas Gohr# PHP-CLI 2*ab8e5256SAndreas Gohr 3*ab8e5256SAndreas GohrPHP-CLI is a simple library that helps with creating nice looking command line scripts. 4*ab8e5256SAndreas Gohr 5*ab8e5256SAndreas GohrIt takes care of 6*ab8e5256SAndreas Gohr 7*ab8e5256SAndreas Gohr- **option parsing** 8*ab8e5256SAndreas Gohr- **help page generation** 9*ab8e5256SAndreas Gohr- **automatic width adjustment** 10*ab8e5256SAndreas Gohr- **colored output** 11*ab8e5256SAndreas Gohr- **optional PSR3 compatibility** 12*ab8e5256SAndreas Gohr 13*ab8e5256SAndreas GohrIt is lightweight and has **no 3rd party dependencies**. Note: this is for non-interactive scripts only. It has no readline or similar support. 14*ab8e5256SAndreas Gohr 15*ab8e5256SAndreas Gohr## Installation 16*ab8e5256SAndreas Gohr 17*ab8e5256SAndreas GohrUse composer: 18*ab8e5256SAndreas Gohr 19*ab8e5256SAndreas Gohr```php composer.phar require splitbrain/php-cli``` 20*ab8e5256SAndreas Gohr 21*ab8e5256SAndreas Gohr## Usage and Examples 22*ab8e5256SAndreas Gohr 23*ab8e5256SAndreas GohrMinimal example: 24*ab8e5256SAndreas Gohr 25*ab8e5256SAndreas Gohr```php 26*ab8e5256SAndreas Gohr#!/usr/bin/php 27*ab8e5256SAndreas Gohr<?php 28*ab8e5256SAndreas Gohrrequire __DIR__ . '/../vendor/autoload.php'; 29*ab8e5256SAndreas Gohruse splitbrain\phpcli\CLI; 30*ab8e5256SAndreas Gohruse splitbrain\phpcli\Options; 31*ab8e5256SAndreas Gohr 32*ab8e5256SAndreas Gohrclass Minimal extends CLI 33*ab8e5256SAndreas Gohr{ 34*ab8e5256SAndreas Gohr // register options and arguments 35*ab8e5256SAndreas Gohr protected function setup(Options $options) 36*ab8e5256SAndreas Gohr { 37*ab8e5256SAndreas Gohr $options->setHelp('A very minimal example that does nothing but print a version'); 38*ab8e5256SAndreas Gohr $options->registerOption('version', 'print version', 'v'); 39*ab8e5256SAndreas Gohr } 40*ab8e5256SAndreas Gohr 41*ab8e5256SAndreas Gohr // implement your code 42*ab8e5256SAndreas Gohr protected function main(Options $options) 43*ab8e5256SAndreas Gohr { 44*ab8e5256SAndreas Gohr if ($options->getOpt('version')) { 45*ab8e5256SAndreas Gohr $this->info('1.0.0'); 46*ab8e5256SAndreas Gohr } else { 47*ab8e5256SAndreas Gohr echo $options->help(); 48*ab8e5256SAndreas Gohr } 49*ab8e5256SAndreas Gohr } 50*ab8e5256SAndreas Gohr} 51*ab8e5256SAndreas Gohr// execute it 52*ab8e5256SAndreas Gohr$cli = new Minimal(); 53*ab8e5256SAndreas Gohr$cli->run(); 54*ab8e5256SAndreas Gohr``` 55*ab8e5256SAndreas Gohr 56*ab8e5256SAndreas Gohr 57*ab8e5256SAndreas Gohr 58*ab8e5256SAndreas Gohr 59*ab8e5256SAndreas GohrThe basic usage is simple: 60*ab8e5256SAndreas Gohr 61*ab8e5256SAndreas Gohr- create a class and ``extend splitbrain\phpcli\CLI`` 62*ab8e5256SAndreas Gohr- implement the ```setup($options)``` method and register options, arguments, commands and set help texts 63*ab8e5256SAndreas Gohr - ``$options->setHelp()`` adds a general description 64*ab8e5256SAndreas Gohr - ``$options->registerOption()`` adds an option 65*ab8e5256SAndreas Gohr - ``$options->registerArgument()`` adds an argument 66*ab8e5256SAndreas Gohr - ``$options->registerCommand()`` adds a sub command 67*ab8e5256SAndreas Gohr- implement the ```main($options)``` method and do your business logic there 68*ab8e5256SAndreas Gohr - ``$options->getOpts`` lets you access set options 69*ab8e5256SAndreas Gohr - ``$options->getArgs()`` returns the remaining arguments after removing the options 70*ab8e5256SAndreas Gohr - ``$options->getCmd()`` returns the sub command the user used 71*ab8e5256SAndreas Gohr- instantiate your class and call ```run()``` on it 72*ab8e5256SAndreas Gohr 73*ab8e5256SAndreas GohrMore examples can be found in the examples directory. Please refer to the [API docs](https://splitbrain.github.io/php-cli/) 74*ab8e5256SAndreas Gohrfor further info. 75*ab8e5256SAndreas Gohr 76*ab8e5256SAndreas Gohr## Exceptions 77*ab8e5256SAndreas Gohr 78*ab8e5256SAndreas GohrBy default, the CLI class registers an exception handler and will print the exception's message to the end user and 79*ab8e5256SAndreas Gohrexit the programm with a non-zero exit code. You can disable this behaviour and catch all exceptions yourself by 80*ab8e5256SAndreas Gohrpassing false to the constructor. 81*ab8e5256SAndreas Gohr 82*ab8e5256SAndreas GohrYou can use the provided ``splitbrain\phpcli\Exception`` to signal any problems within your main code yourself. The 83*ab8e5256SAndreas Gohrexception's code will be used as the exit code then. 84*ab8e5256SAndreas Gohr 85*ab8e5256SAndreas GohrStacktraces will be printed on log level `debug`. 86*ab8e5256SAndreas Gohr 87*ab8e5256SAndreas Gohr## Colored output 88*ab8e5256SAndreas Gohr 89*ab8e5256SAndreas GohrColored output is handled through the ``Colors`` class. It tries to detect if a color terminal is available and only 90*ab8e5256SAndreas Gohrthen uses terminal colors. You can always suppress colored output by passing ``--no-colors`` to your scripts. 91*ab8e5256SAndreas GohrDisabling colors will also disable the emoticon prefixes. 92*ab8e5256SAndreas Gohr 93*ab8e5256SAndreas GohrSimple colored log messages can be printed by you using the convinence methods ``success()`` (green), ``info()`` (cyan), 94*ab8e5256SAndreas Gohr``error()`` (red) or ``fatal()`` (red). The latter will also exit the programm with a non-zero exit code. 95*ab8e5256SAndreas Gohr 96*ab8e5256SAndreas GohrFor more complex coloring you can access the color class through ``$this->colors`` in your script. The ``wrap()`` method 97*ab8e5256SAndreas Gohris probably what you want to use. 98*ab8e5256SAndreas Gohr 99*ab8e5256SAndreas GohrThe table formatter allows coloring full columns. To use that mechanism pass an array of colors as third parameter to 100*ab8e5256SAndreas Gohrits ``format()`` method. Please note that you can not pass colored texts in the second parameters (text length calculation 101*ab8e5256SAndreas Gohrand wrapping will fail, breaking your texts). 102*ab8e5256SAndreas Gohr 103*ab8e5256SAndreas Gohr## Table Formatter 104*ab8e5256SAndreas Gohr 105*ab8e5256SAndreas GohrThe ``TableFormatter`` class allows you to align texts in multiple columns. It tries to figure out the available 106*ab8e5256SAndreas Gohrterminal width on its own. It can be overwritten by setting a ``COLUMNS`` environment variable. 107*ab8e5256SAndreas Gohr 108*ab8e5256SAndreas GohrThe formatter is used through the ``format()`` method which expects at least two arrays: The first defines the column 109*ab8e5256SAndreas Gohrwidths, the second contains the texts to fill into the columns. Between each column a border is printed (a single space 110*ab8e5256SAndreas Gohrby default). 111*ab8e5256SAndreas Gohr 112*ab8e5256SAndreas GohrSee the ``example/table.php`` for sample usage. 113*ab8e5256SAndreas Gohr 114*ab8e5256SAndreas GohrColumns width can be given in three forms: 115*ab8e5256SAndreas Gohr 116*ab8e5256SAndreas Gohr- fixed width in characters by providing an integer (eg. ``15``) 117*ab8e5256SAndreas Gohr- precentages by provifing an integer and a percent sign (eg. ``25%``) 118*ab8e5256SAndreas Gohr- a single fluid "rest" column marked with an asterisk (eg. ``*``) 119*ab8e5256SAndreas Gohr 120*ab8e5256SAndreas GohrWhen mixing fixed and percentage widths, percentages refer to the remaining space after all fixed columns have been 121*ab8e5256SAndreas Gohrassigned. 122*ab8e5256SAndreas Gohr 123*ab8e5256SAndreas GohrSpace for borders is automatically calculated. It is recommended to always have some relative (percentage) or a fluid 124*ab8e5256SAndreas Gohrcolumn to adjust for different terminal widths. 125*ab8e5256SAndreas Gohr 126*ab8e5256SAndreas GohrThe table formatter is used for the automatic help screen accessible when calling your script with ``-h`` or ``--help``. 127*ab8e5256SAndreas Gohr 128*ab8e5256SAndreas Gohr## PSR-3 Logging 129*ab8e5256SAndreas Gohr 130*ab8e5256SAndreas GohrThe CLI class is a fully PSR-3 compatible logger (printing colored log data to STDOUT and STDERR). This is useful when 131*ab8e5256SAndreas Gohryou call backend code from your CLI that expects a Logger instance to produce any sensible status output while running. 132*ab8e5256SAndreas Gohr 133*ab8e5256SAndreas GohrIf you need to pass a class implementing the `Psr\Log\LoggerInterface` you can do so by inheriting from one of the two provided classes implementing this interface instead of `splitbrain\phpcli\CLI`. 134*ab8e5256SAndreas Gohr 135*ab8e5256SAndreas Gohr * Use `splitbrain\phpcli\PSR3CLI` if you're using version 2 of PSR3 (PHP < 8.0) 136*ab8e5256SAndreas Gohr * Use `splitbrain\phpcli\PSR3CLIv3` if you're using version 3 of PSR3 (PHP >= 8.0) 137*ab8e5256SAndreas Gohr 138*ab8e5256SAndreas GohrThe resulting object then can be passed as the logger instance. The difference between the two is in adjusted method signatures (with appropriate type hinting) only. Be sure you have the suggested `psr/log` composer package installed when using these classes. 139*ab8e5256SAndreas Gohr 140*ab8e5256SAndreas GohrNote: if your backend code calls for a PSR-3 logger but does not actually type check for the interface (AKA being LoggerAware only) you can also just pass an instance of `splitbrain\phpcli\CLI`. 141*ab8e5256SAndreas Gohr 142*ab8e5256SAndreas Gohr## Log Levels 143*ab8e5256SAndreas Gohr 144*ab8e5256SAndreas GohrYou can adjust the verbosity of your CLI tool using the `--loglevel` parameter. Supported loglevels are the PSR-3 145*ab8e5256SAndreas Gohrloglevels and our own `success` level: 146*ab8e5256SAndreas Gohr 147*ab8e5256SAndreas Gohr* debug 148*ab8e5256SAndreas Gohr* info 149*ab8e5256SAndreas Gohr* notice 150*ab8e5256SAndreas Gohr* success (this is not defined in PSR-3) 151*ab8e5256SAndreas Gohr* warning 152*ab8e5256SAndreas Gohr* error 153*ab8e5256SAndreas Gohr* critical 154*ab8e5256SAndreas Gohr* alert 155*ab8e5256SAndreas Gohr* emergency 156*ab8e5256SAndreas Gohr 157*ab8e5256SAndreas Gohr 158*ab8e5256SAndreas Gohr 159*ab8e5256SAndreas GohrConvenience methods for all log levels are available. Placeholder interpolation as described in PSR-3 is available, too. 160*ab8e5256SAndreas GohrMessages from `warning` level onwards are printed to `STDERR` all below are printed to `STDOUT`. 161*ab8e5256SAndreas Gohr 162*ab8e5256SAndreas GohrThe default log level of your script can be set by overwriting the `$logdefault` member. 163*ab8e5256SAndreas Gohr 164*ab8e5256SAndreas GohrSee `example/logging.php` for an example. 165