php - yii 控制台命令自定义帮助信息

标签 php yii

我在我的 yii 支持的项目中创建了文件 commands/TestCommand.php:

class TestCommand extends CConsoleCommand
{
    public function actionIndex()
    {
        echo "Hello World!\n";
    }
}

它通过 yiic 变得可见:

Yii command runner (based on Yii v1.1.14)
Usage: yiic.php <command-name> [parameters...]

The following commands are available:
 - message
 - migrate
 - shell
 - test     <<<
 - webapp

To see individual command help, use the following:
   yiic.php help <command-name>

如果我试图获取有关此控制台命令的一些帮助信息:

php yiic.php help test

我看到默认文本:

Usage: yiic.php test index

如何编写将显示我的帮助信息的 TestCommand 类?它是返回帮助文本的某些公共(public)字段还是特殊方法?我需要这样的东西:

php yiic.php help webapp

我需要的结果:

USAGE
  yiic webapp <app-path> [<vcs>]

DESCRIPTION
  This command generates an Yii Web Application at the specified location.

PARAMETERS
 * app-path: required, the directory where the new application will be created.
   If the directory does not exist, it will be created. After the application
   is created, please make sure the directory can be accessed by Web users.
 * vcs: optional, version control system you're going to use in the new project.
   Application generator will create all needed files to the specified VCS
   (such as .gitignore, .gitkeep, etc.). Possible values: git, hg. Do not
   use this argument if you're going to create VCS files yourself.

最佳答案

您可以覆盖默认的 getHelp 方法来实现您的帮助!

它必须返回一个字符串,即帮助文本。

Provides the command description. This method may be overridden to return the actual command description.

这是默认方法:

public function getHelp()
{
    $help='Usage: '.$this->getCommandRunner()->getScriptName().' '.$this->getName();
    $options=$this->getOptionHelp();
    if(empty($options))
        return $help."\n";
    if(count($options)===1)
        return $help.' '.$options[0]."\n";
    $help.=" <action>\nActions:\n";
    foreach($options as $option)
        $help.='    '.$option."\n";
    return $help;
}

您还可以覆盖 getHelp 中调用的默认 getOptionHelp 方法

Provides the command option help information. The default implementation will return all available actions together with their corresponding option information.

Source

关于php - yii 控制台命令自定义帮助信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24952264/

相关文章:

javascript - polymer 铁型输出误差

php - 如何在mysql中选择日期范围?

php - Yii - 获取自动递增 id

yii - 创建新迁移时未定义 CConsoleApplication.defaultController

php - 将 mysql 数据库从实时服务器备份到我的本地 MySQL

php - 从 SELECT PHP 设置变量

php - 增加 MySQL 查询的深度

yii - 如何替换 Yii 中的元标记?

yii - 使用 CDbCriteria 搜索

php - Yii2 - 获取不带参数的应用程序 URL