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

[Console] Add a RepeatedQuestion #19678

Closed
GuilhemN opened this issue Aug 19, 2016 · 13 comments
Closed

[Console] Add a RepeatedQuestion #19678

GuilhemN opened this issue Aug 19, 2016 · 13 comments

Comments

@GuilhemN
Copy link
Contributor

GuilhemN commented Aug 19, 2016

Actually when I want to ask several times the same question with SymfonyStyle i have a very verbose output:

What do you want? [default]:
> foo

What do you want? [default]:
> bar

What do you want? [default]:
>

I propose to introduce a RepeatedQuestion. It could be implemented like that:

class RepeatedQuestion extends Question
{
    private $generator;

    public function __construct($question, \Generator $generator)
    {
        parent::__construct($question);
        $this->generator = $generator;
    }

    public function addAnswer($answer)
    {
         $this->generator->send($answer);
    }

    public function shouldContinue(): bool
    {
        return $this->generator->valid();
    }

    public function getDefault()
    {
        return $this->generator->current();
    }
}

It would be used like that:

$question = new RepeatedQuestion(call_user_func(function() use ($default) {
    do {
        $answer = yield $default;
    } while ($answer !== $default);
}));
foreach ($helper->askQuestion($question) as $answer) {
    // do what you want
}

And the command output would be:

What do you want?
[default] > foo
[default] > bar
[default] >

WDYT?

@linaori
Copy link
Contributor

linaori commented Aug 20, 2016

Looks like a valid case to me

@GuilhemN
Copy link
Contributor Author

BTW it could also be used for things like multiline answer. Maybe we should choose a name showing this use case as well ?

@jameshalsall
Copy link
Contributor

This can easily be implemented in user land with Question

@GuilhemN
Copy link
Contributor Author

@jameshalsall how would you do that ? That's not that easy and would probably need some hacks.
Anyway even if that's doable in userland i don't understand why it wouldn't be supported by the core to make it easier to use.

@sstok
Copy link
Contributor

sstok commented Aug 22, 2016

Depending on there relationship this is not as easy as it seems, in https://github.com/rollerworks/SkeletonDancer I started with a collection of questions, but then I wanted to add support for conditional questions (only ask when x) and then it become complicated.

So Instead of adding all to a collection, each question is asked by a Questioner that keeps track of all questions, but instead of using the Symfony Console Question directly it uses a wrapper that creates the Question at a later state so that the default can be passed https://github.com/rollerworks/SkeletonDancer/blob/master/src/Question.php

@jameshalsall
Copy link
Contributor

jameshalsall commented Aug 22, 2016

@Ener-Getick at a simple level off the top of my head...

$answers = [];
$question = new Question('What is foo?', 'bar');
do {
    $answers[] = $questionHelper->ask($input, $output, $question);
} while ($answer !== 'bar');

$output->writeln('You answered with ' , implode(', ', $answers);

@sstok it may well get more complicated if you want to only ask Y when X has a certain answer, but that's more like a Questionnaire component, which may well have some value

@GuilhemN
Copy link
Contributor Author

@jameshalsall yes but as i said that's very verbose.

@jameshalsall
Copy link
Contributor

it's no more verbose than your suggested implementation, imo

@GuilhemN
Copy link
Contributor Author

GuilhemN commented Aug 22, 2016

@jameshalsall the console output is verbose, not the call to ask :P

@sstok
Copy link
Contributor

sstok commented Aug 22, 2016

@jameshalsall Good point, that's actually something I want to do in the long run :) but that will take some time.

@javiereguiluz javiereguiluz changed the title [RFC][Console] Add a RepeatedQuestion [Console] Add a RepeatedQuestion Aug 29, 2016
@javiereguiluz
Copy link
Member

I'm 👎 for this proposed feature:

  1. Having a repeated question in a command is not that common (unlike forms, where you always have a repeated password field, for example).
  2. The output is "verbose" ... because the command showed an example is verbose in the first place. You are asking 3 or 4 times the same thing, so it's a verbose command originally.
  3. It can be solved with existing features. E.g.
What do you want? (separate the values with a comma):
> foo, bar, baz

@GuilhemN
Copy link
Contributor Author

GuilhemN commented Aug 29, 2016

@javiereguiluz i want to use this in my QuickInstallBundle to reconfigure an app.
It is very common in Configuration to have array nodes and everytime to configure one of them it is verbose (see this example). When you don't have a default and you use small values you can of course use , but you have to explain it. But what if you want to ask for urls, usernames, etc?

BTW I think it would also be useful for multiline input (mails, comments, ...).

@javiereguiluz
Copy link
Member

Closing it because the related issue (#19699) was closed as "won't fix".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants