Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii console command custom help information

Tags:

php

yii

I created file commands/TestCommand.php in my yii-powered project:

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

And it's became visible via 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>

If I am trying to get some help information about this console command:

php yiic.php help test

I see default text:

Usage: yiic.php test index

How can I write my TestCommand class which will show my help information? Is it a some public field or special method which return help text? I need something like that:

php yiic.php help webapp

Result like I need:

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.
like image 780
itnelo Avatar asked May 06 '26 17:05

itnelo


1 Answers

You can override the default getHelp method to implements your help!

It must return a string that is the help text.

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

Here is the default method:

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;
}

You can also override the default getOptionHelp method that is called in getHelp

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

Source

like image 63
darkheir Avatar answered May 08 '26 07:05

darkheir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!