OSDN Git Service

Exit on end-of-file
authorNikolay Bachiyski <nb@nikolay.bg>
Thu, 14 Feb 2013 14:28:40 +0000 (16:28 +0200)
committerNikolay Bachiyski <nb@nikolay.bg>
Thu, 14 Feb 2013 14:28:40 +0000 (16:28 +0200)
It's a common practice shells to exit on end-of-file. WP-CLI doesn't do
this.

I traced the reason to the fact we're not checking the exit code from
the `read` builtin. If it timeouts or receives end-of-file, it's
non-zero. In both these cases it makes sense to exit.

php/commands/shell.php

index c0e7376..75a9015 100644 (file)
@@ -52,6 +52,10 @@ class Shell_Command extends \WP_CLI_Command {
 
                $line = fgets( $fp );
 
+               if ( !$line ) {
+                       $line = 'exit';
+               }
+
                return trim( $line );
        }
 
@@ -61,6 +65,7 @@ class Shell_Command extends \WP_CLI_Command {
                        sprintf( 'history -r %s', escapeshellarg( $history_path ) ),
                        'LINE=""',
                        sprintf( 'read -re -p %s LINE', escapeshellarg( $prompt ) ),
+                       '[ $? -eq 0 ] || exit',
                        'history -s "$LINE"',
                        sprintf( 'history -w %s', escapeshellarg( $history_path ) ),
                        'echo $LINE'