diff --git a/features/profile-stage.feature b/features/profile-stage.feature index 99a86b46..0a1152b7 100644 --- a/features/profile-stage.feature +++ b/features/profile-stage.feature @@ -134,6 +134,17 @@ Feature: Profile the template render stage Error: Invalid stage. Must be one of bootstrap, main_query, template, or use --all. """ + @require-wp-4.0 + Scenario: Invalid field name supplied to --fields + Given a WP install + + When I try `wp profile stage template --fields=test` + Then STDERR should contain: + """ + Invalid field(s): test + """ + And the return code should be 1 + @require-wp-4.0 Scenario: Identify callback_count for each hook Given a WP install diff --git a/src/Formatter.php b/src/Formatter.php index d09101e4..775db154 100644 --- a/src/Formatter.php +++ b/src/Formatter.php @@ -27,11 +27,22 @@ public function __construct( &$assoc_args, $fields = null, $prefix = false ) { $format_args['fields'] = explode( ',', $format_args['fields'] ); } - if ( 'time' !== $fields[0] ) { - $this->total_cell_index = array_search( $fields[0], $format_args['fields'], true ); + $format_args['fields'] = array_filter( array_map( 'trim', $format_args['fields'] ) ); + + if ( isset( $assoc_args['fields'] ) ) { + if ( empty( $format_args['fields'] ) ) { + $format_args['fields'] = $fields; + } + $invalid_fields = array_diff( $format_args['fields'], $fields ); + if ( ! empty( $invalid_fields ) ) { + \WP_CLI::error( 'Invalid field(s): ' . implode( ', ', $invalid_fields ) ); + } } - $format_args['fields'] = array_map( 'trim', $format_args['fields'] ); + if ( 'time' !== $fields[0] ) { + $index = array_search( $fields[0], $format_args['fields'], true ); + $this->total_cell_index = ( false !== $index ) ? $index : null; + } $this->args = $format_args; $this->formatter = new \WP_CLI\Formatter( $assoc_args, $fields, $prefix );