I have a drush command (ie. 'my-command') that has has a custom callback (ie. 'my_custom_callback') function defined in hook_drush_command() implementation.

From another drush command I do drush_invoke('my-command') but my_custom_callback() doesn't get called.
If I comment out the 'callback' option form hook_drush_command() and name my function to drush_MY_FILE_my_command() then it gets picked up correctly when doing the same drush_invoke call.

I've tested this with the latest 4 dev snapshot

Comments

greg.1.anderson’s picture

Title: drush_invoke does not respect 'callback' property » drush_dispatch is preferred to drush_invoke, but is less convenient to use
Category: bug » feature
Status: Active » Needs review
StatusFileSize
new1.9 KB

You are correct; drush_invoke does not do a full dispatch of the command. It does not support the callback function, and it does not apply command-specific options.

For your purpose, it would be better to call drush_dispatch instead. This function will support the callback function, apply command-specific options, and so on.

The unfortunate thing about this situation is that drush_dispatch is less convenient to call than drush_invoke in that it takes a command array instead of a command name, as drush_invoke does, and it takes a few awkward lines for the client to look up the command record first. We could make things better in drush-5 and even in drush-4.6 by adding this lookup code to the head of drush_dispatch, as shown in the attached patch.

@andreiashu: Make sure that drush_invoke / drush_dispatch are really the functions you intend to call. These functions are for use in cases where you want to create a compound command that dispatches to other drush commands that run with the arguments and options the user specified on the command line. If you want to run a command using the arguments and options that you specify in your function, drush_invoke_process is preferable. Use '@self' for the site alias record parameter to run on the bootstrapped site.

moshe weitzman’s picture

I'm on the fence about this change. It is a little ugly for dispatch to take a string or an array. Also, I like the name correlation between drush_invoke and drush_invoke_process. drush_dispatch() and drush_invoke_process() don't hang together as well.

greg.1.anderson’s picture

Moving command-specific options to drush_invoke would not work well; ditto for support of the 'callback' function. Renaming drush_dispatch to drush_invoke would be inadvisable. If we wish to keep drush_invoke as the preferred entrypoint for local dispatches, then we have to live with its limitations, I think.

moshe weitzman’s picture

Status: Needs review » Closed (works as designed)

i think i am ok with status quo.

greg.1.anderson’s picture

Title: drush_dispatch is preferred to drush_invoke, but is less convenient to use » drush_invoke does not respect 'callback' property
Status: Closed (works as designed) » Needs review
StatusFileSize
new3.31 KB

Maybe I was too hasty in #3.

The attached patch changes drush_invoke to call through to drush_dispatch. drush_dispatch remains unchanged. The old drush_invoke is renamed to _drush_invoke_hooks, an internal-only function.

This change might subtly affect some drush commands, but all test cases pass, and overall I think it is an improvement (drush_invoke behaves consistently, and can now be used with commands that use a callback).

If this looks good, I will fix up the comments to match the new code. It might also be a good idea to stop overloading $command['arguements'], maybe renaming the cli arguements to 'args' or 'cli-args' to minimize the impact.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me too. If you grep around and more or less verify that existing calls are OK, go ahead and commit.

moshe weitzman’s picture

Version: All-versions-4.x-dev »
greg.1.anderson’s picture

Status: Reviewed & tested by the community » Fixed

Existing commands in Drush core are okay; there is some small chance that contrib commands may be affected, which is what I was referring to in #5. I have added a change record for this issue & committed to master.

Not eligible for back-port.

greg.1.anderson’s picture

Hm, I thought that change records are linked to from their respective issues. Guess not. The change record is here: http://drupal.org/node/1359038

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.