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.

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 532
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) || (drush_get_option(array('backend', 'invoke'), FALSE))) {
    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());

  // 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');
    $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']));
    if (drush_get_option('strict', TRUE)) {
      return drush_set_error('DRUSH_UNKNOWN_OPTION', $msg);
    }
  }

  // Next check to see if all required options were specified.
  $missing_required_options = array();
  foreach ($command['options'] as $key => $value) {
    if (is_array($value) && array_key_exists('required', $value)) {
      $option_value = drush_get_option($key, NULL);
      if (!isset($option_value)) {
        $missing_required_options[] = $key;
      }
    }
  }
  if (!empty($missing_required_options)) {
    $missing = count($missing_required_options) > 1 ? dt('Missing required options') : dt('Missing required option');
    return drush_set_error(dt("@missing: --@options.  See `drush help @command` for information on usage.", array('@missing' => $missing, '@options' => implode(', --', $missing_required_options), '@command' => $command['command'])));
  }
  return TRUE;
}