toolkit / cli-utils
some cli tool library of the php
Installs: 81 351
Dependents: 5
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >7.1.0
Requires (Dev)
- phpunit/phpunit: ^7.5
Suggests
- inhere/console: a lightweight php console application library.
README
Provide some useful utils for the php CLI.
- Parse CLI arguments and options
- Console color render
- CLI code highlighter
- Build simple CLI application
- CLI env information helper
Install
composer require toolkit/cli-utils
Console color
Color::printf('<info>%s</info> world', 'hello'); Color::println('hello world', 'info'); Color::println('hello world', 'error'); Color::println('hello world', 'warning'); Color::println('hello world', 'success'); echo Color::render('hello world', 'success');
PHP file highlight
This is inspire jakub-onderka/php-console-highlighter
use Toolkit\Cli\Highlighter; // this is an comment $rendered = Highlighter::create()->highlight(file_get_contents(__FILE__)); \Toolkit\Cli\Cli::write($rendered);
Parse CLI arguments and options
use Toolkit\Cli\Flags; $argv = $_SERVER['argv']; // notice: must shift first element. $script = \array_shift($argv); // do parse list($args, $shortOpts, $longOpts) = Flags::parseArgv($argv);
Build CLI application
You can quickly build an simple CLI application:
use Toolkit\Cli\App; // create app instance $app = new App([ 'desc' => 'this is my cli application', ]);
Register commands
Use closure:
$app->addCommand('test', function ($app) { echo "args:\n"; /** @var Toolkit\Cli\App $app */ /** @noinspection ForgottenDebugOutputInspection */ print_r($app->getArgs()); }, 'the description text for the command: test');
Use closure with config:
$app->addByConfig(function ($app) { echo "args:\n"; /** @var Toolkit\Cli\App $app */ /** @noinspection ForgottenDebugOutputInspection */ print_r($app->getArgs()); }, [ 'name' => 'cmd2', 'desc' => 'the description text for the command: test', ]);
Use an object:
class MyCommand { public function getHelpConfig(): array { $help = <<<STR Options: --info Output some information Example: {{fullCmd}} STR; return [ 'name' => 'list', 'desc' => 'list all directory name in src/', 'help' => $help, ]; } public function __invoke(App $app) { echo "hello\n"; } } // add command $app->addObject(new MyCommand);
Run application
// run $app->run();
Run demo: php example/liteApp
CLI downloader
use Toolkit\Cli\Download; $url = 'http://no2.php.net/distributions/php-7.2.5.tar.bz2'; $down = Download::file($url, ''); // $down->setShowType('bar'); $down->start();
Progress bar output:
Connected...
Mime-type: text/html; charset=utf-8
Being redirected to: http://no2.php.net/distributions/php-7.2.5.tar.bz2
Connected...
FileSize: 14280 kb
Mime-type: application/octet-stream
[========================================> ] 40% (3076/7590 kb)
Progress text output:
Download: http://no2.php.net/distributions/php-7.2.5.tar.bz2
Save As: /path/to/php-7.2.5.tar.bz2
Connected ...
Got the file size: 14280 kb
Found the mime-type: application/octet-stream
Made some progress, downloaded 641 kb so far