Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation about hidden response method in Console helper #1822

Merged
merged 2 commits into from Nov 16, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 72 additions & 2 deletions components/console/introduction.rst
Expand Up @@ -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'
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part is documenting a method which exists since 2.0, so you should send it as a separate PR to the 2.0 branch (and btw, we may want to organize things a bit, as mentionned in #1811)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, i'll do it tonight

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @stof exactly. We should first make a PR against 2.0 with everything you have here except for anything that's new in 2.2. I'll then merge that in and then we can rebase this PR after that's been done.

I also agree totally with @stof in #1811, though I think we can either do that right now, or get all of this stuff added and then refactor. @romainneutron, if you're comfortable refactoring into some new sections - great :). Otherwise, we'll handle that later.

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hello @stof , @weaverryan ,

I'm always happy to give some time, but I'm quite busy lately.
Let's refactor into some new sections 👍 !


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 ``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) == '') {
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
-------------------------

Expand All @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down