Skip to content

A class for creating menus in symfony console commands

License

Notifications You must be signed in to change notification settings

The-Road-Bunch/command-menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

97 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

theroadbunch/command-menu build status

A simple menu for symfony console commands

Latest Stable Version License: MIT Compatible Symfony Version

a quick note:
This README assumes you have an understanding of creating and running a console command in the symfony ecosystem

Contents

  1. Release Notes
  2. Installation
  3. Usage
    a. Basic Usage
    b. Selectors
    c. Wrappers
  4. License

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