A simple menu for symfony console commands
a quick note:
This README assumes you have an understanding of creating and running a console command in the symfony ecosystem
composer require theroadbunch/command-menu
Creating, rendering, and using a menu in your symfony console command
<?php
// ...
use RoadBunch\CommandMenu\Menu;
use RoadBunch\CommandMenu\Option;
// ...
public function execute(InputInterface $input, OutputInterface $output)
{
// create the menu
$menu = new Menu($input, $output);
// optional title
$menu->title('Options');
// add options
$menu->addOption('add', 'Add User');
$menu->addOption('delete', 'Delete User');
$menu->addOption('quit', 'Quit');
// render the menu
$menu->render();
// prompt the user to make a selection
$selection = $menu->promptForSelection();
// do work
}
Output
Options
-------
1 Add User
2 Delete User
3 Quit
Please make a selection:
> |
If the user selects 1
then promptForSelection()
will return "add"
, 2
will return "delete"
, and
3
will return "quit"
In the event a user selects something other than a given selector, promptForSelection()
will return null