From 355abd071b831a66569212e8695bd0784edfbbe9 Mon Sep 17 00:00:00 2001 From: Greg Anderson Date: Wed, 18 Sep 2013 23:03:07 -0700 Subject: [PATCH 1/5] Add --no-field-labels option, so that users may omit field labels from command output if they do not want them (e.g. to emit only a single data item with --field=xyzzy --no-field-labels). --- commands/core/outputformat.drush.inc | 5 +++++ commands/core/outputformat/key_value.inc | 3 +++ commands/core/outputformat/table.inc | 7 ++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/commands/core/outputformat.drush.inc b/commands/core/outputformat.drush.inc index 5d0bf521b4..a7e88f24b9 100644 --- a/commands/core/outputformat.drush.inc +++ b/commands/core/outputformat.drush.inc @@ -64,6 +64,9 @@ function outputformat_drush_engine_type_info() { 'description' => 'In nested lists of lists, specify how the outer lists ("lines") should be separated.', 'hidden' => TRUE, ), + 'no-field-labels' => array( + 'description' => 'Leave off the field labels.', + ), ), // Allow output formats to declare their // "output data type" instead of their @@ -278,12 +281,14 @@ function outputformat_drush_help_alter(&$command) { if (isset($outputformat['require-engine-capability']) && is_array($outputformat['require-engine-capability'])) { if (!in_array('format-table', $outputformat['require-engine-capability'])) { unset($command['options']['fields']); + unset($command['options']['no-field-labels']); } } // If the command does define output formats, but does not // define fields, then just hide the help for the --fields option. else { $command['options']['fields']['hidden'] = TRUE; + $command['options']['no-field-labels']['hidden'] = TRUE; } } diff --git a/commands/core/outputformat/key_value.inc b/commands/core/outputformat/key_value.inc index 24e7a51a63..05a55f18f7 100644 --- a/commands/core/outputformat/key_value.inc +++ b/commands/core/outputformat/key_value.inc @@ -57,6 +57,9 @@ */ class drush_outputformat_key_value extends drush_outputformat { function format($input, $metadata) { + if (isset($metadata['no-field-labels'])) { + return drush_format($input, $metadata, 'list'); + } if (!is_array($input)) { if (isset($metadata['label'])) { $input = array(dt($metadata['label']) => $input); diff --git a/commands/core/outputformat/table.inc b/commands/core/outputformat/table.inc index e5f9af835a..2a6b94d988 100644 --- a/commands/core/outputformat/table.inc +++ b/commands/core/outputformat/table.inc @@ -41,6 +41,11 @@ class drush_outputformat_table extends drush_outputformat { } ++$col; } - return drush_format_table(drush_rows_of_key_value_to_array_table($input, $field_list, $metadata), TRUE, $widths); + $rows = drush_rows_of_key_value_to_array_table($input, $field_list, $metadata); + $field_labels = !array_key_exists('no-field-labels', $metadata); + if (!$field_labels) { + array_shift($rows); + } + return drush_format_table($rows, $field_labels, $widths); } } From 17b32f57098d6b1e219fdfd4b1a26ced6124dfe3 Mon Sep 17 00:00:00 2001 From: Greg Anderson Date: Wed, 18 Sep 2013 23:19:43 -0700 Subject: [PATCH 2/5] Handle --no-field-labels in a slightly different place for the key-value formatter. --- commands/core/outputformat/key_value.inc | 6 +++--- includes/output.inc | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/commands/core/outputformat/key_value.inc b/commands/core/outputformat/key_value.inc index 05a55f18f7..45ff9482aa 100644 --- a/commands/core/outputformat/key_value.inc +++ b/commands/core/outputformat/key_value.inc @@ -57,9 +57,6 @@ */ class drush_outputformat_key_value extends drush_outputformat { function format($input, $metadata) { - if (isset($metadata['no-field-labels'])) { - return drush_format($input, $metadata, 'list'); - } if (!is_array($input)) { if (isset($metadata['label'])) { $input = array(dt($metadata['label']) => $input); @@ -72,6 +69,9 @@ class drush_outputformat_key_value extends drush_outputformat { if ((!isset($kv_metadata['key-value-item'])) && (isset($metadata['field-labels']))) { $input = drush_select_output_fields($input, $metadata['field-labels'], $metadata['field-mappings']); } + if (isset($metadata['no-field-labels'])) { + $kv_metadata['no-field-labels'] = $metadata['no-field-labels']; + } $formatted_table = drush_key_value_to_array_table($input, $kv_metadata); if ($formatted_table === FALSE) { return FALSE; diff --git a/includes/output.inc b/includes/output.inc index 8e5a5e24fc..d7808d292b 100644 --- a/includes/output.inc +++ b/includes/output.inc @@ -314,6 +314,11 @@ function drush_key_value_to_array_table($keyvalue_table, $metadata = array()) { if (is_array($value)) { $value = drush_format($value, $metadata, 'list'); } + } + if (isset($metadata['no-field-labels'])) { + $table[] = array(isset($value) ? $value : ''); + } + elseif (isset($value)) { $table[] = array($key, ' :', $value); } else { From 51251b569af1bc5f8f2aed8b50b9ddbd83618eac Mon Sep 17 00:00:00 2001 From: Greg Anderson Date: Thu, 19 Sep 2013 19:39:55 -0700 Subject: [PATCH 3/5] Use --field-labels=0 instead of --no-field-labels --- commands/core/outputformat.drush.inc | 10 ++++++---- commands/core/outputformat/key_value.inc | 4 ++-- commands/core/outputformat/table.inc | 2 +- includes/command.inc | 6 ++++++ includes/output.inc | 2 +- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/commands/core/outputformat.drush.inc b/commands/core/outputformat.drush.inc index a7e88f24b9..5fbd720cdf 100644 --- a/commands/core/outputformat.drush.inc +++ b/commands/core/outputformat.drush.inc @@ -64,8 +64,10 @@ function outputformat_drush_engine_type_info() { 'description' => 'In nested lists of lists, specify how the outer lists ("lines") should be separated.', 'hidden' => TRUE, ), - 'no-field-labels' => array( - 'description' => 'Leave off the field labels.', + 'field-labels' => array( + 'description' => 'Add field labels before first line of data. Default is on; --field-labels=0 to disable.', + 'default' => '1', + 'key' => 'include-field-labels', ), ), // Allow output formats to declare their @@ -281,14 +283,14 @@ function outputformat_drush_help_alter(&$command) { if (isset($outputformat['require-engine-capability']) && is_array($outputformat['require-engine-capability'])) { if (!in_array('format-table', $outputformat['require-engine-capability'])) { unset($command['options']['fields']); - unset($command['options']['no-field-labels']); + unset($command['options']['field-labels']); } } // If the command does define output formats, but does not // define fields, then just hide the help for the --fields option. else { $command['options']['fields']['hidden'] = TRUE; - $command['options']['no-field-labels']['hidden'] = TRUE; + $command['options']['field-labels']['hidden'] = TRUE; } } diff --git a/commands/core/outputformat/key_value.inc b/commands/core/outputformat/key_value.inc index 45ff9482aa..5cc85eeedc 100644 --- a/commands/core/outputformat/key_value.inc +++ b/commands/core/outputformat/key_value.inc @@ -69,8 +69,8 @@ class drush_outputformat_key_value extends drush_outputformat { if ((!isset($kv_metadata['key-value-item'])) && (isset($metadata['field-labels']))) { $input = drush_select_output_fields($input, $metadata['field-labels'], $metadata['field-mappings']); } - if (isset($metadata['no-field-labels'])) { - $kv_metadata['no-field-labels'] = $metadata['no-field-labels']; + if (isset($metadata['include-field-labels'])) { + $kv_metadata['include-field-labels'] = $metadata['include-field-labels']; } $formatted_table = drush_key_value_to_array_table($input, $kv_metadata); if ($formatted_table === FALSE) { diff --git a/commands/core/outputformat/table.inc b/commands/core/outputformat/table.inc index 2a6b94d988..81a9cfa27b 100644 --- a/commands/core/outputformat/table.inc +++ b/commands/core/outputformat/table.inc @@ -42,7 +42,7 @@ class drush_outputformat_table extends drush_outputformat { ++$col; } $rows = drush_rows_of_key_value_to_array_table($input, $field_list, $metadata); - $field_labels = !array_key_exists('no-field-labels', $metadata); + $field_labels = array_key_exists('include-field-labels', $metadata) && $metadata['include-field-labels']; if (!$field_labels) { array_shift($rows); } diff --git a/includes/command.inc b/includes/command.inc index ba2c404fc4..bd5fadb685 100644 --- a/includes/command.inc +++ b/includes/command.inc @@ -493,6 +493,9 @@ function drush_handle_command_output($command, $structured_output) { // 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); } @@ -500,6 +503,9 @@ function drush_handle_command_output($command, $structured_output) { $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; } } diff --git a/includes/output.inc b/includes/output.inc index d7808d292b..c73a873c3f 100644 --- a/includes/output.inc +++ b/includes/output.inc @@ -315,7 +315,7 @@ function drush_key_value_to_array_table($keyvalue_table, $metadata = array()) { $value = drush_format($value, $metadata, 'list'); } } - if (isset($metadata['no-field-labels'])) { + if (isset($metadata['include-field-labels']) && !$metadata['include-field-labels']) { $table[] = array(isset($value) ? $value : ''); } elseif (isset($value)) { From edf4d81bc13017a368ed7bb0521a4d8064c7542f Mon Sep 17 00:00:00 2001 From: Greg Anderson Date: Thu, 19 Sep 2013 21:17:46 -0700 Subject: [PATCH 4/5] Also support field labels in csv output. --- commands/core/outputformat/list.inc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/commands/core/outputformat/list.inc b/commands/core/outputformat/list.inc index e994111d44..cb56f685ab 100644 --- a/commands/core/outputformat/list.inc +++ b/commands/core/outputformat/list.inc @@ -105,6 +105,12 @@ class drush_outputformat_list extends drush_outputformat { } } + // Include field labels, if specified + if (!isset($metadata['list-item']) && is_array($input) && isset($metadata['field-labels'])) { + if (isset($metadata['include-field-labels']) && $metadata['include-field-labels']) { + array_unshift($input, $metadata['field-labels']); + } + } foreach ($input as $label => $data) { // If this output formatter is set to print a single item from each // element, select that item here. From 69220f474f71f4d66108a4ca42b141108a90a778 Mon Sep 17 00:00:00 2001 From: Greg Anderson Date: Thu, 19 Sep 2013 21:33:00 -0700 Subject: [PATCH 5/5] Only add field labels to list formats that explicitly request them. --- commands/core/outputformat.drush.inc | 1 + commands/core/outputformat/list.inc | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/commands/core/outputformat.drush.inc b/commands/core/outputformat.drush.inc index 5fbd720cdf..5b26b2778a 100644 --- a/commands/core/outputformat.drush.inc +++ b/commands/core/outputformat.drush.inc @@ -208,6 +208,7 @@ function outputformat_drush_engine_outputformat() { 'machine-parsable' => TRUE, 'engine-class' => 'list', 'list-item-type' => 'nested-csv', + 'labeled-list' => TRUE, 'description' => 'A list of values, one per row, each of which is a comma-separated list of values.', 'engine-capabilities' => array('format-table'), 'topic-example' => array(array('a', 12, 'a@one.com'),array('b', 17, 'b@two.com')), diff --git a/commands/core/outputformat/list.inc b/commands/core/outputformat/list.inc index cb56f685ab..c46da3f0f1 100644 --- a/commands/core/outputformat/list.inc +++ b/commands/core/outputformat/list.inc @@ -106,7 +106,7 @@ class drush_outputformat_list extends drush_outputformat { } // Include field labels, if specified - if (!isset($metadata['list-item']) && is_array($input) && isset($metadata['field-labels'])) { + if (!isset($metadata['list-item']) && isset($metadata['labeled-list']) && is_array($input) && isset($metadata['field-labels'])) { if (isset($metadata['include-field-labels']) && $metadata['include-field-labels']) { array_unshift($input, $metadata['field-labels']); }