function drush_handle_command_output

8.0.x command.inc drush_handle_command_output($command, $structured_output)
6.x command.inc drush_handle_command_output($command, $structured_output)
7.x command.inc drush_handle_command_output($command, $structured_output)
master command.inc drush_handle_command_output($command, $structured_output)

Convert the structured output array provided from the Drush command into formatted output. Output is only printed for commands that define 'default-format' &/or 'default-pipe-format'; all other commands are expected to do their own output.

Related topics

1 call to drush_handle_command_output()
_drush_invoke_hooks in includes/command.inc
Invoke Drush API calls, including all hooks.

File

includes/command.inc, line 455
The drush command engine.

Code

function drush_handle_command_output($command, $structured_output) {
  // If the hook already called drush_backend_set_result,
  // then return that value. If it did not, then the return
  // value from the hook will be the value returned from
  // this routine.
  $return = drush_backend_get_result();
  if (empty($return)) {
    drush_backend_set_result($structured_output);
  }
  // We skip empty strings and empty arrays, but note that 'empty'
  // returns TRUE for the integer value '0', but we do want to print that.
  // Only handle output here if the command defined an output format
  // engine.  If no engine was declared, then we presume that the command
  // handled its own output.
  if ((!empty($structured_output) || ($structured_output === 0))) {
    // If the command specifies a default pipe format and
    // returned a result, then output the formatted output when
    // in --pipe mode.
    $formatter = drush_get_outputformat();
    if (!$formatter && is_string($structured_output)) {
      $formatter = drush_load_engine('outputformat', 'string');
    }
    if ($formatter) {
      if ($formatter === TRUE) {
        return drush_set_error(dt('No outputformat class defined for !format', array('!format' => $format)));
      }
      if ((!empty($command['engines']['outputformat'])) && (!in_array($formatter->engine, $command['engines']['outputformat']['usable']))) {
        return $formatter->format_error(dt("The command '!command' does not produce output in a structure usable by this output format.", array('!command' => $command['command'])));
      }
      // Add any user-specified options to the metadata passed to the formatter.
      $metadata = array();
      $metadata['strict'] = drush_get_option('strict', FALSE);
      if (isset($formatter->engine_config['options'])) {
        $machine_parsable = $formatter->engine_config['engine-info']['machine-parsable'];
        if (drush_get_option('full', FALSE)) {
          if (isset($formatter->engine_config['fields-full'])) {
            $formatter->engine_config['fields-default'] = $formatter->engine_config['fields-full'];
          }
          else {
            $formatter->engine_config['fields-default'] = array_keys($formatter->engine_config['field-labels']);
          }
        }
        elseif ((drush_get_context('DRUSH_PIPE') || $machine_parsable) && isset($formatter->engine_config['fields-pipe'])) {
          $formatter->engine_config['fields-default'] = $formatter->engine_config['fields-pipe'];
        }

        // Determine the --format, and options relevant for that format.
        foreach ($formatter->engine_config['options'] as $option => $option_info) {
          $default_value = isset($formatter->engine_config[$option . '-default']) ? $formatter->engine_config[$option . '-default'] : FALSE;
          if (($default_value === FALSE) && array_key_exists('default', $option_info)) {
            $default_value = $option_info['default'];
          }
          if (isset($option_info['list'])) {
            $user_specified_value = drush_get_option_list($option, $default_value);
          }
          else {
            $user_specified_value = drush_get_option($option, $default_value);
          }
          if ($user_specified_value !== FALSE) {
            if (array_key_exists('key', $option_info)) {
              $option = $option_info['key'];
            }
            $metadata[$option] = $user_specified_value;
          }
        }
      }
      if (isset($metadata['fields']) && !empty($metadata['fields'])) {
        if (isset($formatter->engine_config['field-labels'])) {
          $formatter->engine_config['field-labels'] = drush_select_fields($formatter->engine_config['field-labels'], $metadata['fields'], $metadata['strict']);
        }
      }
      $output = $formatter->process($structured_output, $metadata);
      if (drush_get_context('DRUSH_PIPE')) {
        drush_print_pipe($output);
      }
      else {
        drush_print($output);
      }
    }
  }
}