创建自定义指令操作步骤:
第一步:运行指令
php think make:command Auto auto
即可看到在 app\command 目录生成的 Auto.php
修改里面的代码:
<?php declare (strict_types = 1); 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 Auto extends Command { protected function configure() { // 命令行参数 $this->addArgument('a', Argument::REQUIRED); // 必须参数 $this->addArgument('b', Argument::REQUIRED); // 必须参数 $this->setName('auto'); $this->setDescription('生成模块'); } protected function execute(Input $input, Output $output) { $a = $input->getArgument('a'); $b = $input->getArgument('b'); echo $a.'+'.$b; } }
第二步:修改 config/console.php 文件
<?php return [ 'commands' => [ 'auto' => 'app\command\Auto', ] ];
第三步:测试
php think auto 100 200
结果: