diff --git a/features/shell.feature b/features/shell.feature index 611f09e2..0b219302 100644 --- a/features/shell.feature +++ b/features/shell.feature @@ -220,3 +220,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 c22016f0..d77573de 100644 --- a/src/Shell_Command.php +++ b/src/Shell_Command.php @@ -58,6 +58,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> + * * @param string[] $_ Positional arguments. Unused. * @param array{basic?: bool, watch?: string} $assoc_args Associative arguments. */ @@ -109,6 +114,7 @@ private function start_shell( $assoc_args ) { } $class = WP_CLI\Shell\REPL::class; + $quiet = (bool) WP_CLI::get_config( 'quiet' ); $implementations = array( \Psy\Shell::class, @@ -132,6 +138,9 @@ private function start_shell( $assoc_args ) { if ( \Psy\Shell::class === $class ) { $shell = new Psy\Shell(); $shell->run(); + } elseif ( \Boris\Boris::class === $class ) { + $boris = new \Boris\Boris( 'wp> ' ); + $boris->start(); } else { /** * @var class-string $class @@ -141,7 +150,7 @@ private function start_shell( $assoc_args ) { } do { - $repl = new $class( 'wp> ' ); + $repl = new $class( 'wp> ', $quiet ); if ( $watch_path ) { $repl->set_watch_path( $watch_path ); } diff --git a/src/WP_CLI/Shell/REPL.php b/src/WP_CLI/Shell/REPL.php index bab27359..ce9c9856 100644 --- a/src/WP_CLI/Shell/REPL.php +++ b/src/WP_CLI/Shell/REPL.php @@ -10,14 +10,22 @@ class REPL { private $history_file; + /** @var bool Whether to suppress automatic output. */ + private $quiet; + private $watch_path; private $watch_mtime; const EXIT_CODE_RESTART = 10; - public function __construct( $prompt ) { + /** + * @param string $prompt Prompt to display. + * @param bool $quiet Whether to suppress automatic output. + */ + public function __construct( $prompt, $quiet = false ) { $this->prompt = $prompt; + $this->quiet = $quiet; $this->set_history_file(); } @@ -80,8 +88,11 @@ public function start() { if ( 0 < strlen( $__repl_output ) ) { echo rtrim( $__repl_output, "\n" ) . "\n"; } - echo '=> '; - var_dump( $__repl_eval_result ); + ob_start(); + if ( ! $this->quiet ) { + echo '=> '; + var_dump( $__repl_eval_result ); + } fwrite( STDOUT, (string) ob_get_clean() ); } }