官网文档
https://www.kancloud.cn/manual/thinkphp6_0/1037651
创建命令类文件
运行指令创建一个自定义命令类文件
php think make:command Hello hello
生成内容如下
<?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);
}
}
配置config/console.php文件
<?php
return [
'commands' => [
'hello' => 'app\command\Hello',
]
];
使用命令执行指令
php think hello