Php 使用Laravel LIGHTED Console命令的参数和选项

Php 使用Laravel LIGHTED Console命令的参数和选项,php,laravel,console,Php,Laravel,Console,如何在Laravel中为CLI创建一个简单的命令非常清楚,这也适用于创建参数,但我似乎找不到关于如何为CLI创建选项的简单文档 我想这样做,用户可以在参数后添加选项。 例如: php artisan cmd参数-a php artisan cmd参数-a-c-x 我如何在下面的类中实现这样的结构 更新代码 确实有一些可能的解决办法。这其实很容易 class cmd extends Command { /** * The name and signature of the con

如何在Laravel中为CLI创建一个简单的命令非常清楚,这也适用于创建参数,但我似乎找不到关于如何为CLI创建选项的简单文档

我想这样做,用户可以在参数后添加选项。 例如:

php artisan cmd参数-a

php artisan cmd参数-a-c-x

我如何在下面的类中实现这样的结构

更新代码 确实有一些可能的解决办法。这其实很容易

class cmd extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cmd {argument} 
                           {--s : description}
                           {--x : description}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * 
     * @return mixed
     */
    public function handle()
    {

       $var = $this->argument('argument');
       $options = $this->options();

       new Main($var,$options);
    }
}

对此有很多可能的解决方案,但我更喜欢添加可选参数,如果它们存在,使用
执行确定操作,这意味着参数可以存在或不存在,加上
*
这意味着可以更简单,如下所示:

class cmd extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cmd {argument} {-extra*?}';


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * 
     * @return mixed
     */
    public function handle()
    {

       $var = $this->argument('argument');
       if($this->argument('-extra')) {
         //do things if -extra argument exists, it will be an array with the extra arguments value...
       }

       new Main($var);
    }
}

有一个完整的文档部分专门用于从头开始创建命令,并且有很好的文档记录。仔细看看

如果您需要从示例中学习,那么可以在这里查看laravel的所有内置控制台命令

vendor/laravel/framework/src/Illuminate/Foundation/Console