How to use a custom drush command to help us debug?

Backgroud

  1. Create a drush command is very very very simple! (Do not believe it? Let me show you how later, take a cup of coffee and wait please).
  2. Some developers prefer CLI than open the browser, type a url, enter and watch the output of the dpm() (provided by the Devel module).

I will show you how to debug with a custom drush command. This drush command comes with a custom module.

Create a minimal module foo.

file foo.info:

name = Foo
description = the Foo module
core = 7.x

file foo.module:

<?php

/**
 * @file
 * The foo.module file 
 */

/**
 * The function have a debug, do not eat it without a drush command.
 */
function foo_bar(){
  // Buggy code goes here.
}

Create a minimal drush command foo.

file foo.drush.inc

<?php

/**
 * @file
 * The foo.drush.inc file.
 */

/**
 * Implements hook_drush_command().
 */
function foo_drush_command(){
  return array(
    'foo' => array(
       'description' = 'I'm foo, not fool, eat buuug'
     ),
  );
}

/**
 * The default callback of command foo.
 */
function drush_foo(){
   foo_bar();
}

Happying debugging.