itnelo
itnelo

Reputation: 1103

yii console command custom help information

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.

Upvotes: 3

Views: 1270

Answers (2)

Jeremy French
Jeremy French

Reputation: 12187

A simpler solution is to use docmentation comments.

For example for an option

   /**
     * @var bool whether to enable ANSI color in the output.
     * If not set, ANSI color will only be enabled for terminals that support it.
     */
    public $color;

or for the command itself

   /**
     * Flushes given cache components.
     *
     * For example,
     *
     * ```
     * # flushes caches specified by their id: "first", "second", "third"
     * yii cache/flush first second third
     * ```
     */
    public function actionFlush()

If you look at the default actions you should see how this works and be able to emulate it.

Upvotes: 0

darkheir
darkheir

Reputation: 8950

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

Upvotes: 4

Related Questions