Easyswoole源码分析-7-help

介绍

所有command类都实现了CommandInterface接口

interface CommandInterface
{
    public function commandName():string;
    public function exec(array $args):?string ;
    public function help(array $args):?string ;
}

分析

CommandRunner类里面当输入不存在的command时,会默认为help

function run(array $args):?string 
{
       ···
        if(!CommandContainer::getInstance()->get($command)){
            $command = 'help';
        }
       ···
}

help类

class Help implements CommandInterface
{

    public function commandName(): string
    {
        // TODO: Implement commandName() method.
        return 'help';
    }

    public function exec(array $args): ?string
    {
        // TODO: Implement exec() method.
        // 如果没有任何command,则直接调用本类的help方法
        if (!isset($args[0])) {
            return $this->help($args);
        } else {
            $actionName = $args[0];
            array_shift($args);
            // 获取相应命令的对象
            $call = CommandContainer::getInstance()->get($actionName);
            // 是否实现了CommandInterface接口
            if ($call instanceof CommandInterface) {
                // 执行相应command类的help方法
                return $call->help($args);
            } else {
                return "no help message for command {$actionName} was found";
            }
        }
    }

    public function help(array $args): ?string
    {
        // TODO: Implement help() method.
        // 获取所有command
        $allCommand = implode(PHP_EOL, CommandContainer::getInstance()->getCommandList());
        // 展示es log
        $logo = Utility::easySwooleLog();
        return $logo.<<<HELP
Welcome To EASYSWOOLE Command Console!
Usage: php easyswoole [command] [arg]
Get help : php easyswoole help [command]
Current Register Command:
{$allCommand}
HELP;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 6,331评论 0 10
  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,712评论 0 5
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,654评论 1 32
  • 梦涯的感悟 相对于无穷无尽的物质能量世界来说,我们看得到的感知得到的宇宙就象一个...
    智慧启蒙阅读 312评论 0 0
  • 他朝一旁的她深情的看了一眼,而后手呈喇叭状,向着远处大声喊了一句,“我喜欢你。” 远山似没听到这句话,他等待了好半...
    白祁艺love阅读 405评论 0 0

友情链接更多精彩内容