Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Symfony/Component/Console/Question/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Question
private $autocompleterValues;
private $validator;
private $default;
private $showDefault = false;
Copy link
Author

Choose a reason for hiding this comment

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

Thinking about usability, should the default be shown by default?

private $normalizer;

/**
Expand All @@ -46,6 +47,10 @@ public function __construct($question, $default = null)
*/
public function getQuestion()
{
if ($this->showDefault) {
return sprintf('%s [%s]', $this->question, $this->default);
}

return $this->question;
}

Expand All @@ -59,6 +64,20 @@ public function getDefault()
return $this->default;
}

/**
* Sets whether the default answer should be shown to the user in the question
*
* @param bool $showDefault
*
* @return Question The current instance
*/
public function setShowDefault($showDefault)
Copy link
Member

Choose a reason for hiding this comment

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

What about setting a default of true?

Copy link
Author

Choose a reason for hiding this comment

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

Not sure if we should be more explicit and require true to be passed. Another opinion please?

Copy link
Member

Choose a reason for hiding this comment

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

@wouterj You mean line 28 not the method parameter, don't you?

{
$this->showDefault = (bool) $showDefault;

return $this;
}

/**
* Returns whether the user response must be hidden.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ public function testAskChoice()
$this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
}

public function testAskWithShowDefaults()
{
$dialog = new QuestionHelper();

$dialog->setInputStream($this->getInputStream("\n8AM\n"));

$question = new Question('What time is it?', '2PM');
$question->setShowDefault(true);
$this->assertEquals('2PM', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));

$question = new Question('What time is it?', '2PM');
$question->setShowDefault(true);
$this->assertEquals('8AM', $dialog->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));

rewind($output->getStream());
$this->assertEquals('What time is it? [2PM]', stream_get_contents($output->getStream()));
}

public function testAsk()
{
$dialog = new QuestionHelper();
Expand Down