function drush_format

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

Prepares a variable for printing. Loads the requested output formatter and uses it to process the provided input.


mixed $input: A variable.

string $metadata: Optional formatting metadata. Not used by all formats. 'label' - text to label data with in some formats (e.g. export, config)

string $format: Optional format; defaults to print_r.

Return value

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

Related topics

7 calls to drush_format()
drush_key_value_to_array_table in includes/
Convert an associative array of key : value pairs into a table suitable for processing by drush_print_table.
drush_outputformat_csv_or_string::format in commands/core/outputformat/
drush_print_format in includes/
Format some data and print it out. Respect --format option.
drush_rows_of_key_value_to_array_table in includes/
Convert an array of data rows, where each row contains an associative array of key : value pairs, into a table suitable for processing by drush_print_table. The provided $header determines the order that the items will appear in the output. Only data…
outputFormatUnitCase::testOutputFormat in tests/outputFormatUnitTest.php
Test various output formats using php-eval with no Drupal site.

... See full list


includes/, line 101


function drush_format($input, $metadata = NULL, $format = NULL) {
  $output = '';
  // Look up the format and label, and fill in default values for metadata
  if (is_string($metadata)) {
    $metadata = array('label' => $metadata);
  if (!is_array($metadata)) {
    $metadata = array();
  $metadata += array(
    'metameta' => array(),
  if (isset($metadata['format'])) {
    // If the format is set in metadata, then it will
    // override whatever is passed in via the $format parameter.
    $format = $metadata['format'];
  if (!isset($format)) {
    // TODO: we shouldn't ever call drush_get_option here.
    // Get rid of this once we confirm that there are no
    // callers that still need it.
    $format = drush_get_option('format', 'print-r');

  $formatter = drush_load_engine('outputformat', $format);
  if ($formatter) {
    if ($formatter === TRUE) {
      return drush_set_error(dt('No outputformat class defined for !format', array('!format' => $format)));
    $output = $formatter->process($input, $metadata);

  return $output;