From 25a08b330601068c649fca8ca4d5d84318dadf24 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Sun, 25 Aug 2013 17:17:36 -0700 Subject: [PATCH] We don't need a `prompt()` util right now. Instead, let's better utilize PHP cli tools --- php/WP_CLI/Dispatcher/Subcommand.php | 30 ++++++++++++++++++++++++++---- php/class-wp-cli.php | 19 ------------------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/php/WP_CLI/Dispatcher/Subcommand.php b/php/WP_CLI/Dispatcher/Subcommand.php index 1f191ead..5d91b9ca 100644 --- a/php/WP_CLI/Dispatcher/Subcommand.php +++ b/php/WP_CLI/Dispatcher/Subcommand.php @@ -38,6 +38,18 @@ class Subcommand extends CompositeCommand { ) ); } + private function prompt( $question, $default ) { + + try { + $response = \cli\prompt( $question, $default ); + } catch( \Exception $e ) { + \WP_CLI::line(); + return false; + } + + return $response; + } + private function prompt_args( $args, $assoc_args ) { $synopsis = $this->get_synopsis(); @@ -57,7 +69,7 @@ class Subcommand extends CompositeCommand { foreach( $spec as $key => $spec_arg ) { $current_prompt = ( $key + 1 ) . '/' . count( $spec ) . ' '; - $required = ! $spec_arg['optional']; + $default = ( $spec_arg['optional'] ) ? '' : false; // 'generic' permits arbitrary key=value (e.g. [--=] ) if ( 'generic' == $spec_arg['type'] ) { @@ -71,11 +83,18 @@ class Subcommand extends CompositeCommand { else $key_prompt = str_repeat( " ", strlen( $current_prompt ) ) . $key_token; - $key = \WP_CLI::prompt( $key_prompt, $required ); + $key = $this->prompt( $key_prompt, $default ); + if ( false === $key ) + return array( $args, $assoc_args ); + if ( $key ) { $key_prompt_count = strlen( $key_prompt ) - strlen( $value_token ) - 1; $value_prompt = str_repeat( " ", $key_prompt_count ) . '=' . $value_token; - $value = \WP_CLI::prompt( $value_prompt ); + + $value = $this->prompt( $value_prompt, $default ); + if ( false === $value ) + return array( $args, $assoc_args ); + $assoc_args[$key] = $value; $repeat = true; @@ -92,7 +111,10 @@ class Subcommand extends CompositeCommand { if ( 'flag' == $spec_arg['type'] ) $prompt .= ' (Y/n)'; - $response = \WP_CLI::prompt( $prompt, $required ); + $response = $this->prompt( $prompt, $default ); + if ( false === $response ) + return array( $args, $assoc_args ); + if ( $response ) { switch ( $spec_arg['type'] ) { case 'positional': diff --git a/php/class-wp-cli.php b/php/class-wp-cli.php index ba361a0c..da3a32be 100644 --- a/php/class-wp-cli.php +++ b/php/class-wp-cli.php @@ -165,25 +165,6 @@ class WP_CLI { } /** - * Prompt a user for some input - */ - static function prompt( $question, $required = false ) { - - if ( ! $required ) - $default = ''; - else - $default = false; - - try { - $response = \cli\prompt( $question, $default ); - } catch( Exception $e ) { - $response = ''; - } - return $response; - } - - - /** * Read a value, from various formats * * @param mixed $value -- 2.11.0