function drush_parse_args

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

Parse console arguments.

1 call to drush_parse_args()
_drush_bootstrap_drush in includes/environment.inc
Initial Drush bootstrap phase.

File

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

Code

function drush_parse_args() {
  $args = drush_get_context('argv');

  static $arg_opts = array(
    'c',
    'h',
    'u',
    'r',
    'l',
    'i',
  );

  $arguments = $options = array();

  for ($i = 1; $i < count($args); $i++) {
    $opt = $args[$i];
    // Is the arg an option (starting with '-')?
    if ($opt{0} == "-" && strlen($opt) != 1) {
      // Do we have multiple options behind one '-'?
      if (strlen($opt) > 2 && $opt{1} != "-") {
        // Each char becomes a key of its own.
        for ($j = 1; $j < strlen($opt); $j++) {
          $options[substr($opt, $j, 1)] = true;
        }
      }
      // Do we have a longopt (starting with '--')?
      elseif ($opt{1} == "-") {
        if ($pos = strpos($opt, '=')) {
          $options[substr($opt, 2, $pos - 2)] = substr($opt, $pos + 1);
        }
        else {
          $options[substr($opt, 2)] = true;
        }
      }
      else {
        $opt = substr($opt, 1);
        // Check if the current opt is in $arg_opts (= has to be followed by an argument).
        if ((in_array($opt, $arg_opts))) {
          if (($args[$i + 1] == NULL) || ($args[$i + 1] == "") || ($args[$i + 1]{0} == "-")) {
            drush_set_error('DRUSH_INVALID_INPUT', "Invalid input: -$opt needs to be followed by an argument.");
          }
          $options[$opt] = $args[$i + 1];
          $i++;
        }
        else {
          $options[$opt] = true;
        }
      }
    }
    // If it's not an option, it's a command.
    else {
      $arguments[] = $opt;
    }
  }
  // If arguments are specified, print the help screen.
  $arguments = sizeof($arguments) ? $arguments : array('help');

  drush_set_arguments($arguments);
  drush_set_context('options', $options);
}