ekowabaka / clearice
一个用于帮助构建 CLI 应用程序的工具。提供命令行解析和简单的控制台 I/O 接口。
v3.2.1
2022-12-17 14:23 UTC
Requires
- php: >=7.1
Requires (Dev)
- mikey179/vfsstream: @stable
- phpunit/phpunit: @stable
This package is auto-updated.
Last update: 2024-09-09 00:46:26 UTC
README
ClearIce 为 PHP 应用程序提供工具,帮助解析命令行参数和执行交互式 I/O 会话。命令行或通过 shell 提供的参数将被验证并以一致的格式提供给您的脚本。而且,您猜怎么着,还可以为您的应用程序自动生成帮助信息。
安装
尽管您可以直接将 ClearIce 脚本包含到您的应用程序中,但最佳安装方式是通过 composer。
composer require ekowabaka/clearice
ClearIce 除了需要一个 7.1 版本或更高版本的 PHP 解释器外,没有其他依赖。
使用 ClearICE 解析参数
让我们从一个例子开始。如果您想解析命令行参数,可以将 ...
<?php require "vendor/autoload.php"; $parser = new \clearice\argparser\ArgumentParser(); $parser->addOption([ 'name' => 'input', 'short_name' => 'i', 'type' => 'string', 'required' => true ]); $parser->addOption([ 'name' => 'output', 'short_name' => 'o', 'type' => 'string', 'default' => '/default/output/path' ]); $options = $parser->parse($argv); print_r($options);
... 放在文件中(例如,保存为 wiki.php)。然后当您执行 ...
php wiki.php generate --input=/home/james --output=/var/www/cool-wiki
... 输出将是 ...
Array
(
[input] => /input/path
[output] => /output/path
[__executed] => wiki.php
)
... 以及以下内容
php test.php --input /input/path --output /output/path
php test.php -i/input/path -o/output/path
使用 ClearICE 进行交互式 I/O
如果您对交互式 I/O 感兴趣,输入以下内容
use clearice\io\Io; $io = new Io(); $name = $io->getResponse('What is your name', ['default' => 'No Name']); $direction = $io->getResponse("Okay $name, where do you want to go", [ 'required' => true, 'answers' => array('north', 'south', 'east', 'west') ] );
可能引发如下交互
What is your name [No Name]: ⏎
Okay No Name, where do you want to go (north/south/east/west) []: ⏎
A value is required.
Okay No Name, where do you want to go (north/south/east/west) []: home⏎
Please provide a valid answer.
Okay No Name, where do you want to go (north/south/east/west) []:
希望您喜欢使用 ClearIce!