Implementing Custom Drush Commands

In this post, we cover how to install and create your first custom Drush command

Install Custom Drush Commands

There are two steps to install any drush command:

  1. a) copy drushrc.php from /path/to/drush/example to your $HOME/.drush/ directory if not already present
  2. b) In the drushrc.php specify directory containing your drush cusotm commands
    ($options[‘include’]=/path/to/my/drush/commands
    

This will import the directory where your custom drush function will reside. Lets create one

Implementing Custom Drush Command

Imlementing drush command can be broken into 3 steps:

  1. Create File
    After DRUSH is aware of the location of our custom commands, then we can create any file by extension ‘.drush.inc’, because Drush will load all files with the extension ‘.drush.inc’. Lets, say we have file smile.drush.inc
  2. Declare Command
    Next, lets declare command in smile.drush.inc as following:

    function FILE-NAME_drush_command() {
        $items = array();
        $items['make-me-smile'] = array(
            'description' => "Makes a Happy Smile.",
            'arguments' => array(
                'type' => 'The type of the smile (half_moon, polity, etc.)',
            ),
            'options' => array(
                '--time' => 'specify time of smile (e.g. 10,30 in sec)',
            ),
            'examples' => array(
                'drush smile polity --time=10' => 'Make a great smile that cheers you up for rest of the day.',
            ),
            'aliases' => array('smile'),
            'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap at all.
        );
        return $items;
    }
    

    This declares our custom Drush command – make-me-smile. Important to note, the file-name is the hook in the hook_drush_comand() declaration. The ‘boostrap’ specifies the level of Drush to boot in. Some of other bootstrap levels are ‘DRUSH_BOOTSTRAP_NONE’, ‘DRUSH_BOOTSTRAP_DRUPAL_ROOT’, etc…see /path/to/drush/includes/bootstrap.inc for full list and description

  3. Implement Command
    After we declare the custom command, lets implement it:

    function drush_make_me_smile($type = 'polite'){
        drush_print('Smiling....type:'.$type);
    }
    

    Here the first ‘smile’ is from the file name smile.drush.inc following the name of the drush command – make_me_smile, so the function name is combined into drush_COMMAND_NAME where all the ‘-‘ part of COMMAND-NAME need to be replaced to ‘_’

  4. Test Run

    ○  drush smile --help                                                                                      
    ○  drush smile fake                                                                                       
    Smiling....type:fake
    ○  drush make-me-smile test                                                                               
    Smiling....type:test
    

    here, we run our new custom command in two ways – by alias and by full name

    Troubleshooting

    1. Location of Drush installation

    run:

    which drush
    

    This will display executable of drush. Afterwards, see what directory is linking to

One thought on “Implementing Custom Drush Commands

  1. Pingback: Export Gmail To Scrap Emails via Drush | Margots Kapacs Blog

Leave a Reply

Your email address will not be published. Required fields are marked *