Skip to content

Commit a823ffa

Browse files
authored
Merge pull request #40 from wp-cli/avoid-display-default
Avoid displaying default delimiter on regex fail
2 parents 54c5dc3 + e58cc0d commit a823ffa

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

features/search-replace.feature

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,34 @@ Feature: Do global search/replace
381381
"""
382382
And the return code should be 1
383383

384+
When I try `wp search-replace 'regex error)' '' --regex`
385+
Then STDERR should be:
386+
"""
387+
Error: The regex pattern 'regex error)' with default delimiter 'chr(1)' and no flags fails.
388+
"""
389+
And the return code should be 1
390+
391+
When I try `wp search-replace 'regex error)' '' --regex --regex-flags=u`
392+
Then STDERR should be:
393+
"""
394+
Error: The regex pattern 'regex error)' with default delimiter 'chr(1)' and flags 'u' fails.
395+
"""
396+
And the return code should be 1
397+
398+
When I try `wp search-replace 'regex error)' '' --regex --regex-delimiter=/`
399+
Then STDERR should be:
400+
"""
401+
Error: The regex '/regex error)/' fails.
402+
"""
403+
And the return code should be 1
404+
405+
When I try `wp search-replace 'regex error)' '' --regex --regex-delimiter=/ --regex-flags=u`
406+
Then STDERR should be:
407+
"""
408+
Error: The regex '/regex error)/u' fails.
409+
"""
410+
And the return code should be 1
411+
384412
Scenario: Formatting as count-only
385413
Given a WP install
386414
And I run `wp option set foo 'ALPHA.example.com'`

src/Search_Replace_Command.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,18 @@ public function __invoke( $args, $assoc_args ) {
163163
$php_only = \WP_CLI\Utils\get_flag_value( $assoc_args, 'precise' );
164164
$this->recurse_objects = \WP_CLI\Utils\get_flag_value( $assoc_args, 'recurse-objects', true );
165165
$this->verbose = \WP_CLI\Utils\get_flag_value( $assoc_args, 'verbose' );
166-
$this->regex = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex' );
167-
$this->regex_flags = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex-flags' );
168-
$this->regex_delimiter = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex-delimiter', chr( 1 ) );
169166
$this->format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format' );
170167

168+
if ( ( $this->regex = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex', false ) ) ) {
169+
$this->regex_flags = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex-flags', false );
170+
$default_regex_delimiter = false;
171+
$this->regex_delimiter = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex-delimiter', '' );
172+
if ( '' === $this->regex_delimiter ) {
173+
$this->regex_delimiter = chr( 1 );
174+
$default_regex_delimiter = true;
175+
}
176+
}
177+
171178
if ( ! empty( $this->regex ) ) {
172179
if ( '' === $this->regex_delimiter ) {
173180
$this->regex_delimiter = chr( 1 );
@@ -177,7 +184,13 @@ public function __invoke( $args, $assoc_args ) {
177184
$search_regex .= $this->regex_delimiter;
178185
$search_regex .= $this->regex_flags;
179186
if ( false === @preg_match( $search_regex, '' ) ) {
180-
\WP_CLI::error( "The regex '$search_regex' fails." );
187+
if ( $default_regex_delimiter ) {
188+
$flags_msg = $this->regex_flags ? "flags '$this->regex_flags'" : "no flags";
189+
$msg = "The regex pattern '$old' with default delimiter 'chr(1)' and {$flags_msg} fails.";
190+
} else {
191+
$msg = "The regex '$search_regex' fails.";
192+
}
193+
WP_CLI::error( $msg );
181194
}
182195
}
183196

0 commit comments

Comments
 (0)