Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

cli-menu

> 开发工具
Open source

Build beautiful PHP CLI menus. Simple yet Powerful. Expressive DSL.

1.9K stars0 likes2 views
WebsiteGitHub

About

Build beautiful PHP CLI menus. Simple yet Powerful. Expressive DSL.

</a>
<a href="https://ci.appveyor.com/project/mikeymike/cli-menu" title="Windows Build Status" target="_blank">
 
</a>
<a href="https://codecov.io/github/php-school/cli-menu" title="Coverage Status" target="_blank">
 
</a>
<a href="https://scrutinizer-ci.com/g/php-school/cli-menu/" title="Scrutinizer Code Quality" target="_blank">
 
</a>
<a href="https://phpschool-team.slack.com/messages">
  
</a>


Contents

  • Minimum Requirements
  • Installation
  • Upgrading
  • Usage
    • Quick Setup
    • Examples
  • API
    • Appearance
      • Menu Title
      • Colour
      • Width
      • Padding
      • Margin
      • Borders
      • Exit Button Text
      • Remove Exit Button
    • Items
      • Selectable Item
      • Checkbox Item
      • Radio Item
      • Line Break Item
      • Static Item
      • Ascii Art Item
      • Sub Menu Item
      • Split Item
    • Disabling Items & Sub Menus
    • Item Markers
    • Item Extra
    • Menu Methods
      • Redrawing the Menu
      • Getting, Removing and Adding items
    • Custom Control Mapping
    • Item Keyboard Shortcuts
    • Dialogues
      • Flash
      • Confirm
    • Inputs
      • Text
      • Number
      • Password
      • Custom Input
    • Dialogues & Input Styling
  • Docs Translations
  • Integrations

Minimum Requirements

  • PHP 7.1
  • Composer
  • ext-posix

Installation

bash
composer require php-school/cli-menu

Upgrading

Please refer to the Upgrade Documentation documentation to see what is required to upgrade your installed cli-menu version.

Usage

Quick Setup

Here is a super basic example menu which will echo out the text of the selected item to get you started.

php
<?php

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\Builder\CliMenuBuilder;

require_once(__DIR__ . '/../vendor/autoload.php');

$itemCallable = function (CliMenu $menu) {
    echo $menu->getSelectedItem()->getText();
};

$menu = (new CliMenuBuilder)
    ->setTitle('Basic CLI Menu')
    ->addItem('First Item', $itemCallable)
    ->addItem('Second Item', $itemCallable)
    ->addItem('Third Item', $itemCallable)
    ->addLineBreak('-')
    ->setBorder(1, 2, 'yellow')
    ->setPadding(2, 4)
    ->setMarginAuto()
    ->build();

$menu->open();

Examples

Check out the examples directory and run them to see what is possible! The best way to run the examples is to git clone the repository:

bash
git clone https://github.com/php-school/cli-menu.git
cd cli-menu
composer install --no-dev
cd examples
php basic.php

Basic Menu

Basic Menu Auto Centered

Basic Menu with separation

Menu with crazy separation

Custom Styles

Borders and 256 colours

Useful Separation

Displaying Item Extra

Remove Defaults

Submenu

Split Item

Disabled Items & Submenus

Checkbox Items

Radio Items

Flash Dialogue

Confirm Dialogue

Number Input

Text Input

Password Input

Using cli-menu to create art

Want to see something really cool? Well you can use cli-menu to create a drawing canvas on your terminal. Check it out!:

API

The CliMenu object is constructed via the Builder class

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
    /**
     *  Customise
    **/
    ->build();

Once you have a menu object, you can open and close it like so:

php
$menu->open();
$menu->close();

Appearance

Menu Title

You can give your menu a title and you can customise the separator, a line which displays under the title. Whatever string you pass to setTitleSeparator will be repeated for the width of the Menu.

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
    ->setTitle('One Menu to rule them all!')
    ->setTitleSeparator('*-')
    ->build();

Colour

You can change the foreground and background colour of the menu to any of the following colours:

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
    ->setForegroundColour('green')
    ->setBackgroundColour('black')
    ->build();

If your terminal supports 256 colours then you can also use any of those by specifying the code, like 230. You can find a list of the colours and codes here. If you specify a code and the terminal does not support 256 colours it will automatically fallback to a sane default, using a generated map you can see in src/Util/ColourUtil.php. You can also manually specify the fallback colour as the second argument to setForegroundColour and `setBackgroundColour.

In this example if no 256 colour support is found it will automatically fall back to green and blue.

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
    ->setForegroundColour('40')
    ->setBackgroundColour('92')
    ->build();

In this example if no 256 colour support is found it will fall back to yellow and magenta.

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
    ->setForegroundColour('40', 'yellow')
    ->setBackgroundColour('92', 'magenta')
    ->build();

Width

Customise the width of the menu. Setting a value larger than the size of the terminal will result in the width being the same as the terminal size. The width will include the padding and the border. So with a width of 100 and all around border of 5 and all around padding of 5 will leave for a content width of 80 (5 + 5 + 80 + 5 + 5).

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
    ->setWidth(1000) //if terminal is only 400, width will also be 400
    ->build();

If you want to use the full width of the terminal, you can grab the terminal object and ask/set it from there like so:

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = ($builder = new CliMenuBuilder)
    ->setWidth($builder->getTerminal()->getWidth())
    ->build();

If you want to use the full width of the terminal and apply a margin, use the terminal width, and we will do the calculations automatically (shrink the width based on the margin).

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = ($builder = new CliMenuBuilder)
    ->setWidth($builder->getTerminal()->getWidth())
    ->setMargin(2)
    ->build();

Padding

The padding can be set for all sides with one value or can be set individually for top/bottom and left/right.

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
    ->setPadding(10) //10 padding top/bottom/left/right
    ->build();

Different values can also be set for the top/bottom and the left/right padding:

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
    ->setPaddingTopBottom(10)
    ->setPaddingLeftRight(5)
    ->build();

Configure top/bottom and left/right padding using the shorthand method:

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
    ->setPadding(10, 5) //top/bottom = 10, left/right = 5
    ->build();

Margin

The margin can be customised as one value. It can also be set automatically which will center the menu nicely in the terminal.

Automatically center menu:

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
    ->setWidth(200)
    ->setMarginAuto() 
    ->build();

Arbitrary margin:

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
    ->setWidth(200)
    ->setMargin(5)
    ->build();

Borders

Borders can be customised just like CSS borders. We can add any amount of border to either side, left, right top or bottom and we can apply a colour to it.

Set universal red border of 2:

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
    ->setWidth(200)
    ->setBorder(2, 'red')
    ->build();

Configure each border separately:

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
    ->setWidth(200)
    ->setBorderTopWidth(2)
    ->setBorderRightWidth(4)
    ->setBorderBottomWidth(2)
    ->setBorderLeftWidth(4)
    ->setBorderColour('42', 'red') //SpringGreen2 fallback to red
    ->build();

Configure each border separately using the shorthand method, like CSS:

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
    ->setWidth(200)
    ->setBorder(3, 4, 'red') //top/bottom = 3, left/right = 4
    ->setBorder(3, 4, 5, 'red') //top = 3, left/right = 4, bottom = 5
    ->setBorder(3, 4, 5, 6, 'red') //top = 3, left = 4, bottom = 5, right = 6
    ->build();

Exit Button Text

Modify the exit button text:

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
    ->setExitButtonText("Don't you want me baby?")
    ->build();

Remove Exit Button

You can remove the exit button altogether:

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
    ->disableDefaultItems()
    ->build();

Note: This will also disable the Go Back button for sub menus.

You can manually add exit and go back buttons using the following:

php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\Action\ExitAction;
use PhpSchool\CliMenu\Action\GoBackAction;

$menu = (new CliMenuBuilder)
    ->disableDefaultItems()
    ->addSubMenu('Super Sub Menu', function (CliMenuBuilder $b) {
        $b->disableDefaultItems()
            ->setTitle('Behold the awesomeness')
            ->addItem('Return to parent menu', new GoBackAction); //add a go back button
    })
    ->addItem('Leave this place now !', new ExitAction) //add an exit button
    ->build();

Items

There a few different types of items you can add to your menu

  • Selectable Item - This is the type of item you need for something to be selectable (you can hit enter and it will invoke your callable)
  • Checkbox Item - This is a checkbox type of item that keeps track of its toggled state to show a different marker.
  • Radio Item - This is a radio type of item that keeps track of its toggled state to show a different marker. Disables all other radios within its CliMenu level.
  • Line Break Item - This is used to break up areas, it can span multiple lines and will be the width of Menu. Whatever string is passed will be repeated.
  • Static Item - This will print whatever text is passed, useful for headings.
  • Ascii Art Item - Special item which allows usage of Ascii art. It takes care of padding and alignment.
  • Sub Menu Item - Special item to allow an item to open another menu. Useful for an options menu.
  • Split Item - Special item to fit multiple items on the same row.

Selectabl

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPclicli-menumenuterminal

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具