faslatam / consolekit
用于创建命令行工具的库
Requires (Dev)
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: ^10.4
README
用于创建命令行工具的PHP库。
示例
在 cli.php
<?php class HelloCommand extends ConsoleKit\Command { function execute(array $args, array $options = []) { $this->writeln('hello world!', ConsoleKit\Colors::GREEN); } } $console = new ConsoleKit\Console(); $console->addCommand('HelloCommand'); $console->run();
在shell中
php cli.php hello
hello world!
更多示例请见example.php
安装
使用以下要求安装ConsoleKit的最简单方法是使用Composer
composer require faslatam/consolekit
或者,您可以下载存档并将src/文件夹添加到PHP的include路径中
set_include_path('/path/to/src' . PATH_SEPARATOR . get_include_path());
使用方法
选项解析器
默认选项解析器解析类似于argv的数组。项目可以是以下形式
- --key=value
- --key
- -a
- -ab (相当于 -a -b)
当选项没有值时,将使用true。如果指定了多个具有相同键的键值对,则“key”值将是一个包含所有值的数组。
如果检测到"--",则所有后续值将被视为单个参数
示例:字符串“-a -bc --longopt --key=value arg1 arg2 -- --any text”将生成以下两个数组
$args = ['arg1', 'arg2', '--any text']; $options = [ 'a' => true, 'b' => true, 'c' => true, 'longopt' => true, 'key' => 'value' ];
创建命令
任何回调都可以是命令。它将接收三个参数:参数数组、选项数组和控制台对象。
function my_command($args, $opts, $console) { $console->writeln("hello world!"); }
命令也可以定义为类。在这种情况下,它们必须继承自ConsoleKit\Command
并重写execute()
方法。
class MyCommand extends ConsoleKit\Command { function execute(array $args, array $opts) { $this->writeln("hello world!"); } }
ConsoleKit\Command
类提供了辅助方法,更多信息请查看。
注册命令
需要使用addCommand()
方法(或addCommands()
)在控制台对象中注册命令。
$console = new ConsoleKit\Console(); $console->addCommand('my_command'); // the my_command function $console->addCommand('MyCommand'); // the MyCommand class $console->addCommand(function() { echo 'hello!'; }, 'hello'); // using a closure // or: $console->addCommand('hello', function() { echo 'hello!'; }); // alternative when using a closure
注意,在最后一个例子中,我们提供了一个第二个参数,它是命令的别名。由于闭包没有名称,必须指定一个。
函数的命令名称与函数名称相同,其中下划线被替换为破折号(即my_command变为my-command)。
命令类的命令名称是短类名,不带Command
后缀,并且“dashed”(即HelloWorldCommand变为hello-world)。
运行
只需调用控制台对象的run()
方法即可
$console->run(); $console->run(array('custom arg1', 'custom arg2')); // overrides $_SERVER['argv']
自动生成帮助信息
默认情况下,注册了help命令,并提供有关基于文档注释的可用方法的帮助信息。
查看example.php以获取可用标记的示例
php myscript.php help
格式化文本
颜色
ConsoleKit\Colors::colorize()
方法提供了一种简单的方法来着色文本。颜色定义为字符串或整数(通过Colors
类的常量)。
可用颜色:black,red,green,yellow,blue,magenta,cyan,white。
前景颜色也提供“bold”变体。在颜色名称后缀加上“+bold”或使用常量的OR位操作符。
echo Colors::colorize('my red text', Colors::RED); echo Colors::colorize('my red text', 'red'); echo Colors::colorize('my red bold text', Colors::RED | Colors::BOLD); echo Colors::colorize('my red bold text', 'red+bold'); echo Colors::colorize('my red text over yellow background', Colors::RED, Colors::YELLOW);
TextFormater
ConsoleKit\TextFormater
类允许您使用以下选项格式化文本
- 使用
setIndent()
或indent选项进行缩进 - 使用
setQuote()
或quote选项进行引号 - 使用
setFgColor()
或fgcolor选项设置前景颜色 - 使用
setBgColor()
或bgcolor选项设置背景颜色
可以使用setOptions()
或构造函数的第一个参数定义选项。
$formater = new ConsoleKit\TextFormater(array('quote' => ' > ')); echo $formater->format("hello!"); // produces: " > hello"
小部件
对话框
用于与用户交互
$dialog = new ConsoleKit\Widgets\Dialog($console); $name = $dialog->ask('What is your name?'); if ($dialog->confirm('Are you sure?')) { $console->writeln("hello $name"); }
框
将文本包裹在框中
$box = new ConsoleKit\Widgets\Box($console, 'my text'); $box->write();
生成
********************************************
* my text *
********************************************
进度条
显示进度条
$total = 100; $progress = new ConsoleKit\Widgets\ProgressBar($console, $total); for ($i = 0; $i < $total; $i++) { $progress->incr(); usleep(10000); } $progress->stop();