@@ -19,15 +19,32 @@ if (! is_readable(__DIR__ . '/../vendor/autoload.php')) {
19
19
require __DIR__ . '/../vendor/autoload.php ' ;
20
20
require __DIR__ . '/Support/helpers.php ' ;
21
21
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 ]
29
39
);
30
40
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
+
31
48
$ host = $ opt ['h ' ] ?? $ opt ['host ' ] ?? '127.0.0.1 ' ;
32
49
$ port = $ opt ['p ' ] ?? $ opt ['port ' ] ?? 6379 ;
33
50
$ auth = $ opt ['a ' ] ?? $ opt ['auth ' ] ?? null ;
0 commit comments