-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnacl
executable file
·64 lines (55 loc) · 1.54 KB
/
nacl
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
#!/usr/bin/env php
<?php
foreach ([__DIR__ . '/../../autoload.php', __DIR__ . '/../vendor/autoload.php', __DIR__ . '/vendor/autoload.php'] as $file) {
if (file_exists($file)) {
require $file;
break;
}
}
$options = [
'action' => '',
'input' => [],
];
for ($i = 1; $i < $_SERVER['argc']; $i++) {
switch ($_SERVER['argv'][$i]) {
case '-l':
case '--lint':
$options['action'] = 'lint';
break;
case '-h':
case '--help':
$options['action'] = 'help';
break;
default:
$input = $_SERVER['argv'][$i];
if ('-' === substr($input, 0, 1)) {
die('Unknown option ' . $input . PHP_EOL);
}
$options['input'][] = $input;
}
}
if ('help' === $options['action']) {
$help = <<<"HELP"
nacl [options] FILES...
Options:
-h, --help Display this help
-l, --lint Syntax check (lint)
HELP;
die($help);
}
if (empty($options['input'])) {
die('You must specify at least one input file.' . PHP_EOL);
}
$parser = Nuglif\Nacl\Nacl::createParser();
foreach ($options['input'] as $file) {
try {
$r = $parser->parseFile($file);
if ('lint' === $options['action']) {
printf('No syntax errors detected in %s' . PHP_EOL, $file);
} else {
echo Nuglif\Nacl\Nacl::dump($r), PHP_EOL;
}
} catch (Nuglif\Nacl\Exception $e) {
printf('%s in %s on line %d' . PHP_EOL, $e->getMessage(), $e->getFile(), $e->getLine());
}
}