Skip to content

Commit 5b4aece

Browse files
committed
Options before/after_context, other log options to env vars.
1 parent 5d2901e commit 5b4aece

File tree

2 files changed

+25
-60
lines changed

2 files changed

+25
-60
lines changed

features/search-replace.feature

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ Feature: Do global search/replace
625625
When I run `wp post create --post_title='Title_baz__baz_' --post_content='Content_baz_12345678901234567890_baz_12345678901234567890' --porcelain`
626626
Then save STDOUT as {POST_ID}
627627

628-
When I run `wp search-replace '_baz_' '_' --dry-run --log --log_context=10`
628+
When I run `wp search-replace '_baz_' '_' --dry-run --log --before_context=10 --after_context=10`
629629
Then STDOUT should contain:
630630
"""
631631
Success: 2 replacements to be made.
@@ -673,7 +673,7 @@ Feature: Do global search/replace
673673
And STDERR should be empty
674674

675675
# kana with diacritic and decomposed "a" + umlaut.
676-
When I run `wp search-replace '_baz_' '_バäz_' --log=- --log_context=10,20`
676+
When I run `wp search-replace '_baz_' '_バäz_' --log=- --before_context=10 --after_context=20`
677677
Then STDOUT should contain:
678678
"""
679679
Success: Made 2 replacements.
@@ -687,7 +687,7 @@ Feature: Do global search/replace
687687
And STDERR should be empty
688688

689689
# Testing UTF-8 context
690-
When I run `wp search-replace 'z_' 'zzzz_' --log --log_context=2,1`
690+
When I run `wp search-replace 'z_' 'zzzz_' --log --before_context=2 --after_context=1`
691691
Then STDOUT should contain:
692692
"""
693693
Success: Made 2 replacements.
@@ -716,7 +716,7 @@ Feature: Do global search/replace
716716
_baz1_ _baz1_12345678901234567890123456789012345678901234567890_baz1_ _baz1_1234567890123456789012345678901234567890
717717
"""
718718

719-
When I run `wp search-replace '_baz1_' '_bar1_' wp_options --log --log_context=10`
719+
When I run `wp search-replace '_baz1_' '_bar1_' wp_options --log --before_context=10 --after_context=10`
720720
Then STDOUT should contain:
721721
"""
722722
< _baz1_ _baz1_1234567890 [...] 1234567890_baz1_ _baz1_1234567890
@@ -726,7 +726,7 @@ Feature: Do global search/replace
726726

727727
When I run `wp option set foobar2 '12345678901234567890_bar2_1234567890_bar2_ _bar2_ _bar2_'`
728728

729-
When I run `wp search-replace '_bar2_' '_baz2baz2_' wp_options --log --log_context=10`
729+
When I run `wp search-replace '_bar2_' '_baz2baz2_' wp_options --log --before_context=10 --after_context=10`
730730
Then STDOUT should contain:
731731
"""
732732
< 1234567890_bar2_1234567890 [...] 1234567890_bar2_ _bar2_ _bar2_
@@ -740,7 +740,7 @@ Feature: Do global search/replace
740740
12345678901234567890_baz2baz2_1234567890_baz2baz2_ _baz2baz2_ _baz2baz2_
741741
"""
742742

743-
When I run `wp search-replace '_baz2baz2_' '_barz2_' wp_options --log --log_context=10,4`
743+
When I run `wp search-replace '_baz2baz2_' '_barz2_' wp_options --log --before_context=10 --after_context=4`
744744
Then STDOUT should contain:
745745
"""
746746
< 1234567890_baz2baz2_1234 [...] 1234567890_baz2baz2_ _baz2baz2_ _baz2baz2_
@@ -778,7 +778,7 @@ Feature: Do global search/replace
778778
When I run `wp post create --post_title='Title_baz__boz_' --post_content='Content_baz_1234567890_bez_1234567890_biz_1234567890_boz_1234567890_buz_' --porcelain`
779779
Then save STDOUT as {POST_ID}
780780

781-
When I run `wp search-replace '_b[aeiou]z_' '_bz_' --regex --dry-run --log --log_context=11`
781+
When I run `wp search-replace '_b[aeiou]z_' '_bz_' --regex --dry-run --log --before_context=11 --after_context=11`
782782
Then STDOUT should contain:
783783
"""
784784
Success: 2 replacements to be made.
@@ -802,7 +802,7 @@ Feature: Do global search/replace
802802
"""
803803
And STDERR should be empty
804804

805-
When I run `wp search-replace '_b([aeiou])z_' '_$1b\\1z_\0' --regex --log --log_context=11`
805+
When I run `wp search-replace '_b([aeiou])z_' '_$1b\\1z_\0' --regex --log --before_context=11 --after_context=11`
806806
Then STDOUT should contain:
807807
"""
808808
Success: Made 2 replacements.
@@ -837,15 +837,15 @@ Feature: Do global search/replace
837837
Scenario: Logging with prefixes and custom colors
838838
Given a WP install
839839

840-
When I run `wp search-replace Just Yet --dry-run --log --log_prefixes='- ,+ '`
840+
When I run `WP_CLI_SEARCH_REPLACE_LOG_PREFIXES='- ,+ ' wp search-replace Just Yet --dry-run --log`
841841
Then STDOUT should contain:
842842
"""
843843
- Just another WordPress site
844844
+ Yet another WordPress site
845845
"""
846846
And STDERR should be empty
847847

848-
When I run `wp search-replace Just Yet --dry-run --log --log_prefixes=,`
848+
When I run `WP_CLI_SEARCH_REPLACE_LOG_PREFIXES=',' wp search-replace Just Yet --dry-run --log`
849849
Then STDOUT should not contain:
850850
"""
851851
< Just
@@ -876,7 +876,7 @@ Feature: Do global search/replace
876876
"""
877877
And STDERR should be empty
878878

879-
When I run `SHELL_PIPE=0 wp search-replace WordPress WP --dry-run --log --log_colors=%b,%r,%g`
879+
When I run `SHELL_PIPE=0 WP_CLI_SEARCH_REPLACE_LOG_COLORS='%b,%r,%g' wp search-replace WordPress WP --dry-run --log`
880880
Then STDOUT should contain:
881881
"""
882882
wp_options.option_value:
@@ -888,7 +888,7 @@ Feature: Do global search/replace
888888
"""
889889
And STDERR should be empty
890890

891-
When I run `SHELL_PIPE=0 wp search-replace WordPress WP --dry-run --log=replace.log --log_colors=%b,%r,%g`
891+
When I run `SHELL_PIPE=0 WP_CLI_SEARCH_REPLACE_LOG_COLORS='%b,%r,%g' wp search-replace WordPress WP --dry-run --log=replace.log`
892892
Then STDOUT should not contain:
893893
"""
894894
wp_options.option_value
@@ -920,7 +920,7 @@ Feature: Do global search/replace
920920
"""
921921
And STDERR should be empty
922922

923-
When I run `SHELL_PIPE=0 wp search-replace WordPress WP --dry-run --log --log_colors=,,`
923+
When I run `SHELL_PIPE=0 WP_CLI_SEARCH_REPLACE_LOG_COLORS=',,' wp search-replace WordPress WP --dry-run --log`
924924
Then STDOUT should contain:
925925
"""
926926
wp_options.option_value:

src/Search_Replace_Command.php

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -122,44 +122,11 @@ class Search_Replace_Command extends WP_CLI_Command {
122122
* : Log the items changed. If <file> is not supplied or is "-", will output to STDOUT.
123123
* Warning: causes a significant slow down, similar or worse to enabling --precise or --regex.
124124
*
125-
* [--log_context=<before_num,after_num>]
126-
* : Number of characters to display before and after the match. One number sets both before and after. Two comma-separated numbers set before and after respectively. Defaults to 40. Ignored if not logging.
127-
*
128-
* [--log_prefixes=<old_prefix,new_prefix>]
129-
* : The old and new prefixes to prepend to the log lines for old match and new replacement, comma-separated. Defaults to '< ,> '. Use ',' for no prefixes. Ignored if not logging.
130-
*
131-
* [--log_colors=<table_column_id_color,old_color,new_color>]
132-
* : Percent color codes to use, comma-separated. The first is for 'table:column:id', default '%B'. The second is for the old match, default '%R'. The third is for the new replacement, default '%G'. The defaults are only used if logging to STDOUT. Use ',,' for no colors. Ignored if not logging.
133-
*
134-
* The percent color codes available are:
135-
* '%y' Yellow (dark) (mustard)
136-
* '%g' Green (dark)
137-
* '%b' Blue (dark)
138-
* '%r' Red (dark)
139-
* '%m' Magenta (dark)
140-
* '%c' Cyan (dark)
141-
* '%w' White (dark) (light gray)
142-
* '%k' Black
143-
* '%Y' Yellow (bright)
144-
* '%G' Green (bright)
145-
* '%B' Blue (bright)
146-
* '%R' Red (bright)
147-
* '%M' Magenta (bright)
148-
* '%C' Cyan (bright)
149-
* '%W' White
150-
* '%K' Black (bright) (dark gray)
151-
* '%3' Yellow background (dark) (mustard)
152-
* '%2' Green background (dark)
153-
* '%4' Blue background (dark)
154-
* '%1' Red background (dark)
155-
* '%5' Magenta background (dark)
156-
* '%6' Cyan background (dark)
157-
* '%7' White background (dark) (light gray)
158-
* '%0' Black background
159-
* '%8' Reverse
160-
* '%U' Underline
161-
* '%F' Blink (unlikely to work)
162-
* They can be concatenated.
125+
* [--before_context=<num>]
126+
* : For logging, number of characters to display before the old match and the new replacement. Default 40. Ignored if not logging.
127+
*
128+
* [--after_context=<num>]
129+
* : For logging, number of characters to display after the old match and the new replacement. Default 40. Ignored if not logging.
163130
*
164131
* ## EXAMPLES
165132
*
@@ -254,23 +221,21 @@ public function __invoke( $args, $assoc_args ) {
254221
}
255222
}
256223
if ( $this->log_handle ) {
257-
if ( null !== ( $log_context = \WP_CLI\Utils\get_flag_value( $assoc_args, 'log_context' ) ) && preg_match( '/^([0-9]+)(,[0-9]+)?$/', $log_context, $matches ) ) {
258-
if ( isset( $matches[1] ) ) {
259-
$this->log_before_context = $this->log_after_context = (int) $matches[1];
260-
}
261-
if ( isset( $matches[2] ) ) {
262-
$this->log_after_context = (int) substr( $matches[2], 1 );
263-
}
224+
if ( null !== ( $before_context = \WP_CLI\Utils\get_flag_value( $assoc_args, 'before_context' ) ) && preg_match( '/^[0-9]+$/', $before_context ) ) {
225+
$this->log_before_context = (int) $before_context;
226+
}
227+
if ( null !== ( $after_context = \WP_CLI\Utils\get_flag_value( $assoc_args, 'after_context' ) ) && preg_match( '/^[0-9]+$/', $after_context ) ) {
228+
$this->log_after_context = (int) $after_context;
264229
}
265-
if ( null !== ( $log_prefixes = \WP_CLI\Utils\get_flag_value( $assoc_args, 'log_prefixes' ) ) && preg_match( '/^([^,]*),([^,]*)$/', $log_prefixes, $matches ) ) {
230+
if ( false !== ( $log_prefixes = getenv( 'WP_CLI_SEARCH_REPLACE_LOG_PREFIXES' ) ) && preg_match( '/^([^,]*),([^,]*)$/', $log_prefixes, $matches ) ) {
266231
$this->log_prefixes = array( $matches[1], $matches[2] );
267232
}
268233
if ( STDOUT === $this->log_handle ) {
269234
$default_log_colors = array( 'log_table_column_id' => '%B', 'log_old' => '%R', 'log_new' => '%G' );
270235
} else {
271236
$default_log_colors = array( 'log_table_column_id' => '', 'log_old' => '', 'log_new' => '' );
272237
}
273-
if ( null !== ( $log_colors = \WP_CLI\Utils\get_flag_value( $assoc_args, 'log_colors' ) ) && preg_match( '/^([^,]*),([^,]*),([^,]*)$/', $log_colors, $matches ) ) {
238+
if ( false !== ( $log_colors = getenv( 'WP_CLI_SEARCH_REPLACE_LOG_COLORS' ) ) && preg_match( '/^([^,]*),([^,]*),([^,]*)$/', $log_colors, $matches ) ) {
274239
$default_log_colors = array( 'log_table_column_id' => $matches[1], 'log_old' => $matches[2], 'log_new' => $matches[3] );
275240
}
276241
$this->log_colors = self::get_colors( $assoc_args, $default_log_colors );

0 commit comments

Comments
 (0)