-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path03-interactive.php
52 lines (44 loc) · 1.27 KB
/
03-interactive.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
<?php
require __DIR__ . '/../vendor/autoload.php';
$router = new Clue\Commander\Router();
$router->add('exit [<code:uint>]', function (array $args) {
exit(isset($args['code']) ? $args['code'] : 0);
});
$router->add('sleep <seconds:uint>', function (array $args) {
sleep($args['seconds']);
});
$router->add('echo <words>...', function (array $args) {
echo join(' ', $args['words']) . PHP_EOL;
});
$router->add('help', function () use ($router) {
echo 'Usage:' . PHP_EOL;
foreach ($router->getRoutes() as $route) {
echo ' ' .$route . PHP_EOL;
}
});
echo 'Hello! Try the sleep, echo and exit commands.' . PHP_EOL;
while (true) {
echo '> ';
$line = fgets(STDIN, 1024);
// stop loop if STDIN is no longer readable
if ($line === false) {
break;
}
// try to parse command line or complain
try {
$args = Clue\Arguments\split($line);
} catch (\RuntimeException $e) {
echo 'Invalid command line. Missing quotes?' . PHP_EOL;
continue;
}
// skip empty lines
if (!$args) {
continue;
}
// process the given#
try {
$router->handleArgs($args);
} catch (Clue\Commander\NoRouteFoundException $e) {
echo 'Usage error. ' . $e->getMessage() . PHP_EOL;
}
}