1,以為下方圖片為例,在api模塊下創建了一個command目錄,然后創建自己所需要的命令行執行文件
我的執行文件未Online.php

以下為代碼實例
```php
<?php
namespace app\api\command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use think\console\Command;
/**
* @package app\api\command
*/
class Online extends Command
{
/**
* 配置指令
*/
protected function configure()
{
$this->addArgument('task_id', Argument::OPTIONAL);
$this->addArgument('sex', Argument::OPTIONAL);
//此處為獲取執行命令的參數,并且定義參數key
$this->setName('online:member')->setDescription('online member');
}
protected function execute(Input $input, Output $output)
{
$args = $input->getArguments();
$task_id=$args['task_id'];
$sex=$args['sex'];
}
}
```
> 代碼解讀
> 此處需要繼承thinkphp的command類,才能讓你的執行文件加載
> configure 為配置方法
> $this->addArgument('task_id', Argument::OPTIONAL);為獲取命令中的參數
> 比如 php think online:member 1 2
> 那么第一個 task_id=1
> 第二個 task_id=2
> execute 執行方法
> $args = $input->getArguments(); 獲取定義的參數
然后在application/你的模塊目錄下創建command.php
這些搞定后,找到thinkphp config目錄

直接 return你的命名空間即可使用了
```php
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
return ['app\api\command\Online'];
```
那么就讓我們來一起試一試吧