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] invalid default returned from ChoiceQuestion on non interactive mode #30774

Closed
ThHareau opened this issue Mar 29, 2019 · 3 comments
Closed

Comments

@ThHareau
Copy link

Symfony version(s) affected: symfony/console v4.2.3

Description

ChoiceQuestion returns the index in interactive mode but the value in non interactive mode

How to reproduce
Using this command:

class TestCommand extends Command
{
    protected function configure()
    {
        $this->setName('test');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $choices = [
            'a' => 'aaaa',
            'b' => 'bbbb',
        ];

        $questionHelper = new SymfonyQuestionHelper();
        $choice = $questionHelper->ask(
            $input,
            $output,
            new ChoiceQuestion('Does it work?', $choices, 'a')
        );

        $output->writeln("Choice: $choice");
    }
}

Then php bin/console test or php bin/console -n will not display the same value.

Possible Solution

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Console/Helper/QuestionHelper.php#L56 should probably return default, and not the value.

Additional context
The opposite bug with reported in #25470 , the non-interactive mode was returning the index instead of the value. I suppose the return value was changed from value to index meanwhile?

@jschaedl
Copy link
Contributor

jschaedl commented Apr 5, 2019

When you follow the symfony docs to create a ChoiceQuestion:

$question = new ChoiceQuestion(
        'Please select your favorite color (defaults to red)',
        ['red', 'blue', 'yellow'],
        0
    );

it will display the same output for bin/console test or bin/console test -n

See https://symfony.com/doc/current/components/console/helpers/questionhelper.html#let-the-user-choose-from-a-list-of-answers for more information and try to update your code to:

class TestCommand extends Command
{
    ...

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $choices = ['yes', 'no'];

        $question = new ChoiceQuestion('Does it work?', $choices, 0);

        $questionHelper = new SymfonyQuestionHelper();
        $choice = $questionHelper->ask($input, $output, $question);

        $output->writeln("Choice: " . $choice);
    }
}

@chalasr
Copy link
Member

chalasr commented Apr 5, 2019

The inconsistent result happens when using string keys for choices, the ChoiceQuestion default validator returns the key if it’s a string: https://github.com/symfony/console/blob/f6f44ce1c9513a37b8826ce7f3edd26d6ab7bf09/Question/ChoiceQuestion.php#L160
The difference is that the validator is not called in non-interactive mode, which is inconsistent.
I will submit a patch.

@ThHareau
Copy link
Author

ThHareau commented Apr 5, 2019

Thanks 👍

fabpot added a commit that referenced this issue Apr 6, 2019
…non-interactive mode (chalasr)

This PR was merged into the 3.4 branch.

Discussion
----------

[Console] Fix inconsistent result for choice questions in non-interactive mode

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  |no
| BC breaks?    | no
| Deprecations? | n/a
| Tests pass?   | yes
| Fixed tickets | #30774
| License       | MIT
| Doc PR        | n/a

Commits
-------

198b895 [Console] Fix inconsistent result for choice questions in non-interactive mode
@fabpot fabpot closed this as completed Apr 6, 2019
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

6 participants