Drupal 7 – Add validation on your Drush command

Previously:

 

We could add validation to our drush command. What we need to do is to add the validation function. Let’s once again update our eureka_drush.drush.inc.

<?php

/**
 * Implementation of hook_drush_command().
 */
function eureka_drush_drush_command() {
  $items = array();
  
  $items['say-hello'] = array(
    'description' => 'Say Hello to someone',
    'drupal dependencies' => array('eureka_drush'),
  );
  
  return $items;
} 

/**
 * Call back function for say-hello.
 * The function name is drush_<module_name>_<command_in_underscore>
 */
function drush_eureka_drush_say_hello() {
  $args_count = func_num_args();
  $args = func_get_args();
  
  for ($i = 0; $i < $args_count; $i++) {
    drush_print("Hello " . $args[$i]);
  }
}

/**
 * Validate function for say-hello.
 * The function name is drush_<module_name>_<command_in_underscore>_validate
 */
function drush_eureka_drush_say_hello_validate() {
  $args_count = func_num_args();

  if ($args_count == 0) {
    return drush_set_error('ARGUMENTS_REQUIRED', dt('Please provide at least 1 argument.'));
  }
}

 

So this time if we didn’t provide any input argument…

drush say-hello

drush-hello-world-5
 

Done =)

Reference: Creating Custom Drush Commands

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.