Submitted by greggles on
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:
- The drush.ws website is of course full of useful information
- The Commands doc page ships with drush and is available on drush.ws
- The drush.api.php file - more of an inline-comments-as-docs page
- Codekarate has an article on adding drush integration to your modules
- BuildAModule.com video on writing drush commands
- Drupalize.me has a few videos on drush
- Log in to post comments