在Laravel工匠命令中使用verbose

Ant*_*ony 16 verbosity laravel artisan

有没有办法检测用户在创建自定义工匠命令时指定的详细级别?我在文档中没有看到任何关于它的内容.

luk*_*ter 25

getVerbosity()功能,Symfony\Component\Console\Output\OutputInterface您可以使用它$this->getOutput()来检索输出对象.

$verbosityLevel = $this->getOutput()->getVerbosity();
Run Code Online (Sandbox Code Playgroud)

然后,您可以将级别与内部定义的常量进行比较OutputInterface.例如:

if($verbosityLevel >= OutputInterface::VERBOSITY_VERBOSE){
    // show verbose messages
}
Run Code Online (Sandbox Code Playgroud)

  • 你也可以使用`$ this-> output-> isVerbose()`[api.symfony.com/3.1/...](http://api.symfony.com/3.1/Symfony/Component/Console/Output/ OutputInterface.html#method_isVerbose).`isVerbose()`是`-v`,`isVeryVerbose()`是`-vv`,`isDebug()`是`-vvv`. (6认同)
  • 太好了!我会给它一个旋转.我还发现$ this-> option('verbose')可以为你提供一个简单的布尔值.(它不包括-v vs -vvv,但是) (3认同)

小智 8

您可以根据文档使用不同的详细程度:

https://laravel.com/api/5.6/Illuminate/Console/OutputStyle.html#method_isQuiet

isQuiet()       - no verbosity is set                                   (no option set)
isVerbose()     - if the level is quiet or verbose                      (-v)
isVeryVerbose() - if the level is very verbose, verbose or quiet        (-vv)
isDebug()       - if the level is debug, very verbose, verbose or quiet (-vvv)
Run Code Online (Sandbox Code Playgroud)

例如在您的命令中 $this->getOutput()->isQuiet()

这也会影响writeLn()。如果要编写$this->line('Serious message', null, 'vv');,则消息将显示为-vv-vvv选项,但不会显示为和-v静默模式,因为对于那些级别的日志记录,该消息“太详细”。