Gorm wants to add dishes from the command line too. But he never remembers the order of the arguments, nor the exact names of the categories. He would like to type app:plat:ajouter, and nothing else, and have the command ask him the questions.
Interactive mode
A command meant for a human can ask for what it is missing:
#[Ask('…')], next to an argument: the question is asked if the argument is missing;- an
#[Interact]method: the questions that need code (a list that comes from the database); $io->choice(),$io->confirm(),$io->ask(): theSymfonyStylequestions.
The same command must remain usable by a script. With --no-interaction (-n), no question is asked, and the confirmations take their default value.
Your mission
Make app:plat:ajouter interactive:
- without arguments, it asks
Dish name?,Its price, in gold pieces?, thenWhich category?, to be picked from the menu's list; - an answer that is not in the list is rejected, and the question asked again;
- before writing, it asks
Add "Blackberry Tart" (4.50 gp, Desserts) to the menu?. If Gorm answers no:Nothing was added., and nothing is saved; - what is already given as an argument or an option is not asked again;
- a price that is not a number is rejected (
The price is a number…, validatorunPrix= a price); “4,5” with a comma is accepted too.
In the preview console
The browser console runs like a script, with nobody there to answer: it never asks questions. Try the non-interactive mode there, with all the arguments. The tests are the ones answering the questions, with CommandTester::setInputs(), exactly the way you test an interactive command in a real project.