Skip to content

Commit

Permalink
Merge pull request #56 from wp-cli/regex_fail_default
Browse files Browse the repository at this point in the history
Avoid displaying default delimiter on regex fail in db search.
  • Loading branch information
danielbachhuber authored Oct 13, 2017
2 parents 16e42ed + 8c4840b commit 1379a28
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
28 changes: 28 additions & 0 deletions features/db-search.feature
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,34 @@ Feature: Search through the database
"""
And the return code should be 1

When I try `wp db search 'regex error)' --regex`
Then STDERR should be:
"""
Error: The regex pattern 'regex error)' with default delimiter 'chr(1)' and no flags fails.
"""
And the return code should be 1

When I try `wp db search 'regex error)' --regex --regex-flags=u`
Then STDERR should be:
"""
Error: The regex pattern 'regex error)' with default delimiter 'chr(1)' and flags 'u' fails.
"""
And the return code should be 1

When I try `wp db search 'regex error)' --regex --regex-delimiter=/`
Then STDERR should be:
"""
Error: The regex '/regex error)/' fails.
"""
And the return code should be 1

When I try `wp db search 'regex error)' --regex --regex-delimiter=/ --regex-flags=u`
Then STDERR should be:
"""
Error: The regex '/regex error)/u' fails.
"""
And the return code should be 1

When I run `wp db search '[0-9é]+?https:' --regex --regex-flags=u --before_context=0 --after_context=0`
Then STDOUT should contain:
"""
Expand Down
12 changes: 10 additions & 2 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -818,9 +818,11 @@ public function search( $args, $assoc_args ) {

if ( ( $regex = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex', false ) ) ) {
$regex_flags = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex-flags', false );
$regex_delimiter = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex-delimiter', chr( 1 ) );
$default_regex_delimiter = false;
$regex_delimiter = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex-delimiter', '' );
if ( '' === $regex_delimiter ) {
$regex_delimiter = chr( 1 );
$default_regex_delimiter = true;
}
}

Expand All @@ -841,7 +843,13 @@ public function search( $args, $assoc_args ) {
$search_regex .= $regex_flags;
}
if ( false === @preg_match( $search_regex, '' ) ) {
WP_CLI::error( "The regex '$search_regex' fails." );
if ( $default_regex_delimiter ) {
$flags_msg = $regex_flags ? "flags '$regex_flags'" : "no flags";
$msg = "The regex pattern '$search' with default delimiter 'chr(1)' and {$flags_msg} fails.";
} else {
$msg = "The regex '$search_regex' fails.";
}
WP_CLI::error( $msg );
}
} else {
$search_regex = '#' . preg_quote( $search, '#' ) . '#i';
Expand Down

0 comments on commit 1379a28

Please sign in to comment.