function drush_variable_set

8.0.x variable.drush.inc drush_variable_set()
6.x variable.drush.inc drush_variable_set()
7.x variable.drush.inc drush_variable_set()
3.x variable.drush.inc drush_variable_set()
4.x variable.drush.inc drush_variable_set()
5.x variable.drush.inc drush_variable_set()
master variable.drush.inc drush_variable_set()

Command callback. Set a variable.

File

commands/core/variable.drush.inc, line 111

Code

function drush_variable_set() {
  $args = func_get_args();
  if (!isset($args[0])) {
    return drush_set_error('DRUSH_VARIABLE_ERROR', dt('No variable specified.'));
  }
  $value = $args[1];
  if (!isset($value)) {
    return drush_set_error('DRUSH_VARIABLE_ERROR', dt('No value specified.'));
  }

  $result = drush_variable_like($args[0]);

  $options[] = "$args[0] " . dt('(new variable)');
  $match = FALSE;
  while (!$match && $name = drush_db_result($result)) {
    if ($name == $args[0]) {
      $options[0] = $name;
      $match = TRUE;
    }
    else {
      $options[] = $name;
    }
  }

  if ((drush_get_option('always-set', FALSE)) || ((count($options) == 1 || $match) && drush_get_context('DRUSH_AFFIRMATIVE'))) {
    variable_set($args[0], $value);
    drush_log(dt('!name was set to !value.', array('!name' => $args[0], '!value' => $value)), 'success');
    return '';
  }
  else {
    $choice = drush_choice($options, 'Enter a number to choose which variable to set.');
    if ($choice !== FALSE) {
      $choice = $options[$choice];
      $choice = str_replace(' ' . dt('(new variable)'), '', $choice);
      drush_op('variable_set', $choice, $value);
      drush_log(dt('!name was set to !value', array('!name' => $choice, '!value' => $value)), 'success');
    }
  }
}