https://github.com/fieg/shell
Interactive shell (cli) component for PHP
https://github.com/fieg/shell
Last synced: about 1 year ago
JSON representation
Interactive shell (cli) component for PHP
- Host: GitHub
- URL: https://github.com/fieg/shell
- Owner: fieg
- Created: 2015-05-17T13:20:53.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-05-17T20:09:19.000Z (about 11 years ago)
- Last Synced: 2025-01-29T04:35:00.270Z (over 1 year ago)
- Language: PHP
- Homepage:
- Size: 129 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Interactive shell (cli) component for PHP
[](https://travis-ci.org/fieg/shell)
Getting started
---------------
```php
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;
```
This library also comes with a history support. With this you can use the up and down arrows to
browse through the recently typed commands. To enable the history support, just wrap the Shell
class in a HistoryDecorator:
```php
$shell = new HistoryDecorator(new Shell());
```
You can also type the command "history" to see a list of all recently typed commands.