From d304649aaaf7e9790dc2b29cb631a5787801772d Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Tue, 16 Oct 2012 02:11:31 +0200 Subject: [PATCH 1/2] Add documentation about hidden response method in Console helper --- components/console/introduction.rst | 74 ++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/components/console/introduction.rst b/components/console/introduction.rst index 256b12d91af..4c4efb5056e 100755 --- a/components/console/introduction.rst +++ b/components/console/introduction.rst @@ -287,6 +287,76 @@ if you needed to know the name of something, you might do the following:: 'foo' ); +.. versionadded:: 2.2 + The ``askHiddenResponse`` method was added in Symfony 2.2. + +You can also ask question and hide the response. This is particularly +convenient for passwords:: + + $dialog = $this->getHelperSet()->get('dialog'); + $password = $dialog->askHiddenResponse( + $output, + 'What is the database password ?', + false + ); + +.. caution:: + + When you ask an hidden response, Symfony will use either a binary, change + stty mode or use another trick to hide the response. If none is available, + it will fallback on the classic question unless you pass `false` as the + third argument like in the example above. In this case, a RuntimeException + would be thrown. + +Ask and validate response +------------------------- + +You can easily ask question and validate response with built-in methods:: + + $dialog = $this->getHelperSet()->get('dialog'); + + $validator = function ($value) { + if (trim($value) === '') { + throw new \Exception('The value can not be empty'); + } + } + + $password = $dialog->askAndValidate( + $output, + 'Please enter the name of the widget', + $validator, + 20, + 'foo' + ); + +The validation callback can be any callable PHP function, the fourth argument is +the maximum number of attempts, set it to `false` for unlimited attempts. The +fifth argument is the default value. + +.. versionadded:: 2.2 + The ``askHiddenResponse`` method was added in Symfony 2.2. + +You can also ask and validate hidden response:: + + $dialog = $this->getHelperSet()->get('dialog'); + + $validator = function ($value) { + if (trim($value) === '') { + throw new \Exception('The password can not be empty'); + } + } + + $password = $dialog->askHiddenResponseAndValidate( + $output, + 'Please enter the name of the widget', + $validator, + 20, + false + ); + +If you want to fallback on classic question in case hidden response can not be +provided, pass true as fifth argument. + Displaying a Progress Bar ------------------------- @@ -310,7 +380,7 @@ pass it a total number of units, and advance the progress as your command execut // advance the progress bar 1 unit $progress->advance(); - } + } $progress->finish(); @@ -346,7 +416,7 @@ To see other available options, check the API documentation for to a high number. For example, if you're iterating over a large number of items, consider a smaller "step" number that updates on only some iterations:: - + $progress->start($output, 500); $i = 0; while ($i++ < 50000) { From 0b25facaa1acf201639470d4cb1fcb0c9699926a Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Tue, 16 Oct 2012 11:00:09 +0200 Subject: [PATCH 2/2] Fix CS --- components/console/introduction.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/components/console/introduction.rst b/components/console/introduction.rst index 4c4efb5056e..c6bde934c1e 100755 --- a/components/console/introduction.rst +++ b/components/console/introduction.rst @@ -304,7 +304,7 @@ convenient for passwords:: When you ask an hidden response, Symfony will use either a binary, change stty mode or use another trick to hide the response. If none is available, - it will fallback on the classic question unless you pass `false` as the + it will fallback on the classic question unless you pass ``false`` as the third argument like in the example above. In this case, a RuntimeException would be thrown. @@ -316,7 +316,7 @@ You can easily ask question and validate response with built-in methods:: $dialog = $this->getHelperSet()->get('dialog'); $validator = function ($value) { - if (trim($value) === '') { + if (trim($value) == '') { throw new \Exception('The value can not be empty'); } } @@ -330,18 +330,18 @@ You can easily ask question and validate response with built-in methods:: ); The validation callback can be any callable PHP function, the fourth argument is -the maximum number of attempts, set it to `false` for unlimited attempts. The +the maximum number of attempts, set it to ``false`` for unlimited attempts. The fifth argument is the default value. .. versionadded:: 2.2 - The ``askHiddenResponse`` method was added in Symfony 2.2. + The ``askHiddenResponseAndValidate`` method was added in Symfony 2.2. You can also ask and validate hidden response:: $dialog = $this->getHelperSet()->get('dialog'); $validator = function ($value) { - if (trim($value) === '') { + if (trim($value) == '') { throw new \Exception('The password can not be empty'); } }