fiég/shell

交互式 shell (cli) 组件,用于 PHP

1.0 2015-05-17 19:52 UTC

This package is auto-updated.

Last update: 2024-08-29 03:58:03 UTC


README

交互式 shell (cli) 组件,用于 PHP

Build Status

入门

use Fieg\Shell\Shell;
use Fieg\Shell\ShellEvents;

$shell = new Shell();

// handle some commands
$shell->on(ShellEvents::COMMAND, function ($command) use ($shell) {
    switch ($command) {
        case "help":
            $shell->publish('Available commands:');
            $shell->publish('  help   Print this help');
            $shell->publish('  exit   Exit program');
            break;

        case "exit":
            $shell->stop();
            break;

        // echo everything else the user types
        default:
            $shell->publish('echo: ' . $command);
    }
});

// print some info
$shell->publish("This is an interactive shell.");
$shell->publish("Type 'help' for all available commands.");

// start a prompt so we can receive user input
$shell->prompt();

// statements after this are only executed when `$shell->stop()` is called
$shell->run();

echo "Bye!" . PHP_EOL;

此库还支持历史记录功能。使用此功能,您可以使用上下箭头浏览最近输入的命令。要启用历史记录支持,只需将 Shell 类包裹在 HistoryDecorator 中即可。

$shell = new HistoryDecorator(new Shell());

您还可以输入“history”命令来查看所有最近输入的命令列表。