Closed (fixed)
Project:
Drush
Component:
Core Commands
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
1 Dec 2011 at 17:10 UTC
Updated:
16 Dec 2011 at 08:30 UTC
Jump to comment: Most recent file
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
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | drush-invoke.patch | 3.31 KB | greg.1.anderson |
| #1 | drush-dispatch.patch | 1.9 KB | greg.1.anderson |
Comments
Comment #1
greg.1.anderson commentedYou 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_dispatchinstead. This function will support the callback function, apply command-specific options, and so on.The unfortunate thing about this situation is that
drush_dispatchis 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.
Comment #2
moshe weitzman commentedI'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.
Comment #3
greg.1.anderson commentedMoving 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.
Comment #4
moshe weitzman commentedi think i am ok with status quo.
Comment #5
greg.1.anderson commentedMaybe 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.
Comment #6
moshe weitzman commentedLooks good to me too. If you grep around and more or less verify that existing calls are OK, go ahead and commit.
Comment #7
moshe weitzman commentedComment #8
greg.1.anderson commentedExisting 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.
Comment #9
greg.1.anderson commentedHm, I thought that change records are linked to from their respective issues. Guess not. The change record is here: http://drupal.org/node/1359038