Skip to content

Commit 4de6361

Browse files
committed
Making a few changes to the existing description and adding docs for the Formatter Helper
1 parent bf94952 commit 4de6361

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

components/console/helpers/dialoghelper.rst

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
Dialog Helper
55
=============
66

7-
The Dialog Helper provides functions to ask the user for more information.
8-
9-
The DialogHelper is included in the default helper set, which you can get
7+
The Dialog Helper provides functions to ask the user for more information.
8+
It is included in the default helper set, which you can get
109
by calling :method:`Symfony\\Component\\Console\\Command\\Command::getHelperSet`::
1110

1211
$dialog = $this->getHelperSet()->get('dialog');
@@ -30,16 +29,16 @@ the following to your command::
3029
return;
3130
}
3231

33-
In this case, the user will be asked "Continue with this action", and unless
34-
they answer with ``y``, the task will stop running. The third argument to
35-
``askConfirmation`` is the default value to return if the user doesn't enter
36-
any input.
32+
In this case, the user will be asked "Continue with this action", and will return
33+
``true`` if the user answers with ``y`` or false in any other case. The third
34+
argument to ``askConfirmation`` is the default value to return if the user doesn't
35+
enter any input.
3736

3837
Asking the User for information
3938
-------------------------------
4039

4140
You can also ask question with more than a simple yes/no answer. For instance,
42-
you want to know a bundle name, you can add this to your command::
41+
if you want to know a bundle name, you can add this to your command::
4342

4443
// ...
4544
$bundle = $dialog->ask(
@@ -49,8 +48,8 @@ you want to know a bundle name, you can add this to your command::
4948
);
5049

5150
The user will be asked "Please enter the name of the bundle". They can type
52-
some name or if they leave it empty the default value (``AcmeDemoBundle`` here)
53-
is used. This value will be returned.
51+
some name which will be returned by the ``ask`` method. If they leave it empty
52+
the default value (``AcmeDemoBundle`` here) is returned.
5453

5554
Validating the answer
5655
---------------------
@@ -87,10 +86,12 @@ This methods has 2 new arguments, the full signature is::
8786
)
8887

8988
The ``$validator`` is a callback which handles the validation. It should
90-
throw an exception if there is something wrong. The exception message displayed
89+
throw an exception if there is something wrong. The exception message is displayed
9190
in the console, so it is a good practice to put some usefull information
9291
in it.
9392

9493
You can set the max number of times to ask in the ``$attempts`` argument.
9594
If we reach this max number it will use the default value, which is given
96-
in the last argument. This is ``false`` by default, which means it is infinite.
95+
in the last argument. Using ``false`` means the amount of attempts is infinite.
96+
The user will be asked as long as he provides an invalid answer and will only
97+
be able to proceed if his input is valid.

components/console/helpers/formatterhelper.rst

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,42 @@ The Formatter helpers provides functions to format the output with colors.
88
You can do more advanced things with this helper than the things in
99
:ref:`components-console-coloring`.
1010

11-
The FormatterHelper is included in the default helper set, which you can
11+
The Formatter Helper is included in the default helper set, which you can
1212
get by calling
1313
:method:`Symfony\\Component\\Console\\Command\\Command::getHelperSet`::
1414

1515
$formatter = $this->getHelperSet()->get('formatter');
1616

17-
The methods return a string with the data for the console, you should decide
18-
what you are going to do with that data. In most cases you want to write
17+
The methods return a string which in most cases you want to write
1918
that data to the console with
2019
:method:`Symfony\\Component\\Console\\Output\\Output::writeln`.
2120

2221
Print messages in a section
2322
---------------------------
2423

25-
Assume you want to print
24+
Symfony uses a defined style when printing to the command line.
25+
It prints the section in color and with brackets around and the
26+
actual message behind this section indication.
27+
28+
To reproduce this style, you can use the `` formatSection`` method like this::
29+
30+
$formattedLine = $formatter->formatSection('SomeCommand', 'Some output from within the SomeCommand');
31+
$output->writeln($formattedLine);
32+
33+
Print messages in a block
34+
-------------------------
35+
36+
Sometimes you want to be able to print a whole block of text with a background
37+
color. Symfony uses this when printing error messages.
38+
39+
If you print your error message on more than one line manually, you will
40+
notice that the background is only as long as each individual line. You
41+
can use the `formatBlock` method to create a real block output::
42+
43+
$errorMessages = array('Error!', 'Something went wrong');
44+
$formattedBlock = $formatter->formatBlock($errorMessages, 'error');
45+
46+
As you can see, passing an array of messages to the `formatBlock` method creates
47+
the desired output. If you pass `true` as third parameter, the block will be
48+
formatted with more padding (one blank line above and below the messages and 2
49+
spaces on the left and right).

components/console/helpers/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ Console Helpers
99

1010
dialoghelper
1111

12-
1312
The Console Components comes with some usefull helpers. These helpers contain
1413
function to ease some common tasks.
1514

0 commit comments

Comments
 (0)