Bruna invents her dishes late at night, when the dining room is closed. Rather than opening the kitchen form, she would like to add them in a single line:
app:plat:ajouter "Shire Ale" 3.5 --categorie Drinks --specialite
(app:plat:ajouter = add a dish; --categorie = category, --specialite = specialty.)
Arguments and options
- An argument is a value expected at a precise position: the name, then the price.
- An option has a name (
--categorie), a default value, and can go anywhere. A boolean option is switched on without a value:--specialite.
In an invokable command, both are declared as parameters of __invoke(), with their type: Symfony converts 4.5 into a float, and --help documents them.
The same rules everywhere
A dish added from the command line must follow the same rules as a dish added through the form: a unique name, a positive price, a real description. These rules already live on the Plat entity (dish): the Validator applies them to any object, without a form.
Your mission
Write app:plat:ajouter:
- two arguments, the name (
nom) and the price (prix), and the--categorieoption: the dish is saved, then the command shows the address of its page (/dish/shire-ale); --specialitemakes it a specialty;--descriptiongives its description (a default description otherwise);- an invalid dish (a price of 0, a description that is too short…) is rejected with the entity's messages, and the code
Command::INVALID; - a dish already on the menu is rejected the same way;
- without
--categorie:Command::INVALID; with an unknown category:Command::FAILURE. In both cases, nothing is saved.
Try it in the Console tab, then check with app:inventaire. A value that contains spaces goes between quotes: --categorie "Main courses".