-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
03-commander.php
68 lines (57 loc) · 2.03 KB
/
03-commander.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
require __DIR__ . '/../vendor/autoload.php';
$stdio = new Clue\React\Stdio\Stdio();
$stdio->setPrompt('> ');
// limit history to HISTSIZE env
$limit = getenv('HISTSIZE');
if ($limit === '' || $limit < 0) {
// empty string or negative value means unlimited
$stdio->limitHistory(null);
} elseif ($limit !== false) {
// apply any other value if given
$stdio->limitHistory($limit);
}
// register all available commands and their arguments
$router = new Clue\Commander\Router();
$router->add('exit | quit', function() use ($stdio) {
$stdio->end();
});
$router->add('help', function () use ($stdio) {
$stdio->write('Use TAB-completion or use "exit"' . PHP_EOL);
});
$router->add('(echo | print) <words>...', function (array $args) use ($stdio) {
$stdio->write(implode(' ', $args['words']) . PHP_EOL);
});
$router->add('printf <format> <args>...', function (array $args) use ($stdio) {
$stdio->write(vsprintf($args['format'],$args['args']) . PHP_EOL);
});
// autocomplete the following commands (at offset=0/1 only)
$stdio->setAutocomplete(function ($_, $offset) {
return $offset > 1 ? array() : array('exit', 'quit', 'help', 'echo', 'print', 'printf');
});
$stdio->write('Welcome to this interactive demo' . PHP_EOL);
// react to commands the user entered
$stdio->on('data', function ($line) use ($router, $stdio) {
$line = rtrim($line, "\r\n");
// add all lines from input to history
// skip empty line and duplicate of previous line
$all = $stdio->listHistory();
if ($line !== '' && $line !== end($all)) {
$stdio->addHistory($line);
}
try {
$args = Clue\Arguments\split($line);
} catch (Clue\Arguments\UnclosedQuotesException $e) {
$stdio->write('Error: Invalid command syntax (unclosed quotes)' . PHP_EOL);
return;
}
// skip empty lines
if (!$args) {
return;
}
try {
$router->handleArgs($args);
} catch (Clue\Commander\NoRouteFoundException $e) {
$stdio->write('Error: Invalid command usage' . PHP_EOL);
}
});