The {--order}
option (without an =
sign) declares a switch option, which takes no arguments. If the switch option is present, its value equals true
, and, when absent, false
(--help
is like a switch—no argument needed).
When we provide an argument on the command line for this option, the console framework cannot match the input to an option with an argument, so it throws the error as shown in the question.
要允许选项接受参数,请将命令的$signature
更改为:
protected $signature = 'order:check {--order=}'
Note the addition of the equal sign after --order
. This tells the framework that the --order
option requires an argument—the command will throw an exception if the user doesn't provide one.
如果我们希望命令接受选项with or without作为参数,我们可以使用类似的语法来提供默认值:
protected $signature = 'order:check {--order=7}'
...but this doesn't seem useful for this particular case.
设置好后,我们可以调用命令,传递一个参数--order
.该框架支持两种格式:
$ php artisan order:check --order=7
$ php artisan order:check --order 7
...and then use the value of order
in our command:
$orderNumber = $this->option('order'); // 7