Laravel Artisan自定义命令:能否为选项添加--help帮助说明?
为Laravel Artisan自定义命令的选项添加帮助说明
完全可以给自定义Artisan命令的特定选项添加--help可见的帮助描述,Laravel原生就支持这个功能,具体操作如下:
1. 在命令签名中直接添加选项描述
在自定义命令类的$signature属性里,给目标选项加上冒号和描述文本,格式为{--选项名=: 帮助描述内容}。
示例代码:
<?php namespace App\Console\Commands; use Illuminate\Console\Command; class CustomTaskCommand extends Command { /** * 命令的名称与签名 * * @var string */ protected $signature = 'custom:task {--task=: 指定要执行的任务类型,可选值:backup、sync、cleanup}'; /** * 命令的整体描述 * * @var string */ protected $description = '执行自定义业务任务'; /** * 执行命令逻辑 * * @return int */ public function handle() { $taskType = $this->option('task'); // 编写任务执行逻辑 return Command::SUCCESS; } }
2. 验证效果
运行命令的--help参数,就能看到选项的专属帮助说明:
php artisan custom:task --help
输出内容中会显示:
Options:
--task= 指定要执行的任务类型,可选值:backup、sync、cleanup
-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
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
额外格式说明
- 带默认值的选项:格式为
{--task=默认值: 帮助描述} - 开关型选项(无需传值):格式为
{--task: 帮助描述},比如{--force: 强制执行操作,跳过确认步骤}
内容的提问来源于stack exchange,提问作者Skittles
相关产品推荐
相关产品推荐

