skip to Main Content

I was working on creating a command and wanted to get all options, so I getOptions() in my code, but it always returned an empty array.

After doing some debugging, I found out that this getOptions method is defined in the HasParameters trait and is ALWAYS an empty array; I wonder what its purpose? I couldn’t find anything about it in the documentation. Is that a Laravel thing? Or a Symfony one?

2

Answers


  1. As you said, getOptions comes from HasParameters, and you should see that HasParameters is in IlluminateConsoleConcerns namespace, so it is 100% laarvel…

    So, what you want to use is: $this->options(), it will return an array of options…

    Login or Signup to reply.
  2. When creating a command you can provide parameters:

    protected $signature = 'app:test {user} {test=} {--force}';
    

    this signature contains:

    • arguments: user and test= ($this->argument(‘user’) and $this->argument(‘test=’))

      ex. php artisan app:test 1 test=mail

    • options: force ($this->option(‘force’))

      ex. php artisan app:test 1 test=mail --force

    separately, get all: $this->options() and $this->arguments()

    upd. as docs says:

    The getArguments() and getOptions() methods are where you may define
    any arguments or options your command receives. Both of these methods
    return an array of commands, which are described by a list of array
    options.

    actually, if you creates some command just forget about those functions

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search