-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path02-errors.php
35 lines (30 loc) · 1010 Bytes
/
02-errors.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
<?php
require __DIR__ . '/../vendor/autoload.php';
$router = new Clue\Commander\Router();
$router->add('exit [<code:uint>]', function (array $args) {
$code = isset($args['code']) ? $args['code'] : 0;
if ($code > 255) {
throw new \InvalidArgumentException('Invalid exit code given');
}
exit($code);
});
$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 | -h]', function () use ($router) {
echo 'Usage:' . PHP_EOL;
foreach ($router->getRoutes() as $route) {
echo ' ' .$route . PHP_EOL;
}
});
try {
$router->handleArgv();
} catch (Clue\Commander\NoRouteFoundException $e) {
echo 'Usage error: ' . $e->getMessage() . PHP_EOL;
echo 'Run without arguments if you need help with usage' . PHP_EOL;
} catch (Exception $e) {
echo 'Program error: ' . $e->getMessage() . PHP_EOL;
}