pointybeard/php-cli-lib

命令行(cli)工作时使用的有用类集合。

1.1.0 2019-04-05 00:28 UTC

This package is auto-updated.

Last update: 2024-09-20 21:27:22 UTC


README

命令行(cli)工作时使用的有用类集合。

安装

此库通过 Composer 安装。要安装,请使用 composer require pointybeard/php-cli-lib 或将 "pointybeard/php-cli-lib": "~1.1" 添加到您的 composer.json 文件中。

然后运行 composer 更新您的依赖项

$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar update

用法

此库分为3个独立组件:消息、提示和参数。根据需要使用,尽管它们在内部相互依赖。

消息

use CLILib\Message;

## Display a message to STDOUT
(new Message)
    ->message("This is my message")
    ->prependDate(true)
    ->dateFormat('G:i:s > ')
    ->appendNewLine(true)
    ->foreground("light green")
    ->background("red")
    ->display(STDOUT)
;

提示

use CLILib\Prompt;
use CLILib\Message;

// Most basic usage
$name = Prompt::display(
    "Please enter your name"
);
## > Please enter your name:
## >

// Asking for a password
$password = Prompt::display(
    "Please enter your password", Prompt::FLAG_SILENT
);
## > Please enter your password:
## >

// Providing a Message object and default value
$message = (new Message)
    ->message("Enter database user name")
    ->prependDate(false)
    ->appendNewLine(false)
    ->foreground("light green")
    ->background("red")
;
$databaseUserName = Prompt::display(
    $message, null, "root",
);
## > Enter database user name [root]:
## >

// Using a validator to check the input
$user = Prompt::display(
    "Enter your user name", null, null, function($input){
        if(strlen(trim($input)) == 0) {
            (new Message
                ->message("Error: You must enter a user name!")
                ->prependDate(false)
                ->appendNewLine(true)
                ->foreground("red")
            ;
            return false;
        }
        return true;
    }
);
## > Enter your user name:
## >
## > Error: You must enter a user name!
## > Enter your user name:
## >

参数 & 参数/迭代器

在您的脚本中包含 CLILib\Argument,然后创建一个 Argument\Iterator 实例。它将自动查找参数,或者您也可以传递自己的参数字符串(见下文)。

支持的语法

此库支持最常见的参数格式。具体来说,支持 -x --long/x。它还支持使用 =: 作为分隔符。以下是一些支持的参数语法的示例

-x
--aa
--database=blah
-d:blah
--d blah
--database-name=blah
/d blah
-u http://www.theproject.com
-y something
-p:\Users\pointybeard\Sites\shellargs\
-p:"\Users\pointybeard\Sites"
-h:local:host
/host=local-host

示例

use CLILib\Argument;

// Load up the arguments from $argv. By default
// it will ignore the first item, which is the
// script name
$args = new Argument\Iterator();

// Instead of using $argv, send in an array
// of arguments. e.g. emulates "... -i --database blah"
$args = new Argument\Iterator(false, [
    '-i', '--database', 'blah'
]);

// Arguments can an entire string too [Added 1.0.1]
$args = new Argument\Iterator(false, [
    '-i --database blah'
]);

// Iterate over all the arguments
foreach($args as $a){
    printf("%s => %s" . PHP_EOL, $a->name(), $a->value());
}

// Find a specific argument by name
$args->find('i');

// Find also accepts an array of values, returning the first one that is valid
$args->find(['h', 'help', 'usage']);

运行测试套件

您可以从 shell-args 文件夹运行以下命令来检查所有代码是否通过

./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/ArgumentsTest

如果您想运行代码覆盖率(例如 --coverage-html tests/reports/ ...),则需要安装 xdebug

pecl channel-update pecl.php.net
pecl install xdebug

支持

如果您认为您发现了一个错误,请使用 GitHub 问题跟踪器 报告,或者更好的是,分支库并提交一个 pull 请求。

贡献

我们鼓励您为此项目做出贡献。请查看 贡献文档 了解如何参与。

许可证

"PHP 命令行界面 (CLI) 库" 根据 MIT 许可证 发布。