Jelajahi Sumber

[Console] added the possibility to pass a default value for DialodHelper::askAndValidate()

Fabien Potencier 14 tahun lalu
induk
melakukan
5ad2ff0595
1 mengubah file dengan 4 tambahan dan 3 penghapusan
  1. 4 3
      src/Symfony/Component/Console/Helper/DialogHelper.php

+ 4 - 3
src/Symfony/Component/Console/Helper/DialogHelper.php

@@ -32,7 +32,7 @@ class DialogHelper extends Helper
     public function ask(OutputInterface $output, $question, $default = null)
     {
         // @codeCoverageIgnoreStart
-        $output->writeln($question);
+        $output->write($question);
 
         $ret = trim(fgets(STDIN));
 
@@ -78,12 +78,13 @@ class DialogHelper extends Helper
      * @param string|array    $question
      * @param callback        $validator A PHP callback
      * @param integer         $attempts Max number of times to ask before giving up (false by default, which means infinite)
+     * @param string          $default  The default answer if none is given by the user
      *
      * @return mixed
      *
      * @throws \Exception When any of the validator returns an error
      */
-    public function askAndValidate(OutputInterface $output, $question, $validator, $attempts = false)
+    public function askAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $default = null)
     {
         // @codeCoverageIgnoreStart
         $error = null;
@@ -92,7 +93,7 @@ class DialogHelper extends Helper
                 $output->writeln($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
             }
 
-            $value = $this->ask($output, $question, null);
+            $value = $this->ask($output, $question, $default);
 
             try {
                 return call_user_func($validator, $value);