PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 4, 2022

[FIXED] Laravel 5 command - Mandatory options

 March 04, 2022     laravel-5, laravel-artisan, php     No comments   

Issue

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?


Solution

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



Answered By - Kjell
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing