Skip to content

Commit

Permalink
Complain about unknown arguments (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
yatsukhnenko authored Jul 17, 2023
1 parent 7de92e4 commit 4525414
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions benchmarks/run
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,32 @@ if (! is_readable(__DIR__ . '/../vendor/autoload.php')) {
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/Support/helpers.php';

$opt = getopt(
'h:p:s:a:v',
[
'host:', 'port:', 'verbose', 'workers:', 'filter:', 'duration:', 'runs:', 'warmup:',
'json', 'help', 'user:', 'pass:', 'command-type:', 'key-type:'
],
$optind
$short_options = ['h:', 'p:', 's:', 'a:', 'v'];
$long_options = [
'host:', 'port:', 'verbose', 'workers:', 'filter:', 'duration:', 'runs:', 'warmup:',
'json', 'help', 'user:', 'pass:', 'command-type:', 'key-type:'
];

$opt = getopt(implode($short_options), $long_options, $optind);

$options = array_reduce(
array_merge($short_options, $long_options),
function ($carry, $item) {
$item = rtrim($item, ':');
$prefix = strlen($item) > 1 ? '--' : '-';
$carry[$prefix . $item] = null;
return $carry;
},
['--' => null]
);

for ($i = 1; $i < $optind; ++$i) {
if (str_starts_with($argv[$i], '-') && ! array_key_exists($argv[$i], $options)) {
fprintf(STDERR, "\n\033[41m ERROR \033[0m Unknown option '{$argv[$i]}'.\n");
exit(1);
}
}

$host = $opt['h'] ?? $opt['host'] ?? '127.0.0.1';
$port = $opt['p'] ?? $opt['port'] ?? 6379;
$auth = $opt['a'] ?? $opt['auth'] ?? null;
Expand Down

0 comments on commit 4525414

Please sign in to comment.