• tp5自定义命令行


    一、创建自定义命令行

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

    
    return [
        'app\home\command\Test',//配置命令行路由
    ];
    
    • 1
    • 2
    • 3
    • 4

    2,建立命令类文件,新建application/home/command/Test.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:");//在命令行界面输出内容
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

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

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

    php think
    
    • 1

    输出

    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
    
    • 1

    输出

    TestCommand:

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

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

    
    return [
        'app\home\command\Test',//配置命令行路由
    ];
    
    • 1
    • 2
    • 3
    • 4

    2,建立命令类文件,新建application/home/command/Test.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");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

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

    3,运行test命令

    php think test wuhen 20
    
    • 1

    输出

    My name is wuhen,age is 20

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

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

    
    return [
        'app\home\command\Test',//配置命令行路由
    ];
    
    • 1
    • 2
    • 3
    • 4

    2,建立命令类文件,新建application/home/command/Test.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");
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    3,运行test命令

    php think calculate 20 30 --add
    
    • 1

    输出

    20 + 30 = 50

    php think calculate 20 30 --sub
    
    • 1

    输出

    20 - 30 = -10

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

    
    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);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    执行命令

    php think test --port
    
    • 1

    或者执行命令

     php think test --port=555
    
    • 1
  • 相关阅读:
    python3实现灰度图的双三次插值算法缩放
    SpringBoot整合MQTT总结
    Windows10/11 缩放与布局自定义
    卷积神经网络——vgg16网络及其python实现
    【JavaEE】Servlet(创建Maven、引入依赖、创建目录、编写及打包、部署和验证、smart Tomcat)
    前端跨域问题的解决思路
    springBoot -md
    Mahony 滤波算法参数自动调节方法 11
    Docker实战之Redis主从集群搭建实战
    安装Anaconda之后cmd打不开
  • 原文地址:https://blog.csdn.net/wangzhae/article/details/126396536