fusepump/cli.php

PHP CLI 辅助工具

此包的官方仓库似乎已不存在,因此该包已被冻结。

0.5.2 2013-03-19 11:31 UTC

This package is not auto-updated.

Last update: 2024-01-20 09:27:44 UTC


README

一组用于辅助 CLI 工具的 PHP 类。

Build Status

安装

将以下内容添加到您的 composer.json

{
    "require": {
        "fusepump/cli.php": "0.5.0"
    }
}

然后运行

composer install

并将 require 'vendor/autoload.php' 添加到您的 PHP 文件中;

测试

要运行测试

phpunit

内容

输入

解析命令行输入

示例

use FusePump\Cli\Inputs as Inputs;

$cli = new Inputs($argv);

$cli->option('-h, --ham', 'Add ham');
$cli->option('-m, --mayo', 'Add mayonaise');
$cli->option('-c, --cheese [type]', 'Add a cheese');
$cli->option('-b, --bread [type]', 'Type of bread', true); // required input

$cli->param('when', 'When do you want the sandwhich (now, tomorrow or never)', true);

if(!$cli->parse()) {
    exit(1);    
}

echo "You ordered a sandwich ".$cli->get('when')." with: \n";
if($cli->get('-h')) echo " - Ham \n";
if($cli->get('-m')) echo " - Mayonaise \n";
if($cli->get('--cheese')) echo ' - '.$cli->get('--cheese')." cheese \n";
echo 'On '.$cli->get('-b')." bread \n";

运行

php cli.php now -h -c cheddar --bread white

给出

You ordered a sandwich now with:
 - Ham
 - Cheddar cheese
On white bread

提示

提示用户输入信息。

示例

use FusePump\Cli\Inputs as Inputs;

$cli = new Inputs();

$username = $cli->prompt('Username: ');
echo 'Got '.$username."\n";

$password = $cli->prompt('Password (not a real one): ', true);
echo 'Got '.$password."\n";

$confirm = $cli->confirm('Confirm? ');
echo var_dump($confirm);

注意 确认返回 "y" 或 "yes" 时为真。否则返回假。

日志记录器

具有颜色和时间戳的日志记录器类。

示例

use FusePump\Cli\Logger as Logger;

Logger::log('Hello!'); 
// => [2012-11-15 18:12:34] [log] [logging.php] Hello!

Logger::log('This is red!', array('colour' => 'red')); 
// => [2012-11-15 18:12:34] [log] [logging.php] This is red!

Logger::log('This is green!', array('colour' => 'green')); 
// => [2012-11-15 18:12:34] [log] [logging.php] This is green!

Logger::log('Custom formatting options!', array('format' => '[%s] %s', 'inputs' => array('custom_log'))); 
// => [Custom log] Custom formatting options!

Logger::error('This is an error'); 
// => [2012-11-15 18:12:34] [error] [logging.php] This is an error

Logger::out('Plain output'); 
// => Plain output

Logger::out('Plain output with colour!', 'red'); 
// => Plain output with colour!

工具

工具函数集合。

exec

执行 shell 命令。如果命令失败,则抛出异常。

示例

use FusePump\Cli\Utils as Utils;

Utils::exec('echo hello!'); // => hello!

$value = Utils::exec('echo hello!', true);
echo $value; // => hello!

jsonDecode

将 JSON 字符串解码为关联数组。如果解析 JSON 字符串失败,则抛出包含信息的异常。

示例

$json = FusePump\Cli\Utils::jsonDecode('{"hello": "world"}');

echo $json['hello']; // => world

checkEnv

检查环境变量是否已设置。可以接受单个变量或要设置的变量数组。如果变量未设置,则抛出异常。

示例

use FusePump\Cli\Utils as Utils;

Utils::checkEnv(array(
    'FOO',
    'BAR'
));
    
Utils::checkEnv('FOO');

pregMatchArray

将主题与正则表达式模式数组进行匹配。如果找到匹配项,则返回真。如果没有找到,则返回假。

示例

use FusePump\Cli\Utils as Utils;

Utils::pregMatchArray(array(
    '/foo/i',
    '/bar/i'
), 'foo'); // => true

hexDump

以十六进制形式转储一些数据。

示例

use FusePump\Cli\Utils as Utils;

echo Utils::hexDump('This is a string of data'); 

// =>
//   0 : 54 68 69 73 20 69 73 20 61 20 73 74 72 69 6e 67 [This is a string]
//  10 : 20 6f 66 20 64 61 74 61 [ of data]

计时器

计时代码块。

示例

use FusePump\Cli\Timer as Timer;

$clock = new Timer();

$clock->start('total');

$clock->start('block1');
sleep(2);
$clock->stop('block1');

// Averages
for($i = 0; $i < 5; $i++) {
    $rand = rand(0, 10);
    $clock->startAvg('loop');
    sleep($rand);
    $clock->stopAvg('loop');
}

$clock->stop('total');

$block1 = $clock->get('block1');
// =>
//  Array
//  (
//      [start] => 1355855924.4966
//      [start-line] => 12
//      [stop] => 1355855926.4968
//      [stop-line] => 14
//      [total] => 2.0002529621124
// )


$avg = $clock->getAvg('loop');
// =>
//  Array
//  (
//      [count] => 5
//      [total] => 2.3014297485352
//      [start] => 1355855931.3992
//      [start-line] => 23
//      [stop] => 1355855931.7995
//      [stop-line] => 25
//      [avg] => 0.46028594970703
//  )


$clock->report();
// =>  
//  Timing report:
//      total (10-28): 7.0035 seconds
//
//  Averages:
//      loop [5] (23-25): 4.0034 seconds


$clock->report('total');
// =>    
//  Timing report:
//      total (10-28): 7.0035 seconds

报告格式

{{block name}} ({{lines block wraps}}): {{time taken}} seconds

平均报告格式

{{block name}} [{{number of iterations]}] ({{lines block wraps}}): {{time taken}} seconds

您还可以启用或禁用时钟以节省资源

$clock = new Timer(false); // disable in the constructor

$clock->disable(); // disable after class has been instantiated

$clock->enable(); // enable class again

$clock->getEnabled(); // => true

许可证

MIT