多参数控制台命令不存在参数时间

Argument time does not exist on multi argument console command

Ι 在我的 laravel 项目中执行了以下命令:

namespace App\Console\Commands;

use Illuminate\Console\Command;

class Covid19CloseTimeOverride extends Command
{

    protected $signature   = 'store:covid19:override {time: Time that goverment closes the restaurants} {ids*: The list of store Ids that close on the provided time} ';
    protected $description = "Overrides store's close times if measures against Covid19 are applied on a specific area";

    public function handle()
    {
        $time      = $this->argument('time');
        $store_ids = $this->argument('store_ids');

        dump($time,$store_ids);
    }
}

一旦我 运行 在控制台中执行以下命令:

  php artisan store:covid19:override 20:00 52 1234 66 77

我收到以下错误:

  Too many arguments, expected arguments "command" "time: Time that goverment closes the restaurants" "store_ids*: The list of store Ids that close on the provided time".  

你知道我为什么会收到错误消息以及如何解决它吗?

问题出在你的签名上。

当前签名是:

store:covid19:override {time: Time that goverment closes the restaurants} {ids*: The list of store Ids that close on the provided time}

意思是你的参数是:

  • time: Time that goverment closes the restaurants
  • ids*: The list of store Ids that close on the provided time

因此,为了便于访问,您应该使用以下 argument 调用来获取它们:

        $time=$this->argument('time: Time that goverment closes the restaurants');
        $store_ids=$this->argument('ids*: The list of store Ids that close on the provided time');

是的,它有点像 PITA,因此为了使您之前的代码正常工作:

        $time      = $this->argument('time');
        $store_ids = $this->argument('store_ids')

在参数和 : 之间放置一个 space 产生这个命令签名:

store:covid19:override {time : Time that goverment closes the restaurants} {ids* : The list of store Ids that close on the provided time}