命令遇到错误时显示帮助文本

时间:2015-07-07 16:47:21

标签: php laravel laravel-5

我有一个工作指令。我想为它添加一些选项。这些选项在技术上都不是必需的,但至少最多其中1个需要为命令的任何单个调用提供。我已经设置了选项,可以成功检测到这些情况。

但是,每当检查失败时,我希望显示该命令的帮助对话框。如果我的命令是my:command,我希望在我的错误消息之后显示运行$ php artisan my:command -h的输出。

我试过这样做:

$this->error('my error message');

// Causes infinite failness since it is calling the command but not passing 
// options (or ignoring it), thus hitting the same error condition
$this->call('my:command', ['-h' => true]); 

// Causes.. nothing. No error, no blank line, I just see my error message
Artisan::call('help', ['command_name' => 'my:command', 'format' => 'txt']);

// Both of these also cause nothing to be displayed. They are from:
// Symfony\Component\Console\Command\Command
echo $this->getHelp();
echo $this->getProcessedHelp();

有没有办法获取命令的相同格式化帮助文本或触发在遇到类似错误时显示的帮助消息?

更新

运行$ php artisan help my:command时命令的默认输出是这样的:

Usage:
my:command [options] [--] [<argument>]

Options:
  -h, --help             Display this help message
  -q, --quiet            Do not output any message
  -V, --version          Display this application version
      --ansi             Force ANSI output
      --no-ansi          Disable ANSI output
  -n, --no-interaction   Do not ask any interactive question
      --env[=ENV]        The environment the command should run under.
  -v|vv|vvv, --verbose   Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

我想在输出错误信息后输出。

1 个答案:

答案 0 :(得分:3)

要在错误后显示帮助消息,请在错误消息行

之后添加此消息
$this->call('help', ['command_name' => 'test:mycommand', 'format' => 'raw']);