Score:3

How to get the current running Drush command?

tk flag

I would like to detect from code the current running drush command. I have some hooks that will be run when I exec some drush commands and want to detect which command is triggering the hook.

I manage to get the info from input() but I want to know if there is a more appropriate way:

  if (PHP_SAPI === 'cli') {
    $command = \Drush\Drush::input()->getArguments()
  }

I would like to have something similar to this:

  if (PHP_SAPI === 'cli') {
    $command = \Drush\Drush::...getCurentCommand();
  }

Similar to Drupal::routeMatch()->getRouteName() in someway.

Thank you

id flag
This isn’t an answer but check out its hooks. There might be something there: https://www.drush.org/latest/hooks/. Also: https://www.php.net/manual/en/function.getopt.php
Score:3
cn flag

A more appropriate way would be to use the object data you get as arguments. Most hooks receive $commandData from where you can get the first argument (the command):

$input = $commandData->input();
$command_name = $input->getFirstArgument();

Similar to the route name of the matched route you could get the full command name of the matched command via the annotations:

$full_command_name = $commandData->annotationData()->get('command');

If you need to use static code then get the command name as the first argument like in the question and convert it to the full command name:

$command_name = \Drush\Drush::input()->getFirstArgument();
$full_command_name = \Drush\Drush::getApplication()->find($command_name)->getName();
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.