diff --git a/features/shell.feature b/features/shell.feature index 6cf104ce..fb187641 100644 --- a/features/shell.feature +++ b/features/shell.feature @@ -113,3 +113,49 @@ Feature: WordPress REPL Error: The 'shutdown' hook has not fired yet """ And the return code should be 1 + + Scenario: Quiet mode suppresses return value output + Given a WP install + And a session file: + """ + $a = "hello"; + """ + + When I run `wp shell --basic --quiet < session` + Then STDOUT should be empty + + Scenario: Quiet mode still shows echo output + Given a WP install + And a session file: + """ + echo "test output"; + """ + + When I run `wp shell --basic --quiet < session` + Then STDOUT should contain: + """ + test output + """ + + Scenario: Quiet mode with expression + Given a WP install + And a session file: + """ + get_bloginfo('name'); + """ + + When I run `wp shell --basic --quiet < session` + Then STDOUT should be empty + + Scenario: Normal mode shows return value + Given a WP install + And a session file: + """ + $a = "hello"; + """ + + When I run `wp shell --basic < session` + Then STDOUT should contain: + """ + string(5) "hello" + """ diff --git a/src/Shell_Command.php b/src/Shell_Command.php index 7c89a7b2..65eb5eac 100644 --- a/src/Shell_Command.php +++ b/src/Shell_Command.php @@ -36,6 +36,11 @@ class Shell_Command extends WP_CLI_Command { * * # Start a shell, ensuring the 'init' hook has already fired. * $ wp shell --hook=init + * + * # Start a shell in quiet mode, suppressing return value output. + * $ wp shell --quiet + * wp> $a = "hello"; + * wp> */ public function __invoke( $_, $assoc_args ) { $hook = Utils\get_flag_value( $assoc_args, 'hook', '' ); @@ -71,6 +76,7 @@ public function __invoke( $_, $assoc_args ) { */ private function start_shell( $assoc_args ) { $class = WP_CLI\Shell\REPL::class; + $quiet = (bool) WP_CLI::get_config( 'quiet' ); $implementations = array( \Psy\Shell::class, @@ -98,7 +104,7 @@ private function start_shell( $assoc_args ) { /** * @var class-string $class */ - $repl = new $class( 'wp> ' ); + $repl = new $class( 'wp> ', $quiet ); $repl->start(); } } diff --git a/src/WP_CLI/Shell/REPL.php b/src/WP_CLI/Shell/REPL.php index 9415fc96..011f73c4 100644 --- a/src/WP_CLI/Shell/REPL.php +++ b/src/WP_CLI/Shell/REPL.php @@ -10,8 +10,11 @@ class REPL { private $history_file; - public function __construct( $prompt ) { + private $quiet; + + public function __construct( $prompt, $quiet = false ) { $this->prompt = $prompt; + $this->quiet = $quiet; $this->set_history_file(); } @@ -49,8 +52,10 @@ public function start() { if ( 0 < strlen( $out ) ) { echo rtrim( $out, "\n" ) . "\n"; } - echo '=> '; - var_dump( $evl ); + if ( ! $this->quiet ) { + echo '=> '; + var_dump( $evl ); + } fwrite( STDOUT, (string) ob_get_clean() ); } }