第一步,创建一个自定义命令类文件,运行指令

php think make:command Hello hello

会生成一个app\command\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);
    }
}

<?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);
    }
}<?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);
    }
}<?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选项。

第二步,配置config/console.php文件

<?php
return [
    'commands' => [
        'hello' => 'app\command\Hello',
    ]
];

第三步,测试-命令帮助-命令行下运行

php think

第四步,运行hello命令

例如:
php think hello
php think hello kancloud
php think hello kancloud --city shanghai