Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit601bb87

Browse files
committed
Merge branch '2.1'
Conflicts:components/console/introduction.rst
2 parentsb66cc06 +568392f commit601bb87

File tree

9 files changed

+251
-134
lines changed

9 files changed

+251
-134
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
..index::
2+
single: Console Helpers; Dialog Helper
3+
4+
Dialog Helper
5+
=============
6+
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
9+
by calling:method:`Symfony\\Component\\Console\\Command\\Command::getHelperSet`::
10+
11+
$dialog = $this->getHelperSet()->get('dialog');
12+
13+
All the methods inside the Dialog Helper have an
14+
:class:`Symfony\\Component\\Console\\Output\\OutputInterface` as first the
15+
argument, the question as the second argument and the default value as last
16+
argument.
17+
18+
Asking the User for confirmation
19+
--------------------------------
20+
21+
Suppose you want to confirm an action before actually executing it. Add
22+
the following to you command::
23+
24+
// ...
25+
if (!$dialog->askConfirmation(
26+
$output,
27+
'<question>Continue with this action?</question>',
28+
false
29+
)) {
30+
return;
31+
}
32+
33+
In this case, the user will be asked "Continue with this action", and will return
34+
``true`` if the user answers with ``y`` or false in any other case. The third
35+
argument to ``askConfirmation`` is the default value to return if the user doesn't
36+
enter any input.
37+
38+
Asking the User for Information
39+
-------------------------------
40+
41+
You can also ask question with more than a simple yes/no answer. For instance,
42+
if you want to know a bundle name, you can add this to your command::
43+
44+
// ...
45+
$bundle = $dialog->ask(
46+
$output,
47+
'Please enter the name of the bundle',
48+
'AcmeDemoBundle'
49+
);
50+
51+
The user will be asked "Please enter the name of the bundle". She can type
52+
some name which will be returned by the ``ask`` method. If she leaves it empty,
53+
the default value (``AcmeDemoBundle`` here) is returned.
54+
55+
Hiding the User's Response
56+
~~~~~~~~~~~~~~~~~~~~~~~~~~
57+
58+
..versionadded::2.2
59+
The ``askHiddenResponse`` method was added in Symfony 2.2.
60+
61+
You can also ask question and hide the response. This is particularly
62+
convenient for passwords::
63+
64+
$dialog = $this->getHelperSet()->get('dialog');
65+
$password = $dialog->askHiddenResponse(
66+
$output,
67+
'What is the database password?',
68+
false
69+
);
70+
71+
..caution::
72+
73+
When you ask for a hidden response, Symfony will use either a binary, change
74+
stty mode or use another trick to hide the response. If none is available,
75+
it will fallback and allow the response to be visible unless you pass ``false``
76+
as the third argument like in the example above. In this case, a RuntimeException
77+
would be thrown.
78+
79+
Validating the Answer
80+
---------------------
81+
82+
You can even validate the answer. For instance, in the last example you asked
83+
for the bundle name. Following the Symfony2 naming conventions, it should
84+
be suffixed with ``Bundle``. You can validate that by using the
85+
:method:`Symfony\\Component\\Console\\Helper\\DialogHelper::askAndValidate`
86+
method::
87+
88+
// ...
89+
$bundle = $dialog->askAndValidate(
90+
$output,
91+
'Please enter the name of the bundle',
92+
function ($answer) {
93+
if ('Bundle' !== substr($answer, -6)) {
94+
throw new \RunTimeException(
95+
'The name of the bundle should be suffixed with \'Bundle\''
96+
);
97+
}
98+
},
99+
false,
100+
'AcmeDemoBundle'
101+
);
102+
103+
This methods has 2 new arguments, the full signature is::
104+
105+
askAndValidate(
106+
OutputInterface $output,
107+
string|array $question,
108+
callback $validator,
109+
integer $attempts = false,
110+
string $default = null
111+
)
112+
113+
The ``$validator`` is a callback which handles the validation. It should
114+
throw an exception if there is something wrong. The exception message is displayed
115+
in the console, so it is a good practice to put some useful information
116+
in it.
117+
118+
You can set the max number of times to ask in the ``$attempts`` argument.
119+
If you reach this max number it will use the default value, which is given
120+
in the last argument. Using ``false`` means the amount of attempts is infinite.
121+
The user will be asked as long as he provides an invalid answer and will only
122+
be able to proceed if her input is valid.
123+
124+
Hiding the User's Response
125+
~~~~~~~~~~~~~~~~~~~~~~~~~~
126+
127+
..versionadded::2.2
128+
The ``askHiddenResponseAndValidate`` method was added in Symfony 2.2.
129+
130+
You can also ask and validate a hidden response::
131+
132+
$dialog = $this->getHelperSet()->get('dialog');
133+
134+
$validator = function ($value) {
135+
if (trim($value) == '') {
136+
throw new \Exception('The password can not be empty');
137+
}
138+
}
139+
140+
$password = $dialog->askHiddenResponseAndValidate(
141+
$output,
142+
'Please enter the name of the widget',
143+
$validator,
144+
20,
145+
false
146+
);
147+
148+
If you want to allow the response to be visible if it cannot be hidden for
149+
some reason, pass true as the fifth argument.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
..index::
2+
single: Console Helpers; Formatter Helper
3+
4+
Formatter Helper
5+
================
6+
7+
The Formatter helpers provides functions to format the output with colors.
8+
You can do more advanced things with this helper than you can in
9+
:ref:`components-console-coloring`.
10+
11+
The ``formatter`` helper is included in the default helper set, which you can
12+
get by calling
13+
:method:`Symfony\\Component\\Console\\Command\\Command::getHelperSet`::
14+
15+
$formatter = $this->getHelperSet()->get('formatter');
16+
17+
The methods return a string, which you'll usually render to the console by
18+
passing it to the
19+
:method:`OutputInterface::writeln<Symfony\\Component\\Console\\Output\\OutputInterface::writeln>` method.
20+
21+
Print Messages in a Section
22+
---------------------------
23+
24+
Symfony offers a defined style when printing a message that belongs to some
25+
"section". It prints the section in color and with brackets around it and the
26+
actual message to the right of this. Minus the color, it looks like this:
27+
28+
..code-block::text
29+
30+
[SomeSection] Here is some message related to that section
31+
32+
To reproduce this style, you can use the
33+
:method:`Symfony\\Component\\Console\\Helper\\FormatterHelper::formatSection`
34+
method::
35+
36+
$formattedLine = $formatter->formatSection(
37+
'SomeSection',
38+
'Here is some message related to that section'
39+
);
40+
$output->writeln($formattedLine);
41+
42+
Print Messages in a Block
43+
-------------------------
44+
45+
Sometimes you want to be able to print a whole block of text with a background
46+
color. Symfony uses this when printing error messages.
47+
48+
If you print your error message on more than one line manually, you will
49+
notice that the background is only as long as each individual line. Use the
50+
:method:`Symfony\\Component\\Console\\Helper\\FormatterHelper::formatBlock`
51+
to generate a block output::
52+
53+
$errorMessages = array('Error!', 'Something went wrong');
54+
$formattedBlock = $formatter->formatBlock($errorMessages, 'error');
55+
$output->writeln($formattedBlock);
56+
57+
As you can see, passing an array of messages to the
58+
:method:`Symfony\\Component\\Console\\Helper\\FormatterHelper::formatBlock`
59+
method creates the desired output. If you pass ``true`` as third parameter, the
60+
block will be formatted with more padding (one blank line above and below the
61+
messages and 2 spaces on the left and right).
62+
63+
The exact "style" you use in the block is up to you. In this case, you're using
64+
the pre-defined ``error`` style, but there are other styles, or you can create
65+
your own. See:ref:`components-console-coloring`.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
..index::
2+
single: Console; Console Helpers
3+
4+
The Console Helpers
5+
===================
6+
7+
..toctree::
8+
:hidden:
9+
10+
dialoghelper
11+
formatterhelper
12+
13+
The Console Components comes with some usefull helpers. These helpers contain
14+
function to ease some common tasks.
15+
16+
..include::map.rst.inc
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* :doc:`/components/console/helpers/dialoghelper`
2+
* :doc:`/components/console/helpers/formatterhelper`

‎components/console/index.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ Console
77
introduction
88
usage
99
single_command_tool
10+
11+
helpers/index

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp