Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions features/aliases.feature
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ Feature: Create shortcuts to specific WordPress installs
{"@all":"Run command against every registered alias.","@foo":{"path":"{TEST_DIR}/foo"}}
"""

When I run `wp cli alias list --format=plaintext`
Then STDOUT should match /^array \s*\(/
And STDOUT should contain:
"""
'@foo' =>
"""

When I run `wp cli aliases --format=json`
Then STDOUT should be JSON containing:
"""
Expand Down
8 changes: 8 additions & 0 deletions features/cli.feature
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ Feature: `wp cli` tasks
When I run `wp cli has-command test-command`
Then the return code should be 0

Scenario: param-dump accepts plaintext as an alias for var_export
Given a WP installation

When I run `wp cli param-dump --format=plaintext`
Then STDOUT should match /^array \s*\(/
And STDERR should be empty
And the return code should be 0

Scenario: Dump the list of global parameters with values
Given a WP installation

Expand Down
57 changes: 57 additions & 0 deletions features/formatter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,63 @@ Feature: Format output
slug: foo
"""

Scenario: Format output as var_export or plaintext
Given an empty directory
And a output-export.php file:
"""
<?php
/**
* Output data as var_export or plaintext
*
* <type>
* : Type of output.
*
* [--format=<format>]
* : Output format.
* ---
* default: var_export
* options:
* - var_export
* - plaintext
* ---
*
* @when before_wp_load
*/
$output_export = function( $args, $assoc_args ) {
$items = array(
array(
'label' => 'Foo',
'slug' => 'foo',
),
array(
'label' => 'Bar',
'slug' => 'bar',
),
);
$format_args = array(
'format' => \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'var_export' ),
'fields' => array( 'label', 'slug' ),
);
$formatter = new \WP_CLI\Formatter( $format_args );
$formatter->display_items( $items );
};
WP_CLI::add_command( 'exportfmt', $output_export );
"""

When I run `wp --require=output-export.php exportfmt all --format=var_export`
Then STDOUT should match /^array \s*\(/
And STDOUT should contain:
"""
'label' => 'Foo',
"""

When I run `wp --require=output-export.php exportfmt all --format=plaintext`
Then STDOUT should match /^array \s*\(/
And STDOUT should contain:
"""
'slug' => 'bar',
"""

Scenario: Format data in RTL language
Given an empty directory
And a file.php file:
Expand Down
13 changes: 12 additions & 1 deletion php/WP_CLI/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function display_items( $items, $ascii_pre_colorized = false ) {
$items = iterator_to_array( $items );
}

if ( in_array( $this->args['format'], [ 'csv', 'json', 'table', 'yaml' ], true ) ) {
if ( in_array( $this->args['format'], [ 'csv', 'json', 'table', 'yaml', 'var_export', 'plaintext' ], true ) ) {
// Validate fields exist in at least one item
if ( ! empty( $this->args['fields'] ) ) {
$this->validate_fields( $items );
Expand Down Expand Up @@ -231,6 +231,15 @@ private function format( $items, $ascii_pre_colorized = false ): void {
}
break;

case 'var_export':
case 'plaintext':
$out = [];
foreach ( $items as $item ) {
$out[] = Utils\pick_fields( $item, $fields );
}
echo var_export( $out, true );
break;

default:
WP_CLI::error( 'Invalid format: ' . $this->args['format'] );
}
Expand Down Expand Up @@ -417,6 +426,8 @@ private function show_multiple_fields( $data, $format, $ascii_pre_colorized = fa

case 'yaml':
case 'json':
case 'var_export':
case 'plaintext':
WP_CLI::print_value(
$ordered_data,
[
Expand Down
49 changes: 38 additions & 11 deletions php/class-wp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -1181,24 +1181,51 @@ public static function read_value( $raw_value, $assoc_args = [] ) {
* @param array $assoc_args Arguments passed to the command, determining format.
*/
public static function print_value( $value, $assoc_args = [] ) {
$_value = '';
if ( Utils\get_flag_value( $assoc_args, 'format' ) === 'json' ) {
$_value = json_encode( $value );
} elseif ( Utils\get_flag_value( $assoc_args, 'format' ) === 'yaml' ) {
$format = Utils\get_flag_value( $assoc_args, 'format' );
if ( 'json' === $format ) {
$encoded = json_encode( $value );
$output = false === $encoded ? '' : $encoded;
} elseif ( 'yaml' === $format ) {
/**
* @var array $value
*/
$_value = Spyc::YAMLDump( $value, 2, 0 );
$output = Spyc::YAMLDump( $value, 2, 0 );
} elseif ( in_array( $format, [ 'var_export', 'plaintext' ], true ) ) {
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description mentions adding Utils\is_var_export_format() and using it in print_value(), but there is no such helper in the codebase and the format check is duplicated inline here. Consider adding the helper in WP_CLI\Utils (e.g., returning true for var_export/plaintext) and using it here (and in other updated call sites), or update the PR description if the helper was intentionally dropped.

Copilot uses AI. Check for mistakes.
if ( is_array( $value ) || is_object( $value ) ) {
$output = var_export( $value, true );
} else {
$output = self::scalar_to_print_string( $value );
}
} elseif ( is_array( $value ) || is_object( $value ) ) {
$_value = var_export( $value, true );
$output = var_export( $value, true );
} else {
/**
* @var string|int $_value
*/
$_value = $value;
$output = self::scalar_to_print_string( $value );
}

echo $output . "\n";
}

/**
* String for print_value when the value is not JSON/YAML/var_export(array|object).
*
* @param mixed $value Value to stringify.
* @return string
*/
private static function scalar_to_print_string( $value ): string {
if ( is_string( $value ) ) {
return $value;
}
if ( is_int( $value ) || is_float( $value ) ) {
return (string) $value;
}
if ( is_bool( $value ) ) {
return $value ? '1' : '';
}
if ( null === $value ) {
return '';
}

echo $_value . "\n";
return var_export( $value, true );
}

/**
Expand Down
1 change: 1 addition & 0 deletions php/commands/src/CLI_Alias_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class CLI_Alias_Command extends WP_CLI_Command {
* - yaml
* - json
* - var_export
* - plaintext
* ---
*
* [--raw]
Expand Down
6 changes: 4 additions & 2 deletions php/commands/src/CLI_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ private function array_find( $arr, $callback ) {
}

/**
* Dumps the list of global parameters, as JSON or in var_export format.
* Dumps the list of global parameters, as JSON or in var_export format (`plaintext` is an alias for `var_export`).
*
* ## OPTIONS
*
Expand All @@ -784,13 +784,15 @@ private function array_find( $arr, $callback ) {
* default: json
* options:
* - var_export
* - plaintext
* - json
* ---
*
* ## EXAMPLES
*
* # Dump the list of global parameters.
* $ wp cli param-dump --format=var_export
* $ wp cli param-dump --format=plaintext
* array (
* 'path' =>
* array (
Expand Down Expand Up @@ -821,7 +823,7 @@ public function param_dump( $_, $assoc_args ) {
}
}

if ( 'var_export' === Utils\get_flag_value( $assoc_args, 'format' ) ) {
if ( in_array( Utils\get_flag_value( $assoc_args, 'format' ), [ 'var_export', 'plaintext' ], true ) ) {
var_export( $spec );
} else {
echo json_encode( $spec );
Expand Down
2 changes: 1 addition & 1 deletion php/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ function wp_version_compare( $since, $operator ) {
* @access public
* @category Output
*
* @param string $format Format to use: 'table', 'json', 'csv', 'yaml', 'ids', 'count'.
* @param string $format Format to use: 'table', 'json', 'csv', 'yaml', 'ids', 'count', 'var_export', 'plaintext'.
* @param array<mixed> $items An array of items to output.
* @param array<string>|string $fields Named fields for each item of data. Can be array or comma-separated list.
*/
Expand Down
Loading