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


1 Answer 1


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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.