CHelpCommand.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * CHelpCommand class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CHelpCommand represents a console help command.
  12. *
  13. * CHelpCommand displays the available command list or the help instructions
  14. * about a specific command.
  15. *
  16. * To use this command, enter the following on the command line:
  17. * <pre>
  18. * php path/to/entry_script.php help [command name]
  19. * </pre>
  20. * In the above, if the command name is not provided, it will display all
  21. * available commands.
  22. *
  23. * @property string $help The command description.
  24. *
  25. * @author Qiang Xue <qiang.xue@gmail.com>
  26. * @package system.console
  27. * @since 1.0
  28. */
  29. class CHelpCommand extends CConsoleCommand
  30. {
  31. /**
  32. * Execute the action.
  33. * @param array $args command line parameters specific for this command
  34. * @return integer non zero application exit code after printing help
  35. */
  36. public function run($args)
  37. {
  38. $runner=$this->getCommandRunner();
  39. $commands=$runner->commands;
  40. if(isset($args[0]))
  41. $name=strtolower($args[0]);
  42. if(!isset($args[0]) || !isset($commands[$name]))
  43. {
  44. if(!empty($commands))
  45. {
  46. echo "Yii command runner (based on Yii v".Yii::getVersion().")\n";
  47. echo "Usage: ".$runner->getScriptName()." <command-name> [parameters...]\n";
  48. echo "\nThe following commands are available:\n";
  49. $commandNames=array_keys($commands);
  50. sort($commandNames);
  51. echo ' - '.implode("\n - ",$commandNames);
  52. echo "\n\nTo see individual command help, use the following:\n";
  53. echo " ".$runner->getScriptName()." help <command-name>\n";
  54. }
  55. else
  56. {
  57. echo "No available commands.\n";
  58. echo "Please define them under the following directory:\n";
  59. echo "\t".Yii::app()->getCommandPath()."\n";
  60. }
  61. }
  62. else
  63. echo $runner->createCommand($name)->getHelp();
  64. return 1;
  65. }
  66. /**
  67. * Provides the command description.
  68. * @return string the command description.
  69. */
  70. public function getHelp()
  71. {
  72. return parent::getHelp().' [command-name]';
  73. }
  74. }