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

Commit9102bc3

Browse files
committed
minor#5648 Minor fixes for the QuestionHelper documentation (javiereguiluz)
This PR was squashed before being merged into the 2.7 branch (closes#5648).Discussion----------Minor fixes for the QuestionHelper documentation| Q | A| ------------- | ---| Doc fix? | yes| New docs? | no| Applies to | all| Fixed tickets | -Commits-------25607ae Minor fixes for the QuestionHelper documentation
2 parentsa1cf5e9 +25607ae commit9102bc3

File tree

1 file changed

+102
-67
lines changed

1 file changed

+102
-67
lines changed

‎components/console/helpers/questionhelper.rst

Lines changed: 102 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ helper set, which you can get by calling
1313

1414
The Question Helper has a single method
1515
:method:`Symfony\\Component\\Console\\Command\\Command::ask` that needs an
16-
:class:`Symfony\\Component\\Console\\Output\\InputInterface` instance as the
16+
:class:`Symfony\\Component\\Console\\Input\\InputInterface` instance as the
1717
first argument, an:class:`Symfony\\Component\\Console\\Output\\OutputInterface`
1818
instance as the second argument and a
1919
:class:`Symfony\\Component\\Console\\Question\\Question` as last argument.
@@ -24,14 +24,22 @@ Asking the User for Confirmation
2424
Suppose you want to confirm an action before actually executing it. Add
2525
the following to your command::
2626

27-
use Symfony\Component\Console\Question\ConfirmationQuestion;
2827
// ...
28+
use Symfony\Component\Console\Question\ConfirmationQuestion;
2929

30-
$helper = $this->getHelper('question');
31-
$question = new ConfirmationQuestion('Continue with this action?', false);
30+
class YourCommand extends Command
31+
{
32+
// ...
33+
34+
public function execute(InputInterface $input, OutputInterface $output)
35+
{
36+
$helper = $this->getHelper('question');
37+
$question = new ConfirmationQuestion('Continue with this action?', false);
3238

33-
if (!$helper->ask($input, $output, $question)) {
34-
return;
39+
if (!$helper->ask($input, $output, $question)) {
40+
return;
41+
}
42+
}
3543
}
3644

3745
In this case, the user will be asked "Continue with this action?". If the user
@@ -66,11 +74,15 @@ You can also ask a question with more than a simple yes/no answer. For instance,
6674
if you want to know a bundle name, you can add this to your command::
6775

6876
use Symfony\Component\Console\Question\Question;
69-
// ...
7077

71-
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
78+
// ...
79+
public function execute(InputInterface $input, OutputInterface $output)
80+
{
81+
// ...
82+
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
7283

73-
$bundle = $helper->ask($input, $output, $question);
84+
$bundle = $helper->ask($input, $output, $question);
85+
}
7486

7587
The user will be asked "Please enter the name of the bundle". They can type
7688
some name which will be returned by the
@@ -86,20 +98,24 @@ which makes sure that the user can only enter a valid string
8698
from a predefined list::
8799

88100
use Symfony\Component\Console\Question\ChoiceQuestion;
89-
// ...
90101

91-
$helper = $this->getHelper('question');
92-
$question = new ChoiceQuestion(
93-
'Please select your favorite color (defaults to red)',
94-
array('red', 'blue', 'yellow'),
95-
0
96-
);
97-
$question->setErrorMessage('Color %s is invalid.');
102+
// ...
103+
public function execute(InputInterface $input, OutputInterface $output)
104+
{
105+
// ...
106+
$helper = $this->getHelper('question');
107+
$question = new ChoiceQuestion(
108+
'Please select your favorite color (defaults to red)',
109+
array('red', 'blue', 'yellow'),
110+
0
111+
);
112+
$question->setErrorMessage('Color %s is invalid.');
98113

99-
$color = $helper->ask($input, $output, $question);
100-
$output->writeln('You have just selected: '.$color);
114+
$color = $helper->ask($input, $output, $question);
115+
$output->writeln('You have just selected: '.$color);
101116

102-
// ... do something with the color
117+
// ... do something with the color
118+
}
103119

104120
The option which should be selected by default is provided with the third
105121
argument of the constructor. The default is ``null``, which means that no
@@ -120,18 +136,22 @@ feature using comma separated values. This is disabled by default, to enable
120136
this use:method:`Symfony\\Component\\Console\\Question\\ChoiceQuestion::setMultiselect`::
121137

122138
use Symfony\Component\Console\Question\ChoiceQuestion;
123-
// ...
124139

125-
$helper = $this->getHelper('question');
126-
$question = new ChoiceQuestion(
127-
'Please select your favorite colors (defaults to red and blue)',
128-
array('red', 'blue', 'yellow'),
129-
'0,1'
130-
);
131-
$question->setMultiselect(true);
140+
// ...
141+
public function execute(InputInterface $input, OutputInterface $output)
142+
{
143+
// ...
144+
$helper = $this->getHelper('question');
145+
$question = new ChoiceQuestion(
146+
'Please select your favorite colors (defaults to red and blue)',
147+
array('red', 'blue', 'yellow'),
148+
'0,1'
149+
);
150+
$question->setMultiselect(true);
132151

133-
$colors = $helper->ask($input, $output, $question);
134-
$output->writeln('You have just selected: ' . implode(', ', $colors));
152+
$colors = $helper->ask($input, $output, $question);
153+
$output->writeln('You have just selected: ' . implode(', ', $colors));
154+
}
135155

136156
Now, when the user enters ``1,2``, the result will be:
137157
``You have just selected: blue, yellow``.
@@ -146,13 +166,17 @@ You can also specify an array of potential answers for a given question. These
146166
will be autocompleted as the user types::
147167

148168
use Symfony\Component\Console\Question\Question;
149-
// ...
150169

151-
$bundles = array('AcmeDemoBundle', 'AcmeBlogBundle', 'AcmeStoreBundle');
152-
$question = new Question('Please enter the name of a bundle', 'FooBundle');
153-
$question->setAutocompleterValues($bundles);
170+
// ...
171+
public function execute(InputInterface $input, OutputInterface $output)
172+
{
173+
// ...
174+
$bundles = array('AcmeDemoBundle', 'AcmeBlogBundle', 'AcmeStoreBundle');
175+
$question = new Question('Please enter the name of a bundle', 'FooBundle');
176+
$question->setAutocompleterValues($bundles);
154177

155-
$name = $helper->ask($input, $output, $question);
178+
$name = $helper->ask($input, $output, $question);
179+
}
156180

157181
Hiding the User's Response
158182
~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -161,13 +185,17 @@ You can also ask a question and hide the response. This is particularly
161185
convenient for passwords::
162186

163187
use Symfony\Component\Console\Question\Question;
164-
// ...
165188

166-
$question = new Question('What is the database password?');
167-
$question->setHidden(true);
168-
$question->setHiddenFallback(false);
189+
// ...
190+
public function execute(InputInterface $input, OutputInterface $output)
191+
{
192+
// ...
193+
$question = new Question('What is the database password?');
194+
$question->setHidden(true);
195+
$question->setHiddenFallback(false);
169196

170-
$password = $helper->ask($input, $output, $question);
197+
$password = $helper->ask($input, $output, $question);
198+
}
171199

172200
..caution::
173201

@@ -189,20 +217,24 @@ be suffixed with ``Bundle``. You can validate that by using the
189217
method::
190218

191219
use Symfony\Component\Console\Question\Question;
192-
// ...
193-
194-
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
195-
$question->setValidator(function ($answer) {
196-
if ('Bundle' !== substr($answer, -6)) {
197-
throw new \RuntimeException(
198-
'The name of the bundle should be suffixed with \'Bundle\''
199-
);
200-
}
201-
return $answer;
202-
});
203-
$question->setMaxAttempts(2);
204220

205-
$name = $helper->ask($input, $output, $question);
221+
// ...
222+
public function execute(InputInterface $input, OutputInterface $output)
223+
{
224+
// ...
225+
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
226+
$question->setValidator(function ($answer) {
227+
if ('Bundle' !== substr($answer, -6)) {
228+
throw new \RuntimeException(
229+
'The name of the bundle should be suffixed with \'Bundle\''
230+
);
231+
}
232+
return $answer;
233+
});
234+
$question->setMaxAttempts(2);
235+
236+
$name = $helper->ask($input, $output, $question);
237+
}
206238

207239
The ``$validator`` is a callback which handles the validation. It should
208240
throw an exception if there is something wrong. The exception message is displayed
@@ -222,23 +254,26 @@ Validating a Hidden Response
222254
You can also use a validator with a hidden question::
223255

224256
use Symfony\Component\Console\Question\Question;
225-
// ...
226-
227-
$helper = $this->getHelper('question');
228257

229-
$question = new Question('Please enter your password');
230-
$question->setValidator(function($value) {
231-
if (trim($value) == ''){
232-
throw new \Exception('The password can not be empty');
233-
}
258+
// ...
259+
publicfunctionexecute(InputInterface $input, OutputInterface $output)
260+
{
261+
// ...
262+
$helper = $this->getHelper('question');
234263

235-
return $value;
236-
});
237-
$question->setHidden(true);
238-
$question->setMaxAttempts(20);
264+
$question = new Question('Please enter your password');
265+
$question->setValidator(function ($value) {
266+
if (trim($value) == '') {
267+
throw new \Exception('The password can not be empty');
268+
}
239269

240-
$password = $helper->ask($input, $output, $question);
270+
return $value;
271+
});
272+
$question->setHidden(true);
273+
$question->setMaxAttempts(20);
241274

275+
$password = $helper->ask($input, $output, $question);
276+
}
242277

243278
Testing a Command that Expects Input
244279
------------------------------------
@@ -276,6 +311,6 @@ from the command line, you need to set the helper input stream::
276311
}
277312

278313
By setting the input stream of the ``QuestionHelper``, you imitate what the
279-
console would do internally with all user input through thecli. This way
314+
console would do internally with all user input through theCLI. This way
280315
you can test any user interaction (even complex ones) by passing an appropriate
281316
input stream.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp