function drush_adjust_args_if_shebang_script

8.0.x command.inc drush_adjust_args_if_shebang_script(&$args)
6.x command.inc drush_adjust_args_if_shebang_script(&$args)
7.x command.inc drush_adjust_args_if_shebang_script(&$args)
4.x command.inc drush_adjust_args_if_shebang_script(&$args)
5.x command.inc drush_adjust_args_if_shebang_script(&$args)
master command.inc drush_adjust_args_if_shebang_script(&$args)

Special checking for "shebang" script handling.

If there is a file 'script.php' that begins like so: #!/path/to/drush Then $args will be: /path/to/drush /path/to/script userArg1 userArg2 ... If it instead starts like this: #!/path/to/drush --flag php-script Then $args will be: /path/to/drush "--flag php-script" /path/to/script userArg1 userArg2 ... (Note that execve does not split the parameters from the shebang line on whitespace; see http://en.wikipedia.org/wiki/Shebang_%28Unix%29) When drush is called via one of the "shebang" lines above, the first or second parameter will be the full path to the "shebang" script file -- and if the path to the script is in the second position, then we will expect that the argument in the first position must begin with a '@' (alias) or '-' (flag). Under ordinary circumstances, we do not expect that the drush command must come before any argument that is the full path to a file. We use this assumption to detect "shebang" script execution.

1 call to drush_adjust_args_if_shebang_script()
drush_parse_args in includes/command.inc
Parse console arguments.

File

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

Code

function drush_adjust_args_if_shebang_script(&$args) {
  if (drush_has_bash()) {
    // The drush.launcher script may add --php or --php-options at the
    // head of the argument list; skip past those.
    $base_arg_number = 1;
    while (substr($args[$base_arg_number], 0, 5) == '--php') {
      ++$base_arg_number;
    }
    if (_drush_is_drush_shebang_script($args[$base_arg_number])) {
      // If $args[1] is a drush "shebang" script, we will insert
      // the option "--bootstrap-to-first-arg" and the command
      // "php-script" at the beginning of  @args, so the command
      // line args become:
      //   /path/to/drush --bootstrap-to-first-arg php-script /path/to/script userArg1 userArg2 ...
      drush_set_option('bootstrap-to-first-arg', TRUE);
      array_splice($args, $base_arg_number, 0, array('php-script'));
      drush_set_context('DRUSH_SHEBANG_SCRIPT', TRUE);
    }
    elseif (((strpos($args[$base_arg_number], ' ') !== FALSE) || (!ctype_alnum($args[$base_arg_number][0]))) && (_drush_is_drush_shebang_script($args[$base_arg_number + 1]))) {
      // If $args[2] is a drush "shebang" script, we will insert
      // the space-exploded $arg[1] in place of $arg[1], so the
      // command line args become:
      //   /path/to/drush scriptArg1 scriptArg2 ... /path/to/script userArg1 userArg2 ...
      // If none of the script arguments look like a drush command,
      // then we will insert "php-script" as the default command to
      // execute.
      $script_args = explode(' ', $args[$base_arg_number]);
      $has_command = FALSE;
      foreach ($script_args as $script_arg) {
        if (preg_match("/^[a-z][a-z0-9-]*$/", $script_arg)) {
          $has_command = TRUE;
        }
      }
      if (!$has_command) {
        $script_args[] = 'php-script';
      }
      array_splice($args, 1, $base_arg_number, $script_args);
      drush_set_context('DRUSH_SHEBANG_SCRIPT', TRUE);
    }
  }
}