2017 © Pedro Peláez
 

library cli-menu

Cli Menu code for writing CLI applications in PHP 5.3+

image

programster/cli-menu

Cli Menu code for writing CLI applications in PHP 5.3+

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 5 % Grown

The README.md

CLI Menu

This package makes it simple to create menus in the CLI for terminal applications. There are two primary types., (*1)

  • Action Menu - print out a list of options and the options fire a callback that does something. E.g. exit the program or process a file.
  • Value Menu - prompt the user to make a choice from a limited set. E.g. output a list of the local files, and have the user select one by typing in the menu option index instead of a full path.

Install

Install the package with:, (*2)

composer require programster/cli-menu

Example Code

Action Menu

Here is some code demonstrating that you can build an action menu out of simple clusures, or even invokable objects:, (*3)

$overComplicatedPrintHelloWorldObject = new class {
    public function __invoke() {
        print PHP_EOL . "Hello world!" . PHP_EOL;
    }
};

$exitCallback = function() { die("Goodbye then!" . PHP_EOL); };
$sayHelloWorldOption = new \Programster\CliMenu\MenuOption("Print hello world", $overComplicatedPrintHelloWorldObject);
$exitOption = new \Programster\CliMenu\MenuOption("Exit", $exitCallback);
$cliMenu = new Programster\CliMenu\ActionMenu("My Action Menu", $sayHelloWorldOption, $exitOption);

while(true)
{
    $cliMenu->run();
}

Value Menu

Here is a basic example where we get the user to pick the number of seconds they wish for:, (*4)

$possibleValues = array(
    new Programster\CliMenu\ValueOption("Seconds in a day", (60 * 60 * 24)),
    new Programster\CliMenu\ValueOption("Seconds in a week", (60 * 60 * 24 * 52)),
    new Programster\CliMenu\ValueOption("Seconds in a year", (60 * 60 * 24 * 365)),
);

$valueMenu = new Programster\CliMenu\ValueMenu("Pick your seconds amount", ...$possibleValues);

$chosenValue = $valueMenu->run();

The Versions

30/11 2015

dev-master

9999999-dev http://blog.programster.org/

Cli Menu code for writing CLI applications in PHP 5.3+

  Sources   Download

None

The Requires

  • php >=5.3.0

 

library core

30/11 2015

1.0.0

1.0.0.0 http://blog.programster.org/

Cli Menu code for writing CLI applications in PHP 5.3+

  Sources   Download

None

The Requires

  • php >=5.3.0

 

library core