toolkit / cli-utils
PHP 的实用 CLI 工具库
v2.0.3
2022-06-15 08:50 UTC
Requires
- php: >8.0.0
Requires (Dev)
- ext-posix: *
- ext-readline: *
Suggests
- inhere/console: a lightweight php console application library.
- toolkit/pflag: Command line flag parse library of the php
This package is auto-updated.
Last update: 2024-09-11 12:59:28 UTC
README
为 PHP CLI 应用程序提供一些有用的实用工具。
- 简单的 CLI 参数和选项解析器。
- 终端控制台着色渲染
- CLI 代码高亮
- 构建简单的 CLI 应用程序
- CLI 环境信息助手
安装
- 需要 PHP 8.0+
composer require toolkit/cli-utils
控制台颜色
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');
控制台日志
use Toolkit\Cli\Util\Clog; // run: php example/log.php foreach (Clog::getLevelNames() as $level) { Clog::log($level, "example log $level message"); }
简单的控制台应用程序
use Toolkit\Cli\CliApp; // run: // php example/mycmd // php example/mycmd -i abc --lon def ag1 ag2 ag3 $cmd = CliApp::new('cmd1', 'this is my cli application'); $cmd->addOpt('info', 'i', 'Output some information'); $cmd->addOpt('long-option-name', 'lon', 'this is a long option for command'); $cmd->addArg('arg1', 'this is first argument'); $cmd->setHandler(function (CliApp $cmd) { var_dump($cmd->getOpts(), $cmd->getArgs(), $cmd->getRemainArgs()); }); $cmd->run();
终端控制
示例
use Toolkit\Cli\Util\Terminal; Terminal::forward(3); Terminal::backward(2); Terminal::clearLine(); Terminal::clearScreen();
控制方法
/** * @method static showCursor() * @method static hideCursor() * @method static savePosition() * @method static restorePosition() * @method static toTop() * @method static toColumn(int $step) * @method static up(int $step = 1) * @method static down(int $step = 1) * @method static forward(int $step = 1) * @method static backward(int $step = 1) Moves the terminal cursor backward * @method static toPrevNLineStart(int $step = 1) * @method static toNextNLineStart(int $step = 1) * @method static coordinate(int $col, int $row = 0) * @method static clearScreen() * @method static clearLine() * @method static clearToScreenBegin() * @method static clearToScreenEnd() * @method static scrollUp(int $step = 1) * @method static scrollDown(int $step = 1) * @method static showSecondaryScreen() * @method static showPrimaryScreen() */
PHP 文件高亮
这是受
jakub-onderka/php-console-highlighter
启发
use Toolkit\Cli\Util\Highlighter; // this is an comment $rendered = Highlighter::create()->highlight(file_get_contents(__FILE__)); \Toolkit\Cli\Cli::write($rendered);
CLI 下载器
use Toolkit\Cli\Download; $url = 'http://no2.php.net/distributions/php-7.2.5.tar.bz2'; $down = Download::file($url, ''); // $down->setShowType('bar'); // $down->setDebug(true); $down->start();
进度条
进度文本
项目
- https://github.com/inhere/php-console 构建丰富的控制台应用程序
- https://github.com/php-toolkit/pflag 通用标志解析库,构建简单的控制台应用程序。