Skip to content

Commit 6033ffc

Browse files
authored
Merge pull request #210 from wp-cli/copilot/fail-gracefully-invalid-fields
Fail gracefully when invalid field is supplied to --fields
2 parents 5931819 + 9e1132b commit 6033ffc

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

features/profile-stage.feature

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@ Feature: Profile the template render stage
134134
Error: Invalid stage. Must be one of bootstrap, main_query, template, or use --all.
135135
"""
136136

137+
@require-wp-4.0
138+
Scenario: Invalid field name supplied to --fields
139+
Given a WP install
140+
141+
When I try `wp profile stage template --fields=test`
142+
Then STDERR should contain:
143+
"""
144+
Invalid field(s): test
145+
"""
146+
And the return code should be 1
147+
137148
@require-wp-4.0
138149
Scenario: Identify callback_count for each hook
139150
Given a WP install

src/Formatter.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,22 @@ public function __construct( &$assoc_args, $fields = null, $prefix = false ) {
2727
$format_args['fields'] = explode( ',', $format_args['fields'] );
2828
}
2929

30-
if ( 'time' !== $fields[0] ) {
31-
$this->total_cell_index = array_search( $fields[0], $format_args['fields'], true );
30+
$format_args['fields'] = array_filter( array_map( 'trim', $format_args['fields'] ) );
31+
32+
if ( isset( $assoc_args['fields'] ) ) {
33+
if ( empty( $format_args['fields'] ) ) {
34+
$format_args['fields'] = $fields;
35+
}
36+
$invalid_fields = array_diff( $format_args['fields'], $fields );
37+
if ( ! empty( $invalid_fields ) ) {
38+
\WP_CLI::error( 'Invalid field(s): ' . implode( ', ', $invalid_fields ) );
39+
}
3240
}
3341

34-
$format_args['fields'] = array_map( 'trim', $format_args['fields'] );
42+
if ( 'time' !== $fields[0] ) {
43+
$index = array_search( $fields[0], $format_args['fields'], true );
44+
$this->total_cell_index = ( false !== $index ) ? $index : null;
45+
}
3546

3647
$this->args = $format_args;
3748
$this->formatter = new \WP_CLI\Formatter( $assoc_args, $fields, $prefix );

0 commit comments

Comments
 (0)