thinkphp8 定时任务 addArgument

在ThinkPHP8中,我们可以使用addArgument方法来添加命令行参数。这个方法允许我们定义命令行参数,并且可以指定参数的模式(例如:是否必须,是否可选)。

以下是一个简单的例子,演示如何在ThinkPHP8的命令行中添加一个参数:

php 复制代码
<?php
namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Hello extends Command
{
    protected function configure()
    {
        $this->setName('hello')
        	->addArgument('name', Argument::OPTIONAL, "your name")
            ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
        	->setDescription('Say Hello');
    }

    protected function execute(Input $input, Output $output)
    {
    	$name = trim($input->getArgument('name'));
      	$name = $name ?: 'thinkphp';

		if ($input->hasOption('city')) {
        	$city = PHP_EOL . 'From ' . $input->getOption('city');
        } else {
        	$city = '';
        }
        
        $output->writeln("Hello," . $name . '!' . $city);
    }
}

这个文件定义了一个叫hello的命令,并设置了一个name参数和一个city选项。

bash 复制代码
// 无需任何参数
php think hello
// 输出默认: Hello thinkphp!

// 添加命令参数
php think hello kancloud
// 输出: Hello kancloud!

// 添加city选项
php think hello kancloud --city shanghai
// 输出: 
    Hello kancloud!
    From shanghai
相关推荐
似水明俊德1 小时前
02-C#.Net-反射-面试题
开发语言·面试·职场和发展·c#·.net
Thera7772 小时前
C++ 高性能时间轮定时器:从单例设计到 Linux timerfd 深度优化
linux·开发语言·c++
炘爚3 小时前
C语言(文件操作)
c语言·开发语言
阿蒙Amon3 小时前
C#常用类库-详解SerialPort
开发语言·c#
凸头3 小时前
CompletableFuture 与 Future 对比与实战示例
java·开发语言
wuqingshun3141593 小时前
线程安全需要保证几个基本特征
java·开发语言·jvm
Moksha2623 小时前
5G、VoNR基本概念
开发语言·5g·php
jzlhll1234 小时前
kotlin Flow first() last()总结
开发语言·前端·kotlin
W.D.小糊涂4 小时前
gpu服务器安装windows+ubuntu24.04双系统
c语言·开发语言·数据库
用头发抵命4 小时前
Vue 3 中优雅地集成 Video.js 播放器:从组件封装到功能定制
开发语言·javascript·ecmascript