Skip to content
46 changes: 46 additions & 0 deletions features/shell.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
"""
11 changes: 10 additions & 1 deletion src/Shell_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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,
Expand All @@ -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<WP_CLI\Shell\REPL> $class
Expand All @@ -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 );
}
Expand Down
17 changes: 14 additions & 3 deletions src/WP_CLI/Shell/REPL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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() );
}
}
Expand Down
Loading