function drush_parse_command

8.0.x command.inc drush_parse_command()
6.x command.inc drush_parse_command()
7.x command.inc drush_parse_command()
3.x command.inc drush_parse_command()
4.x command.inc drush_parse_command()
5.x command.inc drush_parse_command()
master command.inc drush_parse_command()

Matches a commands array, as returned by drush_get_arguments, with the current command table.

Note that not all commands may be discoverable at the point-of-call, since Drupal modules can ship commands as well, and they are not available until after bootstrapping.

drush_parse_command returns a normalized command descriptor, which is an associative array with the following entries:

  • callback: name of function to invoke for this command. The callback function name _must_ begin with "drush_commandfile_", where commandfile is from the file "commandfile.drush.inc", which contains the commandfile_drush_command() function that returned this command. Note that the callback entry is optional; it is preferable to omit it, in which case drush_invoke() will generate the hook function name.
  • callback arguments: an array of arguments to pass to the calback.
  • description: description of the command.
  • arguments: an array of arguments that are understood by the command. for help texts.
  • options: an array of options that are understood by the command. for help texts.
  • examples: an array of examples that are understood by the command. for help texts.
  • scope: one of 'system', 'project', 'site'.
  • bootstrap: drupal bootstrap level (depends on Drupal major version). -1=no_bootstrap.
  • core: Drupal major version required.
  • drupal dependencies: drupal modules required for this command.
  • drush dependencies: other drush command files required for this command (not yet implemented)

@example drush_parse_command();

2 calls to drush_parse_command()
drush_main in ./drush.php
The main Drush function.
drush_redispatch_get_options in includes/drush.inc
Get the options for this command.

File

includes/command.inc, line 201
The drush command engine.

Code

function drush_parse_command() {
  $args = drush_get_arguments();

  // Get a list of all implemented commands.
  $implemented = drush_get_commands();

  $command = FALSE;
  $arguments = array();
  // Try to determine the handler for the current command.
  while (!$command && count($args)) {
    $part = implode(" ", $args);
    if (isset($implemented[$part])) {
      $command = $implemented[$part];
    }
    else {
      $arguments[] = array_pop($args);
    }
  }

  // We have found a command that matches. Set the appropriate values.
  if ($command) {
    // Special case. Force help command if --help option was specified.
    if (drush_get_option(array('h', 'help'))) {
      $arguments = array($command['command']);
      $command = $implemented['help'];
      $command['arguments'] = $arguments;
    }
    else {
      $arguments = array_reverse($arguments);

      // Merge specified callback arguments, which precede the arguments passed on the command line.
      if (isset($command['callback arguments']) && is_array($command['callback arguments'])) {
        $arguments = array_merge($command['callback arguments'], $arguments);
      }
    }
    $command['arguments'] = $arguments;
    drush_set_command($command);
  }
  return $command;
}