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
To create a new artisan command, we can use the make:command artisan command. This command will make a new command class within the app/Console/Commands catalog. In case the directory does not exist in our laravel project, it'll be automatically made the primary time we run the artisan make:command command.
Tinker allows you to interact with your entire Laravel application on the command line, including your Eloquent models, jobs, events, and more. To enter the Tinker environment, run the tinker Artisan command: php artisan tinker.
What is a REPL? REPL stands for Read—Eval—Print—Loop, which is a type of interactive shell that takes in single user inputs, evaluates them, and returns the result to the user.
To call another Artisan command and save its output you should use $this->call() from your command.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With