1. Go to this page and download the library: Download b-hayes/cli library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
b-hayes / cli example snippets
(new \BHayes\CLI\CLI( $yourClass ))->run();
public $cats;
if($this->cats) { echo "Cat mode enabled!"; }
#!/usr/bin/env php
// 👆 important 👇
ample.
$yourClass = new Class() {
function hello(int $number = 0) {
if ($number > 10) throw new \BHayes\CLI\UserErrorResponse("$number is too big for me!");
if ($number) return "You gave me the number $number";
return 'Hi ' . \BHayes\CLI\CLI::prompt('Enter your name', `git config user.name`);
}
};
(new \BHayes\CLI\CLI( $yourClass ))->run();
throw new \BHayes\CLI\UserResponse('This has an exit code of 1 and no coloured output');
throw new \BHayes\CLI\UserErrorResponse('Exit code 1 and text is printed in RED');
throw new \BHayes\CLI\UserWarningResponse('Exit code 1 and text is printed in YELLOW');
//IMPORTANT: all responses have an exit code of 1 by default except this one 👇
throw new \BHayes\CLI\UserSuccessResponse('Exit code 0 and text is printed in GREEN');
throw new \BHayes\CLI\UserResponse('Printer failed!',\BHayes\CLI\Colour::BG_LIGHT_MAGENTA, '🖨🔥', 221);
$cli = new CLI($class, [
MyCustomException::class,
SomeSpecificThirdPartyException::class,
]);
$cli = new CLI($class, [], true);
$cli = new CLI($class, [], true);
global $argv; //this is a built-in var where php puts command line inputs
$argv[] = '--debug'; //manually add the --debug input as if the user typed it
$cli->run();
$argv[] = '--debug'; //this will get passed to your application but have no effect on CLI
$cli->run(false);//because debug has been explicitly disabled at run time.