railt / console
Railt 控制台组件
Requires
- php: ^7.1.3
- railt/io: 1.2.*|dev-master
- railt/lexer: 1.2.*|dev-master
- symfony/console: ~3.3|~4.0
Requires (Dev)
- phpunit/phpunit: ^6.5
This package is not auto-updated.
Last update: 2018-10-23 19:14:16 UTC
README
安装
- 使用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
二进制文件运行的控制台命令都有更好的控制台异常处理。
您可以手动使用此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);