commands.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <h1>Creating Custom Drush Commands</h1>
  2. <p>
  3. Creating a new drush command is very easy. There are
  4. four simple steps.
  5. <ol>
  6. <li>Create a command file called COMMANDFILE.drush.inc
  7. <li>Implement the function COMMANDFILE_drush_help(). Optional.
  8. <li>Implement the function COMMANDFILE_drush_command()
  9. <li>Implement the functions that your commands will call.
  10. These will usually be named drush_COMMANDFILE_COMMANDNAME().
  11. </ol><p>
  12. For an example drush command, see examples/sandwich.drush.inc.
  13. The steps for implementing your command are explained in more
  14. detail below.
  15. <h2>Create COMMANDFILE.drush.inc</h2>
  16. <p>
  17. The name of your drush command is very important. It must end
  18. in ".drush.inc" to be recognized as a drush command. The part
  19. of the filename that comes before the ".drush.inc" becomes the
  20. name of the commandfile. Your commandfile name is used by
  21. drush to compose the names of the functions it will call, so
  22. choose wisely.
  23. <p>
  24. The example drush command, 'make-me-a-sandwich', is stored
  25. in the 'sandwich' commandfile, 'sandwich.drush.inc'.
  26. <p>
  27. Drush searches for commandfiles in the following locations:
  28. <ul>
  29. <li>The "/path/to/drush/commands" folder.
  30. <li>Folders listed in the 'include' option (see `drush topic docs-configuration`).
  31. <li>The system-wide drush commands folder, e.g. /usr/share/drush/commands
  32. <li>The ".drush" folder in the user's HOME folder.
  33. <li>All modules in the current Drupal installation
  34. </ul> <p>
  35. Note that modules in the current Drupal installation will only
  36. be considered if drush has bootstrapped to at least the DRUSH_BOOSTRAP_SITE
  37. level. Usually, when working with a Drupal site, drush will
  38. bootstrap to DRUSH_BOOTSTRAP_FULL; in this case, only the drush
  39. commandfiles in enabled modules will be considered eligible for
  40. loading. If a drush only bootstraps to DRUSH_BOOTSTRAP_SITE,
  41. though, then all drush commandfiles will be considered, whether the
  42. module is enabled or not. See `drush topic docs-bootstrap` for
  43. more information on bootstrapping.
  44. <p>
  45. Additionally, drush commandfiles may optionally define a function
  46. COMMANDFILE_drush_load() in the file COMMANDFILE.drush.load.inc.
  47. If this function returns FALSE, then the commandfile will not be loaded.
  48. <h2>Implement COMMANDFILE_drush_help()</h2>
  49. <p>
  50. The drush_help hook is an optional place to describe a command in long form. If
  51. the command only requires a brief description, use the description key in
  52. COMMANDFILE_drush_command(). The drush_help hook for the 'sandwich' commandfile looks
  53. like this:
  54. <pre>
  55. function sandwich_drush_help($section) {
  56. switch ($section) {
  57. case 'drush:make-me-a-sandwich':
  58. return dt("... brief help summary goes here ...");
  59. }
  60. }
  61. </pre><p>
  62. Note that the command name is prepended with 'drush:' in
  63. the drush_help hook. Your commandfile may implement
  64. multiple commands; to do so, just add more 'case' statements
  65. to the switch, one for each command.
  66. <h2>Implement COMMANDFILE_drush_command()</h2>
  67. <p>
  68. The drush_command hook is the most important part of the
  69. commandfile. It returns an array of items that define
  70. how your commands should be called, and how they work.
  71. Drush commands are very similar to the Drupal menu system.
  72. The elements that can appear in a drush command definition
  73. are shown below.
  74. <ul>
  75. <li>'aliases':
  76. Provides a list of shorter names for the command.
  77. For example, pm-download may also be called via `drush dl`.
  78. If the alias is used, drush will substitute back in the
  79. primary command name, so pm-download will still be used
  80. to generate the command hook, etc.
  81. <li>'deprecated-aliases':
  82. Works just like 'aliases', but does not
  83. appear in help. Used in instances where the drush
  84. maintainers intend to eventually remove support for a
  85. command alias. If someone runs a drush command using a
  86. deprecated alias, drush will print a warning message.
  87. <li>'command hook':
  88. Change the name of the function drush will
  89. call to execute the command from drush_COMMANDFILE_COMMANDNAME()
  90. to drush_COMMANDFILE_COMMANDHOOK(), where COMMANDNAME is the
  91. original name of the command, and COMMANDHOOK is the value
  92. of the 'command hook' item.
  93. <li>'callback':
  94. Name of function to invoke for this command. The callback
  95. function name _must_ begin with "drush_commandfile_", where commandfile
  96. is from the file "commandfile.drush.inc", which contains the
  97. commandfile_drush_command() function that returned this command.
  98. Note that the callback entry is optional; it is preferable to
  99. omit it, in which case drush_invoke() will generate the hook function name.
  100. <li>'callback arguments':
  101. An array of arguments to pass to the callback.
  102. The command line arguments, if any, will appear after the
  103. callback arguments in the function parameters.
  104. <li>'description':
  105. Description of the command.
  106. <li>'arguments':
  107. An array of arguments that are understood by the command.
  108. Used by `drush help` only.
  109. <li>'options':
  110. An array of options that are understood by the command.
  111. Used by `drush help` only.
  112. <li>'examples':
  113. An array of examples that are understood by the command.
  114. Used by `drush help` only.
  115. <li>'scope':
  116. One of 'system', 'project', 'site'. Not currently used.
  117. <li>'bootstrap':
  118. Drupal bootstrap level. Valid values are:
  119. <ul>
  120. <li>DRUSH_BOOTSTRAP_DRUSH
  121. <li>DRUSH_BOOTSTRAP_DRUPAL_ROOT
  122. <li>DRUSH_BOOTSTRAP_DRUPAL_SITE
  123. <li>DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION
  124. <li>DRUSH_BOOTSTRAP_DRUPAL_DATABASE
  125. <li>DRUSH_BOOTSTRAP_DRUPAL_FULL
  126. <li>DRUSH_BOOTSTRAP_DRUPAL_LOGIN
  127. <li>DRUSH_BOOTSTRAP_MAX
  128. </ul>
  129. See `drush topic docs-bootstrap`.
  130. <li>'core':
  131. Drupal major version required. Append a '+' to indicate 'and later versions.'
  132. <li>'drupal dependencies':
  133. Drupal modules required for this command.
  134. <li>'drush dependencies':
  135. Other drush commandfiles required for this command.
  136. <li>'topics':
  137. Provides a list of topic commands that are related in
  138. some way to this command. Used by `drush help` only.
  139. <li>'topic':
  140. Set to TRUE if this command is a topic, callable from the
  141. `drush docs-topics` command.
  142. </ul><p>
  143. The 'sandwich' drush_command hook looks like this:
  144. <pre>
  145. function sandwich_drush_command() {
  146. $items = array();
  147. $items['make-me-a-sandwich'] = array(
  148. 'description' => "Makes a delicious sandwich.",
  149. 'arguments' => array(
  150. 'filling' => 'The type of the sandwich (turkey, cheese, etc.)',
  151. ),
  152. 'options' => array(
  153. 'spreads' => 'Comma delimited list of spreads (e.g. mayonnaise, mustard)',
  154. ),
  155. 'examples' => array(
  156. 'drush mmas turkey --spreads=ketchup,mustard' => 'Make a terrible-tasting sandwich that is lacking in pickles.',
  157. ),
  158. 'aliases' => array('mmas'),
  159. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap at all.
  160. );
  161. return $items;
  162. }
  163. </pre><p>
  164. Most of the items in the 'make-me-a-sandwich' command
  165. definition have no effect on execution, and are used only
  166. by `drush help`. The exceptions are 'aliases' (described
  167. above) and 'bootstrap'. As previously mentioned,
  168. `drush topic docs-bootstrap` explains the drush bootstrapping
  169. process in detail.
  170. <h2>Implement drush_COMMANDFILE_COMMANDNAME()</h2>
  171. <p>
  172. The 'make-me-a-sandwich' command in sandwich.drush.inc
  173. is defined as follows:
  174. <pre>
  175. function drush_sandwich_make_me_a_sandwich($filling = 'ascii') {
  176. ... implementation here ...
  177. }
  178. </pre><p>
  179. If a user runs `drush make-me-a-sandwich` with no command line
  180. arguments, then drush will call drush_sandwich_make_me_a_sandwich()
  181. with no function parameters; in this case, $filling will take on
  182. the provided default value, 'ascii'. (If there is no default
  183. value provided, then the variable will be NULL, and a warning
  184. will be printed.) Running `drush make-me-a-sandwich ham` will
  185. cause drush to call drush_sandwich_make_me_a_sandwich('ham'). In
  186. the same way, commands that take two command line arguments can
  187. simply define two functional parameters, and a command that takes
  188. a variable number of command line arguments can use the standard
  189. php function func_get_args() to get them all in an array for easy
  190. processing.
  191. <p>
  192. Note that drush will actually call a sequence of functions before
  193. and after your drush command function. One of these hooks is the
  194. "validate" hook. The 'sandwich' commandfile provides a validate
  195. hook for the 'make-me-a-sandwich' command:
  196. <pre>
  197. function drush_sandwich_make_me_a_sandwich_validate() {
  198. $name = posix_getpwuid(posix_geteuid());
  199. if ($name['name'] !== 'root') {
  200. return drush_set_error('MAKE_IT_YOUSELF', dt('What? Make your own sandwich.'));
  201. }
  202. }
  203. </pre><p>
  204. The validate function should call drush_set_error and return
  205. its result if the command cannot be validated for some reason.
  206. See `drush topic docs-policy` for more information on defining
  207. policy functions with validate hooks, and `drush topic docs-api`
  208. for information on how the command hook process works. Also,
  209. the list of defined drush error codes can be found in
  210. `drush topic docs-errorcodes`.