I have a command with this signature
order:check {--order}
And execute this:
php artisan order:check --order 7
For some reason that results in this exception
[RuntimeException]
Too many arguments, expected arguments "command".
Why? I want that this command can either be executed as php artisan order:check
or with an optional order id php artisan order:check --order X
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.
To allow the option to accept an argument, change the command's $signature
to:
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.
If we want our command to accept an option with or without an argument, we can use a similar syntax to provide a default value:
protected $signature = 'order:check {--order=7}'
...but this doesn't seem useful for this particular case.
After we set this up, we can call the command, passing an argument for --order
. The framework supports both formats:
$ 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