Skip to content
Advertisement

Laravel 5 command – Mandatory options

I am trying to write a laravel command but I can’t have the options to be mandatory.

I do understand that the concept of option is being “optional” but I’d like to have a command in which it is clear which input you are inserting and in no particular order.

i.e. I would like to achieve this, with par2 and par 2 mandatory

$command-abc --par1=value1 --par2=value2

Instead of:

$command-abc value1 value2

So far this is the signature I used:

protected $signature = 'dst-comparison:analyse
                        {--client : Client Name}
                        {--clientId : Client ID}
                        {--recordingId : Client Recording ID}
                        {--CSVFile : Path to the downloaded CSV file from Spotify analytics}
                        {--dataUpdateTo=null : CSV Data will be truncated from this date onwards}';

Following the Laravel documentation (https://laravel.com/docs/5.1/artisan) and this guide: http://code.tutsplus.com/tutorials/your-one-stop-guide-to-laravel-commands–net-30349 it seemed that overwriting the getOptions method was doing the trick, but it is not working for me.

/**
 * Get the console command options.
 *
 * @return array
 */
protected function getOptions()
{
    return array(
        array('client', null, InputOption::VALUE_REQUIRED, 'Client Name'),
        array('clientId', null, InputOption::VALUE_REQUIRED, 'Client ID'),
        array('recordingId', null, InputOption::VALUE_REQUIRED, 'Client Recording ID'),
        array('CSVFile', null, InputOption::VALUE_REQUIRED, 'Path to the downloaded CSV file from Spotify analytics'),
        array('dataUpdateTo', null, InputOption::VALUE_OPTIONAL, 'CSV Data will be truncated from this date onwards')
    );
}

Any ideas?

Advertisement

Answer

I think you will have to take care of mandatory input yourself. Have a look at the various output functions $this->error(...) and check if all necessary input was given $this->option('client'); (<- returns null if no input was defined)

https://laravel.com/docs/master/artisan

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement