Skip to content

Commit 4525414

Browse files
authored
Complain about unknown arguments (#93)
1 parent 7de92e4 commit 4525414

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

benchmarks/run

+24-7
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,32 @@ if (! is_readable(__DIR__ . '/../vendor/autoload.php')) {
1919
require __DIR__ . '/../vendor/autoload.php';
2020
require __DIR__ . '/Support/helpers.php';
2121

22-
$opt = getopt(
23-
'h:p:s:a:v',
24-
[
25-
'host:', 'port:', 'verbose', 'workers:', 'filter:', 'duration:', 'runs:', 'warmup:',
26-
'json', 'help', 'user:', 'pass:', 'command-type:', 'key-type:'
27-
],
28-
$optind
22+
$short_options = ['h:', 'p:', 's:', 'a:', 'v'];
23+
$long_options = [
24+
'host:', 'port:', 'verbose', 'workers:', 'filter:', 'duration:', 'runs:', 'warmup:',
25+
'json', 'help', 'user:', 'pass:', 'command-type:', 'key-type:'
26+
];
27+
28+
$opt = getopt(implode($short_options), $long_options, $optind);
29+
30+
$options = array_reduce(
31+
array_merge($short_options, $long_options),
32+
function ($carry, $item) {
33+
$item = rtrim($item, ':');
34+
$prefix = strlen($item) > 1 ? '--' : '-';
35+
$carry[$prefix . $item] = null;
36+
return $carry;
37+
},
38+
['--' => null]
2939
);
3040

41+
for ($i = 1; $i < $optind; ++$i) {
42+
if (str_starts_with($argv[$i], '-') && ! array_key_exists($argv[$i], $options)) {
43+
fprintf(STDERR, "\n\033[41m ERROR \033[0m Unknown option '{$argv[$i]}'.\n");
44+
exit(1);
45+
}
46+
}
47+
3148
$host = $opt['h'] ?? $opt['host'] ?? '127.0.0.1';
3249
$port = $opt['p'] ?? $opt['port'] ?? 6379;
3350
$auth = $opt['a'] ?? $opt['auth'] ?? null;

0 commit comments

Comments
 (0)