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. Loads the requested output formatter and uses it to process the provided input.

Parameters

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

10 calls to drush_format()
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_key_value_to_array_table in includes/output.inc
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/csv_or_string.inc
drush_print_format in includes/output.inc
Format some data and print it out. Respect --format option.
drush_rows_of_key_value_to_array_table in includes/output.inc
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…

... See full list

File

includes/output.inc, line 99

Code

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;
}