Back to top

Helpful functions for writing Drush commands

There are lots of great tutorials out there for how to use drush, but not as many about how to write Drush commands. I periodically dig in and find useful drush functions, but then forget them a month later when I'm writing a new drush command.

So, here is a helpful list of Drush-specific functions that will make your life easier.

  • drush_log - note that the second argument is the type of notice and it can be a 'notice' by default or 'warning', 'error', or 'success'.
  • drush_confirm - ask people if they want to do something. Users can override this by adding a "-y"
  • drush_prompt - prompt the user for some input like a filename or a date value
  • drush_choice - send the user a list of prompts like the 'drush cc' command presents you a list of caches you might want to clear out.
  • drush_user_abort - break out of a command if a user aborts the operation.
  • drush_get_option - check if the user provided an option on the command line like -h
  • drush_set_error - throws an error so the user sees it and the drush script ends with an exit status of 1
  • drush_print - prints out information

Some helpful resources:

People Involved: