The other day around the office, I was surprised to discover not everyone realizes that you can do some crazy stuff around extending drush commands.
First step: Read the documentation. Drush is intentionally written to be a command-line foil for Drupal, and if you’ve built a module, a few minutes digging into the well-done docs will go a long way. Check out the Drush API if you are of a mind, or for a more focused burst of hook goodness, get out your terminal and type:
$> drush topic docs-api
Once you have a grasp of the fine hooks you can use to piggy-back on and around any Drush command, you can take it to the next level.
Adding options to a Drush command allows you to create more sophisticated, controllable extensions to core command behaviors.
There are several ways of doing this.
The command allows this already
Let’s not make this too complicated. Commands are sometimes intended as passthru mechanisms, such as
drush rsync. How does it accept any rsynch command? It has a command definition that includes
allow-additional-options => TRUE.
The user operates in non-strict mode
If the developer can set a flag, so can the user! You might have already seen this warning:
Unknown option: --simple. See `drush help help` for available options. To suppress this error, add the option --strict=0.
This means that any time the user adds
--strict=0 to a drush command invocation (or to their drushrc file for that specific command), your drush code can use
drush_get_option() to access the submitted value.
But for the clever command extension…
You’ll want to implement
hook_drush_help_alter(). Using this hook, you can add new options to a drush command that will be be qualified as “strict”, and will even show up in the help entry for the command in the user’s system.
Why didn’t you show source code?
Well, I had this small idea in mind, but it evolved rather quickly into a legitimate project. Go check out Drush Notify. It’s pretty Ubuntu-specific out the gate, but it serves as a great example of a powerful drush “extension” that defines no commands.