function drush_format

8.0.x output.inc drush_format($input, $metadata = NULL, $format = NULL)
6.x output.inc drush_format($input, $metadata = NULL, $format = NULL)
7.x output.inc drush_format($input, $metadata = NULL, $format = NULL)
5.x output.inc drush_format($input, $label = NULL, $format = NULL)
master output.inc drush_format($input, $metadata = NULL, $format = NULL)

Prepares a variable for printing.

Parameters

mixed $input: A variable.

string $label: Optional prefix. Not used by JSON format.

Return value

string The variable formatted according to specified format. Ready for drush_print().

Related topics

3 calls to drush_format()
drush_cache_command_get in commands/core/cache.drush.inc
Print an object returned from the cache.
drush_core_help in commands/core/help.drush.inc
Command callback for help command. This is the default command, when none other has been specified.
drush_variable_get in commands/core/variable.drush.inc
Command callback. List your site's variables.

File

includes/output.inc, line 83

Code

function drush_format($input, $label = NULL, $format = NULL) {
  if (is_null(($format))) {
    $format = drush_get_option('format', 'print_r');
  }
  if ($format != 'export' && $format != 'config') {
    if ($input === TRUE) {
      $input = 'TRUE';
    }
    elseif ($input === FALSE) {
      $input = 'FALSE';
    }
  }

  switch ($format) {
    case 'config':
      if ($label) {
        $output = "\$config['$label'] = " . var_export($input, TRUE) . ';';
      }
      else {
        $output = var_export($input, TRUE);
      }
      break;
    case 'export':
      if ($label) {
        $output = "\$variables['$label'] = " . var_export($input, TRUE) . ';';
      }
      else {
        $output = var_export($input, TRUE);
      }
      break;
    case 'json':
      $output = drush_json_encode($input);
      break;
    case 'print_r':
    default:
      if (is_string($input)) {
        $input = '"' . $input . '"';
      }
      elseif (is_array($input) || is_object($input)) {
        $input = print_r($input, TRUE);
      }

      if ($label) {
        $input = $label . ': ' . $input;
      }
      $output = $input;
      break;
  }
  return $output;
}