davek1312 / console
v0.8.1
2017-04-02 14:59 UTC
Requires
- davek1312/app: 0.2.*
- davek1312/variableutils: 0.3.*
- symfony/console: ^3.2
Requires (Dev)
- phpunit/phpunit: ~4.0
README
symfony/console 包的包装器。
安装
此包可在 Packagist 上找到,您可以使用 Composer 安装它。
composer require davek1312/console
创建命令
创建您的命令类并实现 process()
<?php
namespace MyApp\Commands;
use Davek1312\Console\Command;
class MyCommand extends Command {
protected $signature = 'myapp:mycommand {argument} {--option=}';
protected $description = 'My command\'s short description';
protected $help = 'My command\'s full description';
protected function process() {
// MyCommand's code logic
}
}
命令输入
signature
属性定义了命令的参数和选项。
/**
* This command expects one argument for name and with option with a value for age.
*/
protected $signature = 'myapp:mycommand {name} {--age=}';
参数
// Required argument
myapp:mycommand {name}
Console useage: vendor/bin/davek1312-console myapp:mycommand David
// Argument with a description
myapp:mycommand {name : Name's description}
// Optional argument
myapp:mycommand {name?}
Console useage: vendor/bin/davek1312-console myapp:mycommand
// Argument with a default value
myapp:mycommand {name=David}
// Argument where the value is an array
myapp:mycommand {name*}
Console useage: vendor/bin/davek1312-console myapp:mycommand David Paul
// Optional argument where the value is an array
myapp:mycommand {name?*}
选项
// Option where no value is expected. The value is when not present and true when present.
myapp:mycommand {--age}
Console useage: vendor/bin/davek1312-console myapp:mycommand --age
// Option with a description.
myapp:mycommand {--age : description}
// Option with a command line shortcut
myapp:mycommand {--A|age}
Console useage: vendor/bin/davek1312-console myapp:mycommand --A=20
// Option where a value is expected
myapp:mycommand {--age=}
Console useage: vendor/bin/davek1312-console myapp:mycommand --age=20
// Option with a default value
myapp:mycommand {--age=100}
// Option where the value is an array
myapp:mycommand {--age=*}
Console useage: vendor/bin/davek1312-console myapp:mycommand --age=20 --age=30
注册命令
要注册您的命令,请查看 davek1312\app 文档。
调用命令
命令行
vendor/bin/davek1312-console myapp:mycommand argument-value --option=option-value
程序化
在两个示例中都使用 call
返回一个整数返回码。
从另一个命令中
$this->call('myapp:mycommand', [
'argument' => 'argument-value',
'--option' => 'option-value'
]);
从应用程序的任何其他地方
Davek1312\Console\Application::call('myapp:mycommand', [
'argument' => 'argument-value',
'--option' => 'option-value'
]);
控制台输入/输出
提示输入
有几种方式可以请求用户输入
// Ask the user for confirmation
$confirm = $this->inputConfirm('Do you want to continue? [y/n]');
// Ask the user for a value
$name = $this->inputAsk('What is your name?', $default);
// Ask the user for a value from a list of predefined values
$name = $this->inputAskChoice('What is your name?', ['David', 'Paul']);
// Ask the user for a value and their response will be autocompleted from the list of values provided
$name = $this->inputAskAutocomplete('What is your name?', ['David', 'Paul']);
// Ask the user for a value but hide their response as they typre
$password = $this->inputAskHidden('What is your password?');
写入输出
要写入控制台输出,您可以使用以下方法
// "Message" is displayed in default style
$this->outputLine('Message');
// "Message" is displayed in green text
$this->outputInfo('Message');
// "Message" is displayed in yellow text
$this->outputComment('Message');
// "Message" is displayed with text in a blue background
$this->outputQuestion('Message');
// "Message" is displayed with text in a red background
$this->outputError('Message');
进度条
要显示命令的进度,您可以向 ProgressBar
提供一个计数
$count = 3;
$progressBar = $this->getProgressBar($count);
for($i = 0; $i < $count; $i++) {
//Do something
$progressBar->advance();
}
$progressBar->finish();
测试命令
您可以使用以下代码执行命令并获取控制台输出
<?php
use Davek1312\Console\Tests\CommandTest;
$command = CommandTest::getCommand(MyCommand::class,'myapp:mycommand');
/**
* $argumentsAndOptions are the commands defined arguments and options
* $inputs is an array of booleans that answer i/o inputs
*/
CommandTest::getCommandOutput($command, $argumentsAndOptions, $inputs);