How to create custom Drush commands

Custom Drush Commands for Drupal 8 and Drupal 7It is very easy to create your own custom Drush commands. In this blog post I will show two examples for Drupal 7 and Drupal 8.

Creating custom Drush commands could be very useful when you need to import data into Drupal or export data from Drupal. You would have to create a separate bash script where you would execute your custom command and add the bash script to your crontab.

Drupal 8 example

Drupal 8 module structure:

drush_example/
  - drush_example.info.yml
  - drush_example.drush.inc

drush_example.info.yml:

name: Drush Example command
description: Drupal 8 custom drush command example
type: module
core: 8.x

drush_example.drush.inc

/**
 * Implements hook_drush_command().
 */
function drush_example_drush_command() {

  $commands['my-example-command'] = [
    'description' => 'This is my example command.',
    'aliases' => ['mec'],
    'arguments' => [
       'arg1' => 'My custom argument 1.',
       'arg2' => 'My custom argument 2.',
     ],
     'options' => [
       'opt1' => 'My custom option.',
     ],
     'examples' => [
       'drush mec' => 'Print my example command.',
       'drush mec myargument' => 'Print my example command with an argument "myargument".',
       'drush mec myargument --opt1=myoption' => 'Print my example command with an argument "myargument" and an option "myoption".',
     ],
  ];

  return $commands;
}

/**
 * Drush command logic.
 * drush_[MODULE_NAME]_[COMMAND_NAME]().
 */
function drush_drush_example_my_example_command($arg1 = 'N/A', $arg2 = 'N/A') {
  $opt1 = drush_get_option('opt1', 'N/A');
  $tokens = ['@arg1' => $arg1, '@opt1' => $opt1];
  drush_print(dt('My custom command. Argument: @arg1 Option: @opt1', $tokens));
}

Drupal 7 example

I will use the following structure for the D7 example module:

drush_example/
  - drush_example.info
  - drush_example.module (empty .module file)
  - drush_example.drush.inc

drush_example.info:

name = Drush Example command 
description = Drupal 7 custom drush command example
core = 7.x

drush_example.drush.inc

/**
 * Implements hook_drush_command().
 */
function drush_example_drush_command() {

  $commands['my-example-command'] = array(
    'description' => 'This is my example command.',
    'aliases' => array('mec'),
    'arguments' => array(
       'arg1' => 'My custom argument 1.',
       'arg2' => 'My custom argument 2.',
     ),
     'options' => array(
       'opt1' => 'My custom option.',
     ),
     'examples' => array(
       'drush mec' => 'Print my example command.',
       'drush mec myargument' => 'Print my example command with an argument "myargument".',
       'drush mec myargument --opt1=myoption' => 'Print my example command with an argument "myargument" and an option "myoption".',
     ),
  );

  return $commands;
}

/**
 * Drush command logic.
 * drush_[COMMAND_NAME]().
 */
function drush_my_example_command($arg1 = 'N/A', $arg2 = 'N/A') {
  $opt1 = drush_get_option('opt1', 'N/A');
  $tokens = array('@arg1' => $arg1, '@opt1' => $opt1);
  drush_print(dt('My custom command. Argument: @arg1 Option: @opt1', $tokens));
}