function _drush_verify_cli_options

8.0.x command.inc _drush_verify_cli_options($command)
6.x command.inc _drush_verify_cli_options($command)
7.x command.inc _drush_verify_cli_options($command)
5.x command.inc _drush_verify_cli_options($command)
master command.inc _drush_verify_cli_options($command)

Fail with an error if the user specified options on the command line that are not documented in the current command record. Also verify that required options are present.

Related topics

1 call to _drush_verify_cli_options()
drush_dispatch in includes/command.inc
Given a command record, dispatch it as if it were the original command. Executes in the currently bootstrapped site using the current option contexts. Note that drush_dispatch will not bootstrap any further than the current command has already…

File

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

Code

function _drush_verify_cli_options($command) {
  // Start out with just the options in the current command record.
  $options = _drush_get_command_options($command);
  // Skip all tests if the command is marked to allow anything.
  // Also skip backend commands, which may have options on the commandline
  // that were inherited from the calling command.
  if (($command['allow-additional-options'] === TRUE)) {
    return TRUE;
  }
  // If 'allow-additional-options' contains a list of command names,
  // then union together all of the options from all of the commands.
  if (is_array($command['allow-additional-options'])) {
    $implemented = drush_get_commands();
    foreach ($command['allow-additional-options'] as $subcommand_name) {
      if (array_key_exists($subcommand_name, $implemented)) {
        $options = array_merge($options, _drush_get_command_options($implemented[$subcommand_name]));
      }
    }
  }
  // Also add in global options
  $options = array_merge($options, drush_get_global_options());

  // Add a placeholder option so that backend requests originating from prior versions of Drush are valid.
  $options += array('invoke' => '');

  // Now we will figure out which options in the cli context
  // are not represented in our options list.
  $cli_options = array_keys(drush_get_context('cli'));
  $allowed_options = _drush_flatten_options($options);
  $allowed_options = drush_append_negation_options($allowed_options);
  $disallowed_options = array_diff($cli_options, $allowed_options);
  if (!empty($disallowed_options)) {
    $unknown = count($disallowed_options) > 1 ? dt('Unknown options') : dt('Unknown option');
    if (drush_get_option('strict', TRUE)) {
      $msg = dt("@unknown: --@options.  See `drush help @command` for available options. To suppress this error, add the option --strict=0.", array('@unknown' => $unknown, '@options' => implode(', --', $disallowed_options), '@command' => $command['command']));
      return drush_set_error('DRUSH_UNKNOWN_OPTION', $msg);
    }
  }

  // Next check to see if all required options were specified,
  // and if all specified options with required values have values.
  $missing_required_options = array();
  $options_missing_required_values = array();
  foreach ($command['options'] as $option_name => $option) {
    if (is_array($option) && !empty($option['required']) && drush_get_option($option_name, NULL) === NULL) {
      $missing_required_options[] = $option_name;
    }
    // Note that drush_get_option() will return TRUE if an option
    // was specified without a value (--option), as opposed to
    // the string "1" is --option=1 was used.
    elseif (is_array($option) && !empty($option['value']) && ($option['value'] == 'required') && drush_get_option($option_name, NULL) === TRUE) {
      $options_missing_required_values[] = $option_name;
    }
  }
  if (!empty($missing_required_options) || !empty($options_missing_required_values)) {
    $missing_message = '';
    if (!empty($missing_required_options)) {
      $missing = count($missing_required_options) > 1 ? dt('Missing required options') : dt('Missing required option');
      $missing_message = dt("@missing: --@options.", array('@missing' => $missing, '@options' => implode(', --', $missing_required_options)));
    }
    if (!empty($options_missing_required_values)) {
      if (!empty($missing_message)) {
        $missing_message .= "  ";
      }
      $missing = count($options_missing_required_values) > 1 ? dt('Options used without providing required values') : dt('Option used without a value where one was required');
      $missing_message .= dt("@missing: --@options.", array('@missing' => $missing, '@options' => implode(', --', $options_missing_required_values)));
    }
    return drush_set_error(dt("!message  See `drush help @command` for information on usage.", array('!message' => $missing_message, '@command' => $command['command'])));
  }
  return TRUE;
}