10quality/ayuco

可以使用PHP编写的命令的命令行界面。

v1.0.5 2023-02-08 08:56 UTC

This package is auto-updated.

Last update: 2024-09-08 12:32:09 UTC


README

Latest Stable Version Total Downloads License

可以使用PHP编写的命令的命令行界面。

注意:本包中包含的命令(不包括帮助命令)是为WordPress-MVC编写的。

用法

创建一个将在命令行中调用的PHP文件,例如示例,并包含以下代码行

use Ayuco\Listener;

创建监听器

$ayuco = new Listener();

// or without use
$ayuco = new Ayuco\Listener()

注册您的命令。

$ayuco->register($command1)
    ->register($command2)
    ->register(new MyCommand);

开始解释或监听

$ayuco->interpret();

在命令行中使用

php filename command_key arguments

如果filename命名为ayuco.php并且command_keyclear_cache,则命令行中的命令将是

php ayuco.php clear_cache

参数和命令选项

将参数直接发送到command_key之后,例如

php ayuco.php cache clear 

在上面的示例中,cache将是命令键,而clear将是参数(按照顺序,它将是$arg[2])。

使用前缀--将参数作为命令选项发送,例如

php ayuco.php cache clear --debug --note="Cache clear note"

创建自定义命令

通过从Ayuco基命令类扩展来创建您自己的类命令

use Ayuco\Command;

class MyCommand extends Command
{
    protected $key = 'command_key';

    protected $description = 'My command description.';

    public function call($args = [])
    {
        // TODO command action.
    }
}

清除缓存命令的示例。

use Ayuco\Command;

class ClearCacheCommand extends Command
{
    protected $key = 'clear_cache';

    protected $description = 'Clears system cache.';

    public function call($args = [])
    {
        Cache::flush(); // Example
    }
}

监听器中的注册

$ayuco->register(new ClearCacheCommand);

参数和选项

对于此命令

php ayuco.php cache clear 

可以像这样访问参数

use Ayuco\Command;

class CacheCommand extends Command
{
    protected $key = 'cache';

    public function call($args = [])
    {
        // ayuco.php
        $args[0];

        // cache
        $args[1];

        // clear
        $args[2];
    }
}

对于此命令

php ayuco.php cache clear --debug --note="Cache clear note"

可以像这样访问选项

use Ayuco\Command;

class CacheCommand extends Command
{
    protected $key = 'cache';

    public function call($args = [])
    {
        // ayuco.php
        $args[0];

        // cache
        $args[1];

        // clear
        $args[2];

        // --debug
        $this->options['debug'];

        // --note="..."
        $this->options['note'];
    }
}

输出着色

使用类Ayuco\Coloring的静态方法apply()更改控制台打印的输出的着色

use Ayuco\Coloring;
use Ayuco\Command;

class ColoringCommand extends Command
{
    public function call($args = [])
    {
        $this->_print(Coloring::apply('red', 'Print this message in red.'));
    }
}

您可以在此处了解有关着色的更多信息这里

帮助命令

AYUCO将自动注册其自己的help命令。此命令可用于在命令行中显示已注册的命令列表,使用方法如下

php ayuco.php help

要求

  • PHP >= 5.4

编码规范

PSR-4。

许可

MIT许可(MIT)

版权所有(c)2016 10Quality