tp5自定义命令行

239 阅读2分钟

一、创建自定义命令行

1,配置command.php文件,目录在application/command.php

<?php
return [
    'app\home\command\Test',//配置命令行路由
];

2,建立命令类文件,新建application/home/command/Test.php

<?php
namespace app\home\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

class Test extends Command
{
    protected function configure()
    {
        $this->setName('test')//定义命令的名字
        ->setDescription('Here is the remark ');//设置描述
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln("TestCommand:");//在命令行界面输出内容
    }
}

这个文件定义了一个叫test的命令,备注为Here is the remark,
执行命令会输出TestCommand。

3,测试-命令帮助-命令行下运行

php think

输出

Think Console version 0.1

Usage: command [options] [arguments]

Options: -h, --help Display this help message -V,
–version Display this console version -q, --quiet Do not output any message
–ansi Force ANSI output
–no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question -v|vv|vvv, --verbose Increase the
verbosity of messages: 1 for normal output, 2 for more verbose output
and 3 for debug

Available commands: build Build Application Dirs
clear Clear runtime file help Displays

help for a command list Lists commands test
Here is the remark make make:controller Create a new resource
controller class make:model Create a new model class
optimize optimize:autoload Optimizes PSR0 and PSR4 packages to be
loaded with classmaps too, good for production. optimize:config
Build config and common file cache. optimize:route Build route
cache. optimize:schema Build database schema cache.

4,运行test命令

php think test

输出

TestCommand:

二、创建带参数自定义命令行

1,配置command.php文件,目录在application/command.php

<?php
return [
    'app\home\command\Test',//配置命令行路由
];

2,建立命令类文件,新建application/home/command/Test.php

<?php
namespace app\home\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

class Test extends Command
{
    protected function configure()
    {
        $this->setName('test')//定义命令的名字
        ->setDescription('Here is the remark ')   //设置描述
        ->addArgument('name')                      //增加一个名字参数
        ->addArgument('age');                      //增加一个年龄参数
    }

    protected function execute(Input $input, Output $output)
    {
         //获取输入的参数
        $name = $input->getArgument('name');
        $age = $input->getArgument('age');
        //输出获得的参数
        $output->writeln("My name is $name ,age is $age");
    }
}

这个文件定义了一个叫test的命令,备注为Here is the remark,
执行命令会输出TestCommand。

3,运行test命令

php think test wuhen 20

输出

My name is wuhen,age is 20

三、创建带操作选项的自定义命令行

1,配置command.php文件,目录在application/command.php

<?php
return [
    'app\home\command\Test',//配置命令行路由
];

2,建立命令类文件,新建application/home/command/Test.php

<?php
namespace app\home\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

class Test extends Command
{
    protected function configure()
    {
        $this->setName('test')//定义命令的名字
        ->setDescription('Here is the remark ')   //设置描述
        ->addArgument('number1')                      //增加一个参数
        ->addArgument('number2')                      //增加一个参数
        ->addOption('add')                          //定义相加的选项
        ->addOption('sub');                         //定义相减的选项
    }

    protected function execute(Input $input, Output $output)
    {
         //获取输入的2个参数
        $number1 = $input->getArgument('number1');
        $number2 = $input->getArgument('number2');
        
        //加法操作
        if($input->hasOption('add')){
            $result = $number1 + $number2;
            $output->writeln("$number1 + $number2 = $result");
        }
        
        //减法操作
        if($input->hasOption('sub')){
            $result = $number1 - $number2;
            $output->writeln("$number1 - $number2 = $result");
        }
    }
}

3,运行test命令

php think calculate 20 30 --add

输出

20 + 30 = 50

php think calculate 20 30 --sub

输出

20 - 30 = -10

选项也是可以设置默认值和执行命令时添加值的,例如

<?php
protected function configure()
    {
        $this->setName('test')
        	 ->addArgument('route', Argument::OPTIONAL, "your run  route path! ") 
            ->addOption(  "port", null, Option::VALUE_REQUIRED,  '', 9501 )
            ->setDescription('test is a test function ');
			//addOption(  "指定选项名", "选项名别名", Option::选项类型(VALUE_NONE=没有输入值,VALUE_REQUIRED=输入值必填,VALUE_OPTIONAL=可选,VALUE_ARRAY=数组),  '选项说明', 默认值)
			//addArgument('指定参数名', Argument::类型同上,没有VALUE_, "默认值") 
    }

    protected function execute(Input $input, Output $output)
    {
    
        $Argument = $input->getArguments();//获取所有参数
        $port = $input->getOption('port');
        $output->writeln("TestCommand:".$port);
    }

执行命令

php think test --port

或者执行命令

 php think test --port=555