php Laravel Artisan自定义命令-有没有办法为选项创建帮助上下文?

bcs8qyzn  于 12个月前  发布在  PHP
关注(0)|答案(1)|浏览(116)

有没有人知道是否有一种方法可以为您在自定义artisan命令的签名中创建的任何特定命令选项创建--help上下文?
我创建了一个artisan命令,可以使用{--task=}选项。我希望为用户提供的是一种方法,让他们调用--help布尔标志之前的选项的帮助描述。
我知道你现在可以在工匠中完成这一点,如果你这样做的话;

php artisan make:controller --help

这将显示一些关于make:controller命令的帮助文本。那么,这也可以为命令选项实现吗?

rjee0c15

rjee0c151#

--help命令在对自定义命令运行时,将输出如下内容:

Description:
  // Whatever the value of `protected $description = '...';` is

Usage:
  // Whatever the value of `protected $signature = '...';` is
  // (with additional options truncated)

Options:
  // Additional Options as defined in `protected $signature = '...';`

  // All Default options available to each `php artisan ...` command
  -h, --help            Display help for the given command. When no command is given display help for the list command
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi|--no-ansi  Force (or disable --no-ansi) 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

如果您将命令定义为:

<?php

namespace App\Console\Commands;

class MyCutomCommand extends Command {
  protected $signature = 'my-custom-command {--task : Valid Options: A, B, C}';
  protected $description = 'This is a Custom Command';

  public function handle() {
    // Command Logic...
  }
}

当你运行php artisan my-custom-command --help时,你会看到上面的内容,或者更具体地说:

在你的例子中,如果你的有效选项是动态的(或者太长),你可以这样调整你的代码:

private $validOptions = ['Long', 'List', 'of', 'Options'];

public function __construct() {
  // $validOptions = DB::table('valid_options')->pluck('name');, etc etc
  
  $this->signature = 'my-custom-command {--task : Valid Options: ' . implode(', ', $this->validOptions) . '}';

  parent::__construct();
}

文件:
https://laravel.com/docs/10.x/artisan#input-descriptions

相关问题