railt/console

此包已被废弃,不再维护。没有推荐替代包。

Railt 控制台组件

dev-master 2018-09-14 14:16 UTC

This package is not auto-updated.

Last update: 2018-10-23 19:14:16 UTC


README

Railt

Travis CI Code coverage Scrutinizer CI Latest Stable Version Latest Unstable Version License MIT

安装

  • 使用composer安装此包。
composer require railt/console
  • 将发现事件添加到您的composer.json中。
{
    "scripts": {
         "post-autoload-dump": [
             "Railt\\Console\\Manifest::discover"
         ]
     }
}

用法

只需运行vendor/bin/railt(或Windows OS的vendor/bin/railt.bat)命令。

为了更方便的使用,您可以将编译后的内容放入本地目录

ln -s ./vendor/railt/console/railt ./railt

命令

让我们创建我们的第一个命令。

use Railt\Console\Command;

class ExampleCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';
    
     /**
      * The console command description.
      *
      * @var string
      */
    protected $description = 'Console command description';
    
     /**
      * Create a new command instance.
      *
      * @return void
      */
    public function handle()
    {
        // Command execution code
    }
}

参数

所有用户提供的参数和选项都包含在大括号中。在以下示例中,命令定义了一个必需的参数:user

protected $signature = 'example:command {user}';

您还可以使参数可选,并为参数定义默认值

// Optional argument...
protected $signature = 'example:command {user?}';
// Optional argument with default value...
protected $signature = 'example:command {user=foo}';

选项

选项,就像参数一样,是另一种用户输入形式。当在命令行指定时,选项以两个短横线(--)为前缀。

有两种类型的选项:接收值的选项和不接收值的选项。不接收值的选项充当布尔“开关”。让我们看看这个类型选项的示例

protected $signature = 'example:command {user} {--force}';

在这个示例中,当调用Artisan命令时,可以指定--force开关。如果传递了--force开关,则选项的值将为true。否则,值将为false

php railt example:command 42 --force

带有值的选项

接下来,让我们看看期望值的选项。如果用户必须为选项指定值,则在选项名称后附加一个=符号

protected $signature = 'example:command {user} {--dir=}';

在这个示例中,用户可以像这样传递选项值

php railt example:command 42 --dir=./example/path

您可以通过在选项名称后指定默认值来为选项分配默认值。如果用户没有传递选项值,则将使用默认值

protected $signature = 'example:command {user} {--dir=./some/any}';

选项快捷键

在定义选项时,如果要分配快捷键,请在选项名称之前指定它,并使用|分隔符将快捷键与完整选项名称分开

protected $signature = 'example:command {user} {--F|force}';

输入数组

如果您想要定义期望数组输入的参数或选项,可以使用*字符。首先,让我们看看一个指定数组参数的示例

protected $signature = 'example:command {user*}';

调用此方法时,用户参数可以按顺序传递到命令行。例如,以下命令将user的值设置为['foo', 'bar']

php railt example:command foo bar

当定义期望数组输入的选项时,传递给命令的每个选项值应以前缀选项名称开头

protected $signature = 'example:command {--id=*}';
php railt example:command --id=1 --id=2

输入描述

您可以通过使用冒号将参数与描述分开来为输入参数和选项分配描述。如果您需要更多空间来定义命令,请随时将定义分散到多行

/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = 'example:command
                        {user : The ID of the user}
                        {--force : Force a command execution}';

发现

"发现"系统提供了使用composer配置框架的能力。

命令

只需添加到命令类的链接。在这种情况下,命令将被自动添加到控制台railt可执行文件中。

控制台命令类应该是Symfony或Railt命令的实例。

{
    "extra": {
        "railt": {
            "commands": [
                "Path\\To\\ConsoleCommand",
                "..."
            ]
        }
    }
}

异常

所有通过railt二进制文件运行的控制台命令都有更好的控制台异常处理。

./resources/errors.png

您可以手动使用此API。

use Railt\Console\Highlighter;
use Railt\Console\ExceptionRenderer;
use Railt\Console\Language\PHPLanguage;

/**
 * Code Highlighter
 */
$highlight = new Highlighter();
$highlight->add(new PHPLanguage()); // Add support of PHP code highlight

/**
 * Renderer
 */
$renderer = new ExceptionRenderer($highlight);
$result = $renderer->render(new \Exception('example'));

// $output should be instance of Symfony OuputInterface
$output->write($result);