Previously:
- Drupal 7 – Write your first Hello World Drush command
- Drupal 7 – Write a Drush command which takes arguments input
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
Done =)
Reference: Creating Custom Drush Commands