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
相关推荐
科雷软件测试2 小时前
Python中itertools.product:快速生成笛卡尔积
开发语言·python
OOJO3 小时前
c++---list介绍
c语言·开发语言·数据结构·c++·算法·list
笨笨饿5 小时前
29_Z变换在工程中的实际意义
c语言·开发语言·人工智能·单片机·mcu·算法·机器人
艾为电子5 小时前
【技术帖】让接口不再短命:艾为 C-Shielding™ Type-C智能水汽防护技术解析
c语言·开发语言
棉花骑士5 小时前
【AI Agent】面向 Java 工程师的Claude Code Harness 学习指南
java·开发语言
IGAn CTOU5 小时前
PHP使用Redis实战实录2:Redis扩展方法和PHP连接Redis的多种方案
开发语言·redis·php
环黄金线HHJX.6 小时前
TSE框架配置与部署详解
开发语言·python
Vfw3VsDKo6 小时前
Maui 实践:Go 接口以类型之名,给 runtime 传递方法参数
开发语言·后端·golang
Pyeako6 小时前
PyQt5 + PaddleOCR实战:打造桌面级实时文字识别工具
开发语言·人工智能·python·qt·paddleocr·pyqt5
白藏y7 小时前
【C++】muduo接口补充
开发语言·c++·muduo